DBIx-Custom / t / sqlite.t /
Newer Older
3675 lines | 120.514kb
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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
22
# Constant
23
my %memory = (dsn => 'dbi:SQLite:dbname=:memory:');
24
my $create_table_default = 'create table table1 (key1 char(255), key2 char(255));';
25

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

            
57
# Prepare table
cleanup test
Yuki Kimoto authored on 2011-08-06
58
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
59

            
60
test 'DBIx::Custom::SQLTemplate basic tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
61
$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
62
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
63
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
64

            
65
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
66
$query = $dbi->execute($source, {}, query => 1);
67
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
68
$rows = $result->all;
69
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
70

            
71
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
72
$query = $dbi->execute($source, {}, query => 1);
73
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
74
$rows = $result->all;
75
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
76

            
77
$source = "select * from table1 where {<= key1} and {like key2};";
78
$query = $dbi->execute($source, {}, query => 1);
79
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
80
$rows = $result->all;
81
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
82

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

            
89
$source = "select * from table1 where {in key1 2};";
90
$query = $dbi->execute($source, {}, query => 1);
91
$result = $dbi->execute($query, param => {key1 => [9, 1]});
92
$rows = $result->all;
93
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
94

            
95
test 'DBIx::Custom::SQLTemplate insert tag';
96
$dbi->execute("delete from table1");
cleanup test
Yuki Kimoto authored on 2011-08-06
97
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
98
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
99

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

            
104
test 'DBIx::Custom::SQLTemplate update tag';
105
$dbi->execute("delete from table1");
cleanup test
Yuki Kimoto authored on 2011-08-06
106
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
107
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
108
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
cleanup test
Yuki Kimoto authored on 2011-08-06
109

            
cleanup test
Yuki Kimoto authored on 2011-08-06
110
$update_source = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
111
$dbi->execute($update_source, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
112

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

            
118

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

            
125
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
126
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
127
$rows = $result->all;
128
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
129

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

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

            
140
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
141
$result = $dbi->execute(
142
    $source,
143
    param => {'table1.key1' => 1, 'table1.key2' => 1},
144
    filter => {'table1.key2' => sub { $_[0] * 2 }}
145
);
146
$rows = $result->all;
147
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
148

            
cleanup test
Yuki Kimoto authored on 2011-08-06
149
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
150
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
151
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
152
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
153
$result = $dbi->execute(
154
    $source,
155
    param => {'key2' => 2},
156
);
157

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
161
$dbi = DBIx::Custom->connect(%memory);
162
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
163
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
164
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
165
$result = $dbi->execute(
166
    $source,
167
    param => {'key2' => 2},
168
);
169
$rows = $result->all;
170
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
171

            
172

            
173
test 'Error case';
174
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')};
175
ok($@, "connect error");
176

            
cleanup test
Yuki Kimoto authored on 2011-08-06
177
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
178
eval{$dbi->execute("{p }", {}, query => 1)};
179
ok($@, "create_query invalid SQL template");
180

            
181
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-06
182
$dbi = DBIx::Custom->connect(%memory);
183
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
184
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
185
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
186
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
187
$rows   = $result->all;
188
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
189

            
190
$dbi->execute('delete from table1');
191
$dbi->register_filter(
192
    twice       => sub { $_[0] * 2 },
193
    three_times => sub { $_[0] * 3 }
194
);
195
$dbi->default_bind_filter('twice');
196
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
test cleanup
Yuki Kimoto authored on 2011-08-06
197
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
198
$rows   = $result->all;
199
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
200
$dbi->default_bind_filter(undef);
201

            
cleanup test
Yuki Kimoto authored on 2011-08-06
202
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
203
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
204
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
205
$rows = $dbi->select(table => 'table1')->all;
206
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
207

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

            
211
eval{$dbi->insert(table => 'table', param => {';' => 1})};
212
like($@, qr/safety/);
213

            
cleanup test
Yuki Kimoto authored on 2011-08-06
214
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
215
$dbi->quote('"');
216
$dbi->execute('create table "table" ("select")');
217
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
218
$dbi->insert(table => 'table', param => {select => 1});
219
$result = $dbi->execute('select * from "table"');
220
$rows   = $result->all;
221
is_deeply($rows, [{select => 2}], "reserved word");
222

            
cleanup test
Yuki Kimoto authored on 2011-08-06
223
$dbi = DBIx::Custom->connect(%memory);
224
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
225
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
226
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
227
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
228
$rows   = $result->all;
229
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
230

            
cleanup test
Yuki Kimoto authored on 2011-08-06
231
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
232
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
233
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
234
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
235
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
236
$rows   = $result->all;
237
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
238

            
cleanup test
Yuki Kimoto authored on 2011-08-06
239
$dbi = DBIx::Custom->connect(%memory);
240
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
241
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
242
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
test cleanup
Yuki Kimoto authored on 2011-08-06
243
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
244
$rows   = $result->all;
245
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
246

            
247
test 'update';
cleanup test
Yuki Kimoto authored on 2011-08-06
248
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
249
$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
250
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
251
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
252
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
253
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
254
$rows   = $result->all;
255
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
256
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
257
                  "basic");
258
                  
259
$dbi->execute("delete from table1");
260
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
261
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
262
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-06
263
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
264
$rows   = $result->all;
265
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
266
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
267
                  "update key same as search key");
268

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

            
276
$dbi->execute("delete from table1");
277
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
278
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
279
$dbi->register_filter(twice => sub { $_[0] * 2 });
280
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
281
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-06
282
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
283
$rows   = $result->all;
284
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
285
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
286
                  "filter");
287

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

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
296
$dbi = DBIx::Custom->connect(%memory);
297
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
298
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
299
$where = $dbi->where;
300
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
301
$where->param({key1 => 1, key2 => 2});
302
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
303
$result = $dbi->select(table => 'table1');
304
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
305

            
cleanup test
Yuki Kimoto authored on 2011-08-06
306
$dbi = DBIx::Custom->connect(%memory);
307
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
308
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
309
$dbi->update(
310
    table => 'table1',
311
    param => {key1 => 3},
312
    where => [
313
        ['and', 'key1 = :key1', 'key2 = :key2'],
314
        {key1 => 1, key2 => 2}
315
    ]
316
);
317
$result = $dbi->select(table => 'table1');
318
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
319

            
cleanup test
Yuki Kimoto authored on 2011-08-06
320
$dbi = DBIx::Custom->connect(%memory);
321
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
322
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
323
$where = $dbi->where;
324
$where->clause(['and', 'key2 = :key2']);
325
$where->param({key2 => 2});
326
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
327
$result = $dbi->select(table => 'table1');
328
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
329

            
330
eval{$dbi->update(table => 'table1', param => {';' => 1})};
331
like($@, qr/safety/);
332

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
336
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
337
$dbi->quote('"');
338
$dbi->execute('create table "table" ("select", "update")');
339
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
340
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
341
$dbi->insert(table => 'table', param => {select => 1});
342
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
343
$result = $dbi->execute('select * from "table"');
344
$rows   = $result->all;
345
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
346

            
347
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
348
like($@, qr/safety/);
349

            
cleanup test
Yuki Kimoto authored on 2011-08-06
350
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
351
$dbi->reserved_word_quote('"');
352
$dbi->execute('create table "table" ("select", "update")');
353
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
354
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
355
$dbi->insert(table => 'table', param => {select => 1});
356
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
357
$result = $dbi->execute('select * from "table"');
358
$rows   = $result->all;
359
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
360

            
cleanup test
Yuki Kimoto authored on 2011-08-06
361
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
362
$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
363
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
364
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
365
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
366
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
367
$rows   = $result->all;
368
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
369
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
370
                  "basic");
371

            
cleanup test
Yuki Kimoto authored on 2011-08-06
372
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
373
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
374
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
375
$dbi->update(table => 'table1', param => {key2 => 4},
376
  where => {key1 => 1}, prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-06
377
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
378
$rows   = $result->all;
379
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
380

            
cleanup test
Yuki Kimoto authored on 2011-08-06
381
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
382
$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
383
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
384
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
385
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
386
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
387
$rows   = $result->all;
388
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
389
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
390
                  "basic");
391

            
392
test 'update_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
393
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
394
$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
395
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
396
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
397
$dbi->register_filter(twice => sub { $_[0] * 2 });
398
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
399
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
400
$rows   = $result->all;
401
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
402
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
403
                  "filter");
404

            
405

            
406
test 'delete';
cleanup test
Yuki Kimoto authored on 2011-08-06
407
$dbi = DBIx::Custom->connect(%memory);
408
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
409
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
410
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
411
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
412
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
413
$rows   = $result->all;
414
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
415

            
416
$dbi->execute("delete from table1;");
417
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
418
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
419
$dbi->register_filter(twice => sub { $_[0] * 2 });
420
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
test cleanup
Yuki Kimoto authored on 2011-08-06
421
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
422
$rows   = $result->all;
423
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
424

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

            
427
$dbi->delete_all(table => 'table1');
428
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
429
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
430
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
431
$rows = $dbi->select(table => 'table1')->all;
432
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
433

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

            
cleanup test
Yuki Kimoto authored on 2011-08-06
437
$dbi = DBIx::Custom->connect(%memory);
438
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
439
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
440
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
441
$where = $dbi->where;
442
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
443
$where->param({ke1 => 1, key2 => 2});
444
$dbi->delete(table => 'table1', where => $where);
445
$result = $dbi->select(table => 'table1');
446
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
447

            
cleanup test
Yuki Kimoto authored on 2011-08-06
448
$dbi = DBIx::Custom->connect(%memory);
449
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
450
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
451
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
452
$dbi->delete(
453
    table => 'table1',
454
    where => [
455
        ['and', 'key1 = :key1', 'key2 = :key2'],
456
        {ke1 => 1, key2 => 2}
457
    ]
458
);
459
$result = $dbi->select(table => 'table1');
460
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
461

            
cleanup test
Yuki Kimoto authored on 2011-08-06
462
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
463
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
464
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
465
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
test cleanup
Yuki Kimoto authored on 2011-08-06
466
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
467
$rows   = $result->all;
468
is_deeply($rows, [], "basic");
469

            
470
test 'delete error';
cleanup test
Yuki Kimoto authored on 2011-08-06
471
$dbi = DBIx::Custom->connect(%memory);
472
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
473
eval{$dbi->delete(table => 'table1')};
474
like($@, qr/"where" must be specified/,
475
         "where key-value pairs not specified");
476

            
477
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
478
like($@, qr/safety/);
479

            
cleanup test
Yuki Kimoto authored on 2011-08-06
480
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
481
$dbi->quote('"');
482
$dbi->execute('create table "table" ("select", "update")');
483
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
484
$dbi->insert(table => 'table', param => {select => 1});
485
$dbi->delete(table => 'table', where => {select => 1});
486
$result = $dbi->execute('select * from "table"');
487
$rows   = $result->all;
488
is_deeply($rows, [], "reserved word");
489

            
490
test 'delete_all';
cleanup test
Yuki Kimoto authored on 2011-08-06
491
$dbi = DBIx::Custom->connect(%memory);
492
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
493
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
494
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
495
$dbi->delete_all(table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
496
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
497
$rows   = $result->all;
498
is_deeply($rows, [], "basic");
499

            
500

            
501
test 'select';
cleanup test
Yuki Kimoto authored on 2011-08-06
502
$dbi = DBIx::Custom->connect(%memory);
503
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
504
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
505
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
506
$rows = $dbi->select(table => 'table1')->all;
507
is_deeply($rows, [{key1 => 1, key2 => 2},
508
                  {key1 => 3, key2 => 4}], "table");
509

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

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

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

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

            
522
$dbi->register_filter(decrement => sub { $_[0] - 1 });
523
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
524
            ->all;
525
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
526

            
cleanup test
Yuki Kimoto authored on 2011-08-06
527
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
528
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
529
$rows = $dbi->select(
530
    table => [qw/table1 table2/],
531
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
532
    where   => {'table1.key2' => 2},
533
    relation  => {'table1.key1' => 'table2.key1'}
534
)->all;
535
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
536

            
537
$rows = $dbi->select(
538
    table => [qw/table1 table2/],
539
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
540
    relation  => {'table1.key1' => 'table2.key1'}
541
)->all;
542
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
543

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

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

            
556
test 'fetch filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
557
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
558
$dbi->register_filter(
559
    twice       => sub { $_[0] * 2 },
560
    three_times => sub { $_[0] * 3 }
561
);
562
$dbi->default_fetch_filter('twice');
cleanup test
Yuki Kimoto authored on 2011-08-06
563
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
564
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
565
$result = $dbi->select(table => 'table1');
566
$result->filter({key1 => 'three_times'});
567
$row = $result->one;
568
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
569

            
570
test 'filters';
571
$dbi = DBIx::Custom->new;
572

            
573
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
574
   'あ', "decode_utf8");
575

            
576
is($dbi->filters->{encode_utf8}->('あ'),
577
   encode_utf8('あ'), "encode_utf8");
578

            
579
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
580
$dbi = DBIx::Custom->connect(%memory);
581
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
582
$dbi->dbh->begin_work;
583
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
584
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
585
$dbi->dbh->commit;
586
$result = $dbi->select(table => 'table1');
587
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
588
          "commit");
589

            
cleanup test
Yuki Kimoto authored on 2011-08-06
590
$dbi = DBIx::Custom->connect(%memory);
591
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
592
$dbi->dbh->begin_work(0);
593
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
594
$dbi->dbh->rollback;
595

            
596
$result = $dbi->select(table => 'table1');
597
ok(! $result->fetch_first, "rollback");
598

            
599
test 'cache';
cleanup test
Yuki Kimoto authored on 2011-08-06
600
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
601
$dbi->cache(1);
cleanup test
Yuki Kimoto authored on 2011-08-06
602
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
603
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
604
$dbi->execute($source, {}, query => 1);
605
is_deeply($dbi->{_cached}->{$source}, 
606
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
607

            
cleanup test
Yuki Kimoto authored on 2011-08-06
608
$dbi = DBIx::Custom->connect(%memory);
609
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
610
$dbi->{_cached} = {};
611
$dbi->cache(0);
612
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
613
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
614

            
615
test 'execute';
cleanup test
Yuki Kimoto authored on 2011-08-06
616
$dbi = DBIx::Custom->connect(%memory);
617
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
618
{
619
    local $Carp::Verbose = 0;
620
    eval{$dbi->execute('select * frm table1')};
621
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
622
    like($@, qr/\.t /, "fail : not verbose");
623
}
624
{
625
    local $Carp::Verbose = 1;
626
    eval{$dbi->execute('select * frm table1')};
627
    like($@, qr/Custom.*\.t /s, "fail : verbose");
628
}
629

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

            
633
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
634
$dbi->dbh->disconnect;
635
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
636
ok($@, "execute fail");
637

            
638
{
639
    local $Carp::Verbose = 0;
640
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
641
    like($@, qr/\Q.t /, "caller spec : not vebose");
642
}
643
{
644
    local $Carp::Verbose = 1;
645
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
646
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
647
}
648

            
649

            
650
test 'transaction';
cleanup test
Yuki Kimoto authored on 2011-08-06
651
$dbi = DBIx::Custom->connect(%memory);
652
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
653

            
654
$dbi->begin_work;
655

            
656
eval {
657
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
658
    die "Error";
659
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
660
};
661

            
662
$dbi->rollback if $@;
663

            
664
$result = $dbi->select(table => 'table1');
665
$rows = $result->all;
666
is_deeply($rows, [], "rollback");
667

            
668
$dbi->begin_work;
669

            
670
eval {
671
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
672
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
673
};
674

            
675
$dbi->commit unless $@;
676

            
677
$result = $dbi->select(table => 'table1');
678
$rows = $result->all;
679
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
680

            
681
$dbi->dbh->{AutoCommit} = 0;
682
eval{ $dbi->begin_work };
683
ok($@, "exception");
684
$dbi->dbh->{AutoCommit} = 1;
685

            
686

            
687
test 'method';
cleanup test
Yuki Kimoto authored on 2011-08-06
688
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
689
$dbi->method(
690
    one => sub { 1 }
691
);
692
$dbi->method(
693
    two => sub { 2 }
694
);
695
$dbi->method({
696
    twice => sub {
697
        my $self = shift;
698
        return $_[0] * 2;
699
    }
700
});
701

            
702
is($dbi->one, 1, "first");
703
is($dbi->two, 2, "second");
704
is($dbi->twice(5), 10 , "second");
705

            
706
eval {$dbi->XXXXXX};
707
ok($@, "not exists");
708

            
709
test 'out filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
710
$dbi = DBIx::Custom->connect(%memory);
711
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
712
$dbi->register_filter(twice => sub { $_[0] * 2 });
713
$dbi->register_filter(three_times => sub { $_[0] * 3});
714
$dbi->apply_filter(
715
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
716
              'key2' => {out => 'three_times', in => 'twice'});
717
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
718
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
719
$row   = $result->fetch_hash_first;
720
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
721
$result = $dbi->select(table => 'table1');
722
$row   = $result->one;
723
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
724

            
cleanup test
Yuki Kimoto authored on 2011-08-06
725
$dbi = DBIx::Custom->connect(%memory);
726
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
727
$dbi->register_filter(twice => sub { $_[0] * 2 });
728
$dbi->register_filter(three_times => sub { $_[0] * 3});
729
$dbi->apply_filter(
730
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
731
              'key2' => {out => 'three_times', in => 'twice'});
732
$dbi->apply_filter(
733
    'table1', 'key1' => {out => undef}
734
); 
735
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
736
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
737
$row   = $result->one;
738
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
739

            
cleanup test
Yuki Kimoto authored on 2011-08-06
740
$dbi = DBIx::Custom->connect(%memory);
741
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
742
$dbi->register_filter(twice => sub { $_[0] * 2 });
743
$dbi->apply_filter(
744
    'table1', 'key1' => {out => 'twice', in => 'twice'}
745
);
746
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
747
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
test cleanup
Yuki Kimoto authored on 2011-08-06
748
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
749
$row   = $result->one;
750
is_deeply($row, {key1 => 4, key2 => 2}, "update");
751

            
cleanup test
Yuki Kimoto authored on 2011-08-06
752
$dbi = DBIx::Custom->connect(%memory);
753
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
754
$dbi->register_filter(twice => sub { $_[0] * 2 });
755
$dbi->apply_filter(
756
    'table1', 'key1' => {out => 'twice', in => 'twice'}
757
);
758
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
759
$dbi->delete(table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-06
760
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
761
$rows   = $result->all;
762
is_deeply($rows, [], "delete");
763

            
cleanup test
Yuki Kimoto authored on 2011-08-06
764
$dbi = DBIx::Custom->connect(%memory);
765
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
766
$dbi->register_filter(twice => sub { $_[0] * 2 });
767
$dbi->apply_filter(
768
    'table1', 'key1' => {out => 'twice', in => 'twice'}
769
);
770
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
771
$result = $dbi->select(table => 'table1', where => {key1 => 1});
772
$result->filter({'key2' => 'twice'});
773
$rows   = $result->all;
774
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
775

            
cleanup test
Yuki Kimoto authored on 2011-08-06
776
$dbi = DBIx::Custom->connect(%memory);
777
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
778
$dbi->register_filter(twice => sub { $_[0] * 2 });
779
$dbi->apply_filter(
780
    'table1', 'key1' => {out => 'twice', in => 'twice'}
781
);
782
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
783
$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
784
                        param => {key1 => 1, key2 => 2},
785
                        table => ['table1']);
786
$rows   = $result->all;
787
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
788

            
cleanup test
Yuki Kimoto authored on 2011-08-06
789
$dbi = DBIx::Custom->connect(%memory);
790
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
791
$dbi->register_filter(twice => sub { $_[0] * 2 });
792
$dbi->apply_filter(
793
    'table1', 'key1' => {out => 'twice', in => 'twice'}
794
);
795
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
796
$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
797
                        param => {key1 => 1, key2 => 2});
798
$rows   = $result->all;
799
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
800

            
cleanup test
Yuki Kimoto authored on 2011-08-06
801
$dbi = DBIx::Custom->connect(%memory);
802
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
803
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
804
$dbi->register_filter(twice => sub { $_[0] * 2 });
805
$dbi->register_filter(three_times => sub { $_[0] * 3 });
806
$dbi->apply_filter(
807
    'table1', 'key2' => {out => 'twice', in => 'twice'}
808
);
809
$dbi->apply_filter(
810
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
811
);
812
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
813
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
814
$result = $dbi->select(
815
     table => ['table1', 'table2'],
816
     column => ['key2', 'key3'],
817
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
818

            
819
$result->filter({'key2' => 'twice'});
820
$rows   = $result->all;
821
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
822

            
823
$result = $dbi->select(
824
     table => ['table1', 'table2'],
825
     column => ['key2', 'key3'],
826
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
827

            
828
$result->filter({'key2' => 'twice'});
829
$rows   = $result->all;
830
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
831

            
832
test 'each_column';
cleanup test
Yuki Kimoto authored on 2011-08-06
833
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
834
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
835
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
836

            
837
$infos = [];
838
$dbi->each_column(sub {
839
    my ($self, $table, $column, $cinfo) = @_;
840
    
841
    if ($table =~ /^table/) {
842
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
843
         push @$infos, $info;
844
    }
845
});
846
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
847
is_deeply($infos, 
848
    [
849
        ['table1', 'key1', 'key1'],
850
        ['table1', 'key2', 'key2'],
851
        ['table2', 'key1', 'key1'],
852
        ['table2', 'key3', 'key3']
853
    ]
854
    
855
);
856
test 'each_table';
cleanup test
Yuki Kimoto authored on 2011-08-06
857
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
858
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
859
$dbi->execute('create table table1 (key1 Date, key2 datetime);');
cleanup test
Yuki Kimoto authored on 2011-08-06
860

            
861
$infos = [];
862
$dbi->each_table(sub {
863
    my ($self, $table, $table_info) = @_;
864
    
865
    if ($table =~ /^table/) {
866
         my $info = [$table, $table_info->{TABLE_NAME}];
867
         push @$infos, $info;
868
    }
869
});
870
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
871
is_deeply($infos, 
872
    [
873
        ['table1', 'table1'],
874
        ['table2', 'table2'],
875
    ]
876
);
877

            
878
test 'limit';
cleanup test
Yuki Kimoto authored on 2011-08-06
879
$dbi = DBIx::Custom->connect(%memory);
880
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
881
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
882
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
883
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
884
$dbi->register_tag(
885
    limit => sub {
886
        my ($count, $offset) = @_;
887
        
888
        my $s = '';
889
        $s .= "limit $count";
890
        $s .= " offset $offset" if defined $offset;
891
        
892
        return [$s, []];
893
    }
894
);
895
$rows = $dbi->select(
896
  table => 'table1',
897
  where => {key1 => 1},
898
  append => "order by key2 {limit 1 0}"
899
)->all;
900
is_deeply($rows, [{key1 => 1, key2 => 2}]);
901
$rows = $dbi->select(
902
  table => 'table1',
903
  where => {key1 => 1},
904
  append => "order by key2 {limit 2 1}"
905
)->all;
906
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
907
$rows = $dbi->select(
908
  table => 'table1',
909
  where => {key1 => 1},
910
  append => "order by key2 {limit 1}"
911
)->all;
912
is_deeply($rows, [{key1 => 1, key2 => 2}]);
913

            
914
test 'connect super';
915
{
916
    package MyDBI;
917
    
918
    use base 'DBIx::Custom';
919
    sub connect {
920
        my $self = shift->SUPER::connect(@_);
921
        
922
        return $self;
923
    }
924
    
925
    sub new {
926
        my $self = shift->SUPER::new(@_);
927
        
928
        return $self;
929
    }
930
}
931

            
cleanup test
Yuki Kimoto authored on 2011-08-06
932
$dbi = MyDBI->connect(%memory);
933
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
934
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
935
is($dbi->select(table => 'table1')->one->{key1}, 1);
936

            
cleanup test
Yuki Kimoto authored on 2011-08-06
937
$dbi = MyDBI->new(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
938
$dbi->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
939
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
940
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
941
is($dbi->select(table => 'table1')->one->{key1}, 1);
942

            
943
{
944
    package MyDBI2;
945
    
946
    use base 'DBIx::Custom';
947
    sub connect {
948
        my $self = shift->SUPER::new(@_);
949
        $self->connect;
950
        
951
        return $self;
952
    }
953
}
954

            
cleanup test
Yuki Kimoto authored on 2011-08-06
955
$dbi = MyDBI->connect(%memory);
956
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
957
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
958
is($dbi->select(table => 'table1')->one->{key1}, 1);
959

            
960
test 'end_filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
961
$dbi = DBIx::Custom->connect(%memory);
962
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
963
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
964
$result = $dbi->select(table => 'table1');
965
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
966
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
967
$row = $result->fetch_first;
968
is_deeply($row, [6, 40]);
969

            
cleanup test
Yuki Kimoto authored on 2011-08-06
970
$dbi = DBIx::Custom->connect(%memory);
971
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
972
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
973
$result = $dbi->select(table => 'table1');
974
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
975
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
976
$row = $result->fetch_first;
977
is_deeply($row, [6, 12]);
978

            
cleanup test
Yuki Kimoto authored on 2011-08-06
979
$dbi = DBIx::Custom->connect(%memory);
980
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
981
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
982
$result = $dbi->select(table => 'table1');
983
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
984
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
985
$row = $result->fetch_first;
986
is_deeply($row, [6, 12]);
987

            
988
$dbi->register_filter(five_times => sub { $_[0] * 5 });
989
$result = $dbi->select(table => 'table1');
990
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
991
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
992
$row = $result->one;
993
is_deeply($row, {key1 => 6, key2 => 40});
994

            
995
$dbi->register_filter(five_times => sub { $_[0] * 5 });
996
$dbi->apply_filter('table1',
997
    key1 => {end => sub { $_[0] * 3 } },
998
    key2 => {end => 'five_times'}
999
);
1000
$result = $dbi->select(table => 'table1');
1001
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1002
$row = $result->one;
1003
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
1004

            
1005
$dbi->register_filter(five_times => sub { $_[0] * 5 });
1006
$dbi->apply_filter('table1',
1007
    key1 => {end => sub { $_[0] * 3 } },
1008
    key2 => {end => 'five_times'}
1009
);
1010
$result = $dbi->select(table => 'table1');
1011
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
1012
$result->filter(key1 => undef);
1013
$result->end_filter(key1 => undef);
1014
$row = $result->one;
1015
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
1016

            
1017
test 'remove_end_filter and remove_filter';
cleanup test
Yuki Kimoto authored on 2011-08-06
1018
$dbi = DBIx::Custom->connect(%memory);
1019
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1020
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1021
$result = $dbi->select(table => 'table1');
1022
$row = $result
1023
       ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
1024
       ->remove_filter
1025
       ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
1026
       ->remove_end_filter
1027
       ->fetch_first;
1028
is_deeply($row, [1, 2]);
1029

            
1030
test 'empty where select';
cleanup test
Yuki Kimoto authored on 2011-08-06
1031
$dbi = DBIx::Custom->connect(%memory);
1032
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1033
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1034
$result = $dbi->select(table => 'table1', where => {});
1035
$row = $result->one;
1036
is_deeply($row, {key1 => 1, key2 => 2});
1037

            
1038
test 'select query option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1039
$dbi = DBIx::Custom->connect(%memory);
1040
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1041
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
1042
is(ref $query, 'DBIx::Custom::Query');
1043
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
1044
is(ref $query, 'DBIx::Custom::Query');
1045
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
1046
is(ref $query, 'DBIx::Custom::Query');
1047
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
1048
is(ref $query, 'DBIx::Custom::Query');
1049

            
1050
test 'DBIx::Custom::Where';
cleanup test
Yuki Kimoto authored on 2011-08-06
1051
$dbi = DBIx::Custom->connect(%memory);
1052
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1053
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1054
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1055
$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
1056
is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
1057

            
1058
$where = $dbi->where
1059
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1060
             ->param({key1 => 1});
1061

            
1062
$result = $dbi->select(
1063
    table => 'table1',
1064
    where => $where
1065
);
1066
$row = $result->all;
1067
is_deeply($row, [{key1 => 1, key2 => 2}]);
1068

            
1069
$result = $dbi->select(
1070
    table => 'table1',
1071
    where => [
1072
        ['and', 'key1 = :key1', 'key2 = :key2'],
1073
        {key1 => 1}
1074
    ]
1075
);
1076
$row = $result->all;
1077
is_deeply($row, [{key1 => 1, key2 => 2}]);
1078

            
1079
$where = $dbi->where
1080
             ->clause(['and', 'key1 = :key1', 'key2 = :key2'])
1081
             ->param({key1 => 1, key2 => 2});
1082
$result = $dbi->select(
1083
    table => 'table1',
1084
    where => $where
1085
);
1086
$row = $result->all;
1087
is_deeply($row, [{key1 => 1, key2 => 2}]);
1088

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

            
1099
$where = $dbi->where
1100
             ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2'])
1101
             ->param({key1 => [0, 3], key2 => 2});
1102
$result = $dbi->select(
1103
    table => 'table1',
1104
    where => $where,
1105
); 
1106
$row = $result->all;
1107
is_deeply($row, [{key1 => 1, key2 => 2}]);
1108

            
1109
$where = $dbi->where;
1110
$result = $dbi->select(
1111
    table => 'table1',
1112
    where => $where
1113
);
1114
$row = $result->all;
1115
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1116

            
1117
eval {
1118
$where = $dbi->where
1119
             ->clause(['uuu']);
1120
$result = $dbi->select(
1121
    table => 'table1',
1122
    where => $where
1123
);
1124
};
1125
ok($@);
1126

            
1127
$where = $dbi->where;
1128
is("$where", '');
1129

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

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

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

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

            
1170
$where = $dbi->where
1171
             ->clause('key1 = :key1 key2 = :key2')
1172
             ->param({key1 => 1});
1173
eval{$where->to_string};
1174
like($@, qr/one column/);
1175

            
1176
$where = $dbi->where
1177
             ->clause('key1 = :key1')
1178
             ->param([]);
1179
eval{$where->to_string};
1180
like($@, qr/Parameter/);
1181

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

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

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

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

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

            
1232
$where = $dbi->where
1233
             ->clause(['or', ('key1 = :key1') x 3])
1234
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1235
$result = $dbi->select(
1236
    table => 'table1',
1237
    where => $where,
1238
);
1239
$row = $result->all;
1240
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1241

            
1242
$where = $dbi->where
1243
             ->clause(['or', ('key1 = :key1') x 3])
1244
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1245
$result = $dbi->select(
1246
    table => 'table1',
1247
    where => $where,
1248
);
1249
$row = $result->all;
1250
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1251

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

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

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

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

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

            
1302
$where = $dbi->where
1303
             ->clause(['and', 'key1 is not null', 'key2 is not null' ]);
1304
$result = $dbi->select(
1305
    table => 'table1',
1306
    where => $where,
1307
);
1308
$row = $result->all;
1309
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1310

            
1311
eval {$dbi->where(ppp => 1) };
1312
like($@, qr/invalid/);
1313

            
1314
$where = $dbi->where(
1315
    clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']],
1316
    param => {key1 => 1, key2 => 2}
1317
);
1318
$result = $dbi->select(
1319
    table => 'table1',
1320
    where => $where,
1321
);
1322
$row = $result->all;
1323
is_deeply($row, [{key1 => 1, key2 => 2}]);
1324

            
1325

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

            
1337

            
1338
test 'dbi_option default';
1339
$dbi = DBIx::Custom->new;
1340
is_deeply($dbi->dbi_option, {});
1341

            
1342
test 'register_tag_processor';
cleanup test
Yuki Kimoto authored on 2011-08-06
1343
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1344
$dbi->register_tag_processor(
1345
    a => sub { 1 }
1346
);
1347
is($dbi->query_builder->tag_processors->{a}->(), 1);
1348

            
1349
test 'register_tag';
cleanup test
Yuki Kimoto authored on 2011-08-06
1350
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1351
$dbi->register_tag(
1352
    b => sub { 2 }
1353
);
1354
is($dbi->query_builder->tags->{b}->(), 2);
1355

            
1356
test 'table not specify exception';
cleanup test
Yuki Kimoto authored on 2011-08-06
1357
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1358
eval {$dbi->insert};
1359
like($@, qr/table/);
1360
eval {$dbi->update};
1361
like($@, qr/table/);
1362
eval {$dbi->delete};
1363
like($@, qr/table/);
1364
eval {$dbi->select};
1365
like($@, qr/table/);
1366

            
1367

            
1368
test 'more tests';
cleanup test
Yuki Kimoto authored on 2011-08-06
1369
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1370
eval{$dbi->apply_filter('table', 'column', [])};
1371
like($@, qr/apply_filter/);
1372

            
1373
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1374
like($@, qr/apply_filter/);
1375

            
1376
$dbi->apply_filter(
1377

            
1378
);
cleanup test
Yuki Kimoto authored on 2011-08-06
1379
$dbi = DBIx::Custom->connect(%memory);
1380
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1381
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1382
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1383
$dbi->apply_filter('table1', 'key2', 
1384
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1385
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->all;
1386
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1387

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1388
$dbi = DBIx::Custom->connect(%memory);
1389
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1390
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1391
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1392
$dbi->apply_filter('table1', 'key2', {});
1393
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->all;
1394
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1395

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1396
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1397
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1398
like($@, qr/not registered/);
1399
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1400
like($@, qr/not registered/);
1401
$dbi->method({one => sub { 1 }});
1402
is($dbi->one, 1);
1403

            
1404
eval{DBIx::Custom->connect()};
1405
like($@, qr/_connect/);
1406

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1407
$dbi = DBIx::Custom->connect(%memory);
1408
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1409
$dbi->register_filter(twice => sub { $_[0] * 2 });
1410
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1411
             filter => {key1 => 'twice'});
1412
$row = $dbi->select(table => 'table1')->one;
1413
is_deeply($row, {key1 => 2, key2 => 2});
1414
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1415
             filter => {key1 => 'no'}) };
1416
like($@, qr//);
1417

            
1418
$dbi->register_filter(one => sub { });
1419
$dbi->default_fetch_filter('one');
1420
ok($dbi->default_fetch_filter);
1421
$dbi->default_bind_filter('one');
1422
ok($dbi->default_bind_filter);
1423
eval{$dbi->default_fetch_filter('no')};
1424
like($@, qr/not registered/);
1425
eval{$dbi->default_bind_filter('no')};
1426
like($@, qr/not registered/);
1427
$dbi->default_bind_filter(undef);
1428
ok(!defined $dbi->default_bind_filter);
1429
$dbi->default_fetch_filter(undef);
1430
ok(!defined $dbi->default_fetch_filter);
1431
eval {$dbi->execute('select * from table1 {} {= author') };
1432
like($@, qr/Tag not finished/);
1433

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1434
$dbi = DBIx::Custom->connect(%memory);
1435
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1436
$dbi->register_filter(one => sub { 1 });
1437
$result = $dbi->select(table => 'table1');
1438
eval {$result->filter(key1 => 'no')};
1439
like($@, qr/not registered/);
1440
eval {$result->end_filter(key1 => 'no')};
1441
like($@, qr/not registered/);
1442
$result->default_filter(undef);
1443
ok(!defined $result->default_filter);
1444
$result->default_filter('one');
1445
is($result->default_filter->(), 1);
1446

            
1447
test 'dbi_option';
cleanup test
Yuki Kimoto authored on 2011-08-06
1448
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1449
                             dbi_option => {PrintError => 1});
1450
ok($dbi->dbh->{PrintError});
cleanup test
Yuki Kimoto authored on 2011-08-06
1451
$dbi = DBIx::Custom->connect(%memory,
cleanup test
Yuki Kimoto authored on 2011-08-06
1452
                             dbi_options => {PrintError => 1});
1453
ok($dbi->dbh->{PrintError});
1454

            
1455
test 'DBIx::Custom::Result stash()';
1456
$result = DBIx::Custom::Result->new;
1457
is_deeply($result->stash, {}, 'default');
1458
$result->stash->{foo} = 1;
1459
is($result->stash->{foo}, 1, 'get and set');
1460

            
1461
test 'filter __ expression';
cleanup test
Yuki Kimoto authored on 2011-08-06
1462
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1463
$dbi->execute('create table company (id, name, location_id)');
1464
$dbi->execute('create table location (id, name)');
1465
$dbi->apply_filter('location',
1466
  name => {in => sub { uc $_[0] } }
1467
);
1468

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

            
1472
$result = $dbi->select(
1473
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1474
    column => ['location.name as location__name']
1475
);
1476
is($result->fetch_first->[0], 'B');
1477

            
1478
$result = $dbi->select(
1479
    table => 'company', relation => {'company.location_id' => 'location.id'},
1480
    column => ['location.name as location__name']
1481
);
1482
is($result->fetch_first->[0], 'B');
1483

            
1484
$result = $dbi->select(
1485
    table => 'company', relation => {'company.location_id' => 'location.id'},
1486
    column => ['location.name as "location.name"']
1487
);
1488
is($result->fetch_first->[0], 'B');
1489

            
1490
test 'Model class';
1491
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1492
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1493
$dbi->execute("create table book (title, author)");
1494
$model = $dbi->model('book');
1495
$model->insert({title => 'a', author => 'b'});
1496
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
1497
$dbi->execute("create table company (name)");
1498
$model = $dbi->model('company');
1499
$model->insert({name => 'a'});
1500
is_deeply($model->list->all, [{name => 'a'}], 'basic');
1501
is($dbi->models->{'book'}, $dbi->model('book'));
1502
is($dbi->models->{'company'}, $dbi->model('company'));
1503

            
1504
{
1505
    package MyDBI4;
1506

            
1507
    use strict;
1508
    use warnings;
1509

            
1510
    use base 'DBIx::Custom';
1511

            
1512
    sub connect {
1513
        my $self = shift->SUPER::connect(@_);
1514
        
1515
        $self->include_model(
1516
            MyModel2 => [
1517
                'book',
1518
                {class => 'Company', name => 'company'}
1519
            ]
1520
        );
1521
    }
1522

            
1523
    package MyModel2::Base1;
1524

            
1525
    use strict;
1526
    use warnings;
1527

            
1528
    use base 'DBIx::Custom::Model';
1529

            
1530
    package MyModel2::book;
1531

            
1532
    use strict;
1533
    use warnings;
1534

            
1535
    use base 'MyModel2::Base1';
1536

            
1537
    sub insert {
1538
        my ($self, $param) = @_;
1539
        
1540
        return $self->SUPER::insert(param => $param);
1541
    }
1542

            
1543
    sub list { shift->select; }
1544

            
1545
    package MyModel2::Company;
1546

            
1547
    use strict;
1548
    use warnings;
1549

            
1550
    use base 'MyModel2::Base1';
1551

            
1552
    sub insert {
1553
        my ($self, $param) = @_;
1554
        
1555
        return $self->SUPER::insert(param => $param);
1556
    }
1557

            
1558
    sub list { shift->select; }
1559
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1560
$dbi = MyDBI4->connect(%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

            
1570
{
1571
     package MyDBI5;
1572

            
1573
    use strict;
1574
    use warnings;
1575

            
1576
    use base 'DBIx::Custom';
1577

            
1578
    sub connect {
1579
        my $self = shift->SUPER::connect(@_);
1580
        
1581
        $self->include_model('MyModel4');
1582
    }
1583
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1584
$dbi = MyDBI5->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1585
$dbi->execute("create table company (name)");
1586
$dbi->execute("create table table1 (key1)");
1587
$model = $dbi->model('company');
1588
$model->insert({name => 'a'});
1589
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
1590
$dbi->insert(table => 'table1', param => {key1 => 1});
1591
$model = $dbi->model('book');
1592
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
1593

            
1594
test 'primary_key';
1595
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1596
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1597
$model = $dbi->model('book');
1598
$model->primary_key(['id', 'number']);
1599
is_deeply($model->primary_key, ['id', 'number']);
1600

            
1601
test 'columns';
1602
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1603
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1604
$model = $dbi->model('book');
1605
$model->columns(['id', 'number']);
1606
is_deeply($model->columns, ['id', 'number']);
1607

            
1608
test 'setup_model';
1609
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1610
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1611
$dbi->execute('create table book (id)');
1612
$dbi->execute('create table company (id, name);');
1613
$dbi->execute('create table test (id, name, primary key (id, name));');
1614
$dbi->setup_model;
1615
is_deeply($dbi->model('book')->columns, ['id']);
1616
is_deeply($dbi->model('company')->columns, ['id', 'name']);
1617

            
1618
test 'delete_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1619
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1620
$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
1621
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1622
$dbi->delete_at(
1623
    table => 'table1',
1624
    primary_key => ['key1', 'key2'],
1625
    where => [1, 2],
1626
);
1627
is_deeply($dbi->select(table => 'table1')->all, []);
1628

            
1629
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1630
$dbi->delete_at(
1631
    table => 'table1',
1632
    primary_key => 'key1',
1633
    where => 1,
1634
);
1635
is_deeply($dbi->select(table => 'table1')->all, []);
1636

            
1637
test 'insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1638
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1639
$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
1640
$dbi->insert_at(
1641
    primary_key => ['key1', 'key2'], 
1642
    table => 'table1',
1643
    where => [1, 2],
1644
    param => {key3 => 3}
1645
);
1646
is($dbi->select(table => 'table1')->one->{key1}, 1);
1647
is($dbi->select(table => 'table1')->one->{key2}, 2);
1648
is($dbi->select(table => 'table1')->one->{key3}, 3);
1649

            
1650
$dbi->delete_all(table => 'table1');
1651
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1652
$dbi->insert_at(
1653
    primary_key => 'key1', 
1654
    table => 'table1',
1655
    where => 1,
1656
    param => {key2 => 2, key3 => 3}
1657
);
1658

            
1659
is($dbi->select(table => 'table1')->one->{key1}, 1);
1660
is($dbi->select(table => 'table1')->one->{key2}, 2);
1661
is($dbi->select(table => 'table1')->one->{key3}, 3);
1662

            
1663
eval {
1664
    $dbi->insert_at(
1665
        table => 'table1',
1666
        primary_key => ['key1', 'key2'],
1667
        where => {},
1668
        param => {key1 => 1, key2 => 2, key3 => 3},
1669
    );
1670
};
1671
like($@, qr/must be/);
1672

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1673
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1674
$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
1675
$dbi->insert_at(
1676
    {key3 => 3},
1677
    primary_key => ['key1', 'key2'], 
1678
    table => 'table1',
1679
    where => [1, 2],
1680
);
1681
is($dbi->select(table => 'table1')->one->{key1}, 1);
1682
is($dbi->select(table => 'table1')->one->{key2}, 2);
1683
is($dbi->select(table => 'table1')->one->{key3}, 3);
1684

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

            
1699
$dbi->delete_all(table => 'table1');
1700
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1701
$dbi->update_at(
1702
    table => 'table1',
1703
    primary_key => 'key1',
1704
    where => 1,
1705
    param => {key3 => 4}
1706
);
1707
is($dbi->select(table => 'table1')->one->{key1}, 1);
1708
is($dbi->select(table => 'table1')->one->{key2}, 2);
1709
is($dbi->select(table => 'table1')->one->{key3}, 4);
1710

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1711
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1712
$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
1713
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1714
$dbi->update_at(
1715
    {key3 => 4},
1716
    table => 'table1',
1717
    primary_key => ['key1', 'key2'],
1718
    where => [1, 2]
1719
);
1720
is($dbi->select(table => 'table1')->one->{key1}, 1);
1721
is($dbi->select(table => 'table1')->one->{key2}, 2);
1722
is($dbi->select(table => 'table1')->one->{key3}, 4);
1723

            
1724
test 'select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1725
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1726
$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
1727
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1728
$result = $dbi->select_at(
1729
    table => 'table1',
1730
    primary_key => ['key1', 'key2'],
1731
    where => [1, 2]
1732
);
1733
$row = $result->one;
1734
is($row->{key1}, 1);
1735
is($row->{key2}, 2);
1736
is($row->{key3}, 3);
1737

            
1738
$dbi->delete_all(table => 'table1');
1739
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1740
$result = $dbi->select_at(
1741
    table => 'table1',
1742
    primary_key => 'key1',
1743
    where => 1,
1744
);
1745
$row = $result->one;
1746
is($row->{key1}, 1);
1747
is($row->{key2}, 2);
1748
is($row->{key3}, 3);
1749

            
1750
$dbi->delete_all(table => 'table1');
1751
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1752
$result = $dbi->select_at(
1753
    table => 'table1',
1754
    primary_key => ['key1', 'key2'],
1755
    where => [1, 2]
1756
);
1757
$row = $result->one;
1758
is($row->{key1}, 1);
1759
is($row->{key2}, 2);
1760
is($row->{key3}, 3);
1761

            
1762
eval {
1763
    $result = $dbi->select_at(
1764
        table => 'table1',
1765
        primary_key => ['key1', 'key2'],
1766
        where => {},
1767
    );
1768
};
1769
like($@, qr/must be/);
1770

            
1771
eval {
1772
    $result = $dbi->select_at(
1773
        table => 'table1',
1774
        primary_key => ['key1', 'key2'],
1775
        where => [1],
1776
    );
1777
};
1778
like($@, qr/same/);
1779

            
1780
eval {
1781
    $result = $dbi->update_at(
1782
        table => 'table1',
1783
        primary_key => ['key1', 'key2'],
1784
        where => {},
1785
        param => {key1 => 1, key2 => 2},
1786
    );
1787
};
1788
like($@, qr/must be/);
1789

            
1790
eval {
1791
    $result = $dbi->delete_at(
1792
        table => 'table1',
1793
        primary_key => ['key1', 'key2'],
1794
        where => {},
1795
    );
1796
};
1797
like($@, qr/must be/);
1798

            
1799
test 'columns';
1800
use MyDBI1;
cleanup test
Yuki Kimoto authored on 2011-08-06
1801
$dbi = MyDBI1->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1802
$model = $dbi->model('book');
1803

            
1804

            
1805
test 'model delete_at';
1806
{
1807
    package MyDBI6;
1808
    
1809
    use base 'DBIx::Custom';
1810
    
1811
    sub connect {
1812
        my $self = shift->SUPER::connect(@_);
1813
        
1814
        $self->include_model('MyModel5');
1815
        
1816
        return $self;
1817
    }
1818
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1819
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1820
$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
1821
$dbi->execute("create table table2 (key1, key2, key3)");
1822
$dbi->execute("create table table3 (key1, key2, key3)");
1823
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1824
$dbi->model('table1')->delete_at(where => [1, 2]);
1825
is_deeply($dbi->select(table => 'table1')->all, []);
1826
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
1827
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1828
is_deeply($dbi->select(table => 'table1')->all, []);
1829
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
1830
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1831
is_deeply($dbi->select(table => 'table1')->all, []);
1832

            
1833
test 'model insert_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1834
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1835
$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
1836
$dbi->model('table1')->insert_at(
1837
    where => [1, 2],
1838
    param => {key3 => 3}
1839
);
1840
$result = $dbi->model('table1')->select;
1841
$row = $result->one;
1842
is($row->{key1}, 1);
1843
is($row->{key2}, 2);
1844
is($row->{key3}, 3);
1845

            
1846
test 'model update_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1847
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1848
$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
1849
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1850
$dbi->model('table1')->update_at(
1851
    where => [1, 2],
1852
    param => {key3 => 4}
1853
);
1854
$result = $dbi->model('table1')->select;
1855
$row = $result->one;
1856
is($row->{key1}, 1);
1857
is($row->{key2}, 2);
1858
is($row->{key3}, 4);
1859

            
1860
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
1861
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1862
$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
1863
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1864
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1865
$row = $result->one;
1866
is($row->{key1}, 1);
1867
is($row->{key2}, 2);
1868
is($row->{key3}, 3);
1869

            
1870

            
1871
test 'mycolumn and column';
1872
{
1873
    package MyDBI7;
1874
    
1875
    use base 'DBIx::Custom';
1876
    
1877
    sub connect {
1878
        my $self = shift->SUPER::connect(@_);
1879
        
1880
        $self->include_model('MyModel6');
1881
        
1882
        
1883
        return $self;
1884
    }
1885
}
cleanup test
Yuki Kimoto authored on 2011-08-06
1886
$dbi = MyDBI7->connect(%memory);
1887
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
1888
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
1889
$dbi->separator('__');
1890
$dbi->setup_model;
1891
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1892
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1893
$model = $dbi->model('table1');
1894
$result = $model->select(
1895
    column => [$model->mycolumn, $model->column('table2')],
1896
    where => {'table1.key1' => 1}
1897
);
1898
is_deeply($result->one,
1899
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1900

            
1901
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1902
$dbi = DBIx::Custom->connect(%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->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1905
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1906

            
1907
$param = {key2 => 11};
1908
$update_param = $dbi->update_param($param);
1909
$sql = <<"EOS";
1910
update table1 $update_param
1911
where key1 = 1
1912
EOS
1913
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1914
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1915
$rows   = $result->all;
1916
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1917
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1918
                  "basic");
1919

            
1920

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1921
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1922
$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
1923
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1924
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1925

            
1926
$param = {key2 => 11, key3 => 33};
1927
$update_param = $dbi->update_param($param);
1928
$sql = <<"EOS";
1929
update table1 $update_param
1930
where key1 = 1
1931
EOS
1932
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1933
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1934
$rows   = $result->all;
1935
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1936
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1937
                  "basic");
1938

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1939
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1940
$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
1941
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1942
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1943

            
1944
$param = {key2 => 11, key3 => 33};
1945
$update_param = $dbi->update_param($param, {no_set => 1});
1946
$sql = <<"EOS";
1947
update table1 set $update_param
1948
where key1 = 1
1949
EOS
1950
$dbi->execute($sql, param => $param);
test cleanup
Yuki Kimoto authored on 2011-08-06
1951
$result = $dbi->execute('select * from table1;', table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
1952
$rows   = $result->all;
1953
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5},
1954
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1955
                  "update param no_set");
1956

            
1957
            
1958
eval { $dbi->update_param({";" => 1}) };
1959
like($@, qr/not safety/);
1960

            
1961

            
1962
test 'update_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1963
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1964
$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
1965
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
1966
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
1967

            
1968
$param = {key2 => 11};
1969
$update_param = $dbi->assign_param($param);
1970
$sql = <<"EOS";
1971
update table1 set $update_param
1972
where key1 = 1
1973
EOS
1974
$dbi->execute($sql, param => $param, table => 'table1');
test cleanup
Yuki Kimoto authored on 2011-08-06
1975
$result = $dbi->execute('select * from table1;');
cleanup test
Yuki Kimoto authored on 2011-08-06
1976
$rows   = $result->all;
1977
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
1978
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
1979
                  "basic");
1980

            
1981

            
1982
test 'insert_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
1983
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1984
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
1985
$param = {key1 => 1, key2 => 2};
1986
$insert_param = $dbi->insert_param($param);
1987
$sql = <<"EOS";
1988
insert into table1 $insert_param
1989
EOS
1990
$dbi->execute($sql, param => $param, table => 'table1');
1991
is($dbi->select(table => 'table1')->one->{key1}, 1);
1992
is($dbi->select(table => 'table1')->one->{key2}, 2);
1993

            
cleanup test
Yuki Kimoto authored on 2011-08-06
1994
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
1995
$dbi->quote('"');
1996
$dbi->execute('create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));');
1997
$param = {key1 => 1, key2 => 2};
1998
$insert_param = $dbi->insert_param($param);
1999
$sql = <<"EOS";
2000
insert into table1 $insert_param
2001
EOS
2002
$dbi->execute($sql, param => $param, table => 'table1');
2003
is($dbi->select(table => 'table1')->one->{key1}, 1);
2004
is($dbi->select(table => 'table1')->one->{key2}, 2);
2005

            
2006
eval { $dbi->insert_param({";" => 1}) };
2007
like($@, qr/not safety/);
2008

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

            
2010
test 'join';
cleanup test
Yuki Kimoto authored on 2011-08-06
2011
$dbi = DBIx::Custom->connect(%memory);
2012
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2013
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2014
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
cleanup test
Yuki Kimoto authored on 2011-08-06
2015
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2016
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
cleanup test
Yuki Kimoto authored on 2011-08-06
2017
$dbi->execute('create table table3 (key3 int, key4 int);');
cleanup test
Yuki Kimoto authored on 2011-08-06
2018
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4});
2019
$rows = $dbi->select(
2020
    table => 'table1',
2021
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2022
    where   => {'table1.key2' => 2},
2023
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2024
)->all;
2025
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]);
2026

            
2027
$rows = $dbi->select(
2028
    table => 'table1',
2029
    where   => {'key1' => 1},
2030
    join  => ['left outer join table2 on table1.key1 = table2.key1']
2031
)->all;
2032
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2033

            
2034
eval {
2035
    $rows = $dbi->select(
2036
        table => 'table1',
2037
        column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
2038
        where   => {'table1.key2' => 2},
2039
        join  => {'table1.key1' => 'table2.key1'}
2040
    );
2041
};
2042
like ($@, qr/array/);
2043

            
2044
$rows = $dbi->select(
2045
    table => 'table1',
2046
    where   => {'key1' => 1},
2047
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2048
              'left outer join table3 on table2.key3 = table3.key3']
2049
)->all;
2050
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2051

            
2052
$rows = $dbi->select(
2053
    column => 'table3.key4 as table3__key4',
2054
    table => 'table1',
2055
    where   => {'table1.key1' => 1},
2056
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2057
              'left outer join table3 on table2.key3 = table3.key3']
2058
)->all;
2059
is_deeply($rows, [{table3__key4 => 4}]);
2060

            
2061
$rows = $dbi->select(
2062
    column => 'table1.key1 as table1__key1',
2063
    table => 'table1',
2064
    where   => {'table3.key4' => 4},
2065
    join  => ['left outer join table2 on table1.key1 = table2.key1',
2066
              'left outer join table3 on table2.key3 = table3.key3']
2067
)->all;
2068
is_deeply($rows, [{table1__key1 => 1}]);
2069

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2070
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2071
$dbi->quote('"');
cleanup test
Yuki Kimoto authored on 2011-08-06
2072
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2073
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup test
Yuki Kimoto authored on 2011-08-06
2074
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2075
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2076
$rows = $dbi->select(
2077
    table => 'table1',
2078
    column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"',
2079
    where   => {'table1.key2' => 2},
2080
    join  => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'],
2081
)->all;
2082
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}],
2083
          'quote');
2084

            
2085
{
2086
    package MyDBI8;
2087
    
2088
    use base 'DBIx::Custom';
2089
    
2090
    sub connect {
2091
        my $self = shift->SUPER::connect(@_);
2092
        
2093
        $self->include_model('MyModel7');
2094
        
2095
        return $self;
2096
    }
2097
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2098

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2099
$dbi = DBIx::Custom->connect(%memory);
2100
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2101
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2102
$sql = <<"EOS";
cleanup test
Yuki Kimoto authored on 2011-08-06
2103
left outer join (
2104
  select * from table1 as t1
2105
  where t1.key2 = (
2106
    select max(t2.key2) from table1 as t2
2107
    where t1.key1 = t2.key1
2108
  )
2109
) as latest_table1 on table1.key1 = latest_table1.key1
2110
EOS
cleanup test
Yuki Kimoto authored on 2011-08-06
2111
$join = [$sql];
2112
$rows = $dbi->select(
2113
    table => 'table1',
2114
    column => 'latest_table1.key1 as latest_table1__key1',
2115
    join  => $join
2116
)->all;
2117
is_deeply($rows, [{latest_table1__key1 => 1}]);
2118

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2119
$dbi = DBIx::Custom->connect(%memory);
2120
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2121
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2122
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2123
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2124
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2125
$result = $dbi->select(
2126
    table => 'table1',
2127
    join => [
2128
        "left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1"
2129
    ]
2130
);
2131
is_deeply($result->all, [{key1 => 1, key2 => 2}]);
2132
$result = $dbi->select(
2133
    table => 'table1',
2134
    column => [{table2 => ['key3']}],
2135
    join => [
2136
        "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1"
2137
    ]
2138
);
2139
is_deeply($result->all, [{'table2.key3' => 4}]);
2140
$result = $dbi->select(
2141
    table => 'table1',
2142
    column => [{table2 => ['key3']}],
2143
    join => [
2144
        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2145
    ]
2146
);
2147
is_deeply($result->all, [{'table2.key3' => 4}]);
2148

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2149
$dbi = DBIx::Custom->connect(%memory);
2150
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2151
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2152
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2153
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2154
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2155
$result = $dbi->select(
2156
    table => 'table1',
2157
    column => [{table2 => ['key3']}],
2158
    join => [
2159
        {
2160
            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
2161
            table => ['table1', 'table2']
2162
        }
2163
    ]
2164
);
2165
is_deeply($result->all, [{'table2.key3' => 4}]);
2166

            
2167
test 'mycolumn';
cleanup test
Yuki Kimoto authored on 2011-08-06
2168
$dbi = MyDBI8->connect(%memory);
2169
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2170
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2171
$dbi->setup_model;
2172
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2173
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2174
$model = $dbi->model('table1');
2175
$result = $model->select_at(
2176
    column => [
2177
        $model->mycolumn,
2178
        $model->column('table2')
2179
    ]
2180
);
2181
is_deeply($result->one,
2182
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2183

            
2184
$result = $model->select_at(
2185
    column => [
2186
        $model->mycolumn(['key1']),
2187
        $model->column(table2 => ['key1'])
2188
    ]
2189
);
2190
is_deeply($result->one,
2191
          {key1 => 1, 'table2.key1' => 1});
2192
$result = $model->select_at(
2193
    column => [
2194
        $model->mycolumn(['key1']),
2195
        {table2 => ['key1']}
2196
    ]
2197
);
2198
is_deeply($result->one,
2199
          {key1 => 1, 'table2.key1' => 1});
2200

            
2201
$result = $model->select_at(
2202
    column => [
2203
        $model->mycolumn(['key1']),
2204
        ['table2.key1', as => 'table2.key1']
2205
    ]
2206
);
2207
is_deeply($result->one,
2208
          {key1 => 1, 'table2.key1' => 1});
2209

            
2210
$result = $model->select_at(
2211
    column => [
2212
        $model->mycolumn(['key1']),
2213
        ['table2.key1' => 'table2.key1']
2214
    ]
2215
);
2216
is_deeply($result->one,
2217
          {key1 => 1, 'table2.key1' => 1});
2218

            
2219
test 'dbi method from model';
2220
{
2221
    package MyDBI9;
2222
    
2223
    use base 'DBIx::Custom';
2224
    
2225
    sub connect {
2226
        my $self = shift->SUPER::connect(@_);
2227
        
2228
        $self->include_model('MyModel8')->setup_model;
2229
        
2230
        return $self;
2231
    }
2232
}
cleanup test
Yuki Kimoto authored on 2011-08-06
2233
$dbi = MyDBI9->connect(%memory);
2234
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2235
$model = $dbi->model('table1');
2236
eval{$model->execute('select * from table1')};
2237
ok(!$@);
2238

            
2239
test 'column table option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2240
$dbi = MyDBI9->connect(%memory);
2241
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2242
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2243
$dbi->setup_model;
2244
$dbi->execute('insert into table1 (key1, key2) values (1, 2);');
2245
$dbi->execute('insert into table2 (key1, key3) values (1, 4);');
2246
$model = $dbi->model('table1');
2247
$result = $model->select(
2248
    column => [
2249
        $model->column('table2', {alias => 'table2_alias'})
2250
    ],
2251
    where => {'table2_alias.key3' => 4}
2252
);
2253
is_deeply($result->one, 
2254
          {'table2_alias.key1' => 1, 'table2_alias.key3' => 4});
2255

            
2256
$dbi->separator('__');
2257
$result = $model->select(
2258
    column => [
2259
        $model->column('table2', {alias => 'table2_alias'})
2260
    ],
2261
    where => {'table2_alias.key3' => 4}
2262
);
2263
is_deeply($result->one, 
2264
          {'table2_alias__key1' => 1, 'table2_alias__key3' => 4});
2265

            
2266
$dbi->separator('-');
2267
$result = $model->select(
2268
    column => [
2269
        $model->column('table2', {alias => 'table2_alias'})
2270
    ],
2271
    where => {'table2_alias.key3' => 4}
2272
);
2273
is_deeply($result->one, 
2274
          {'table2_alias-key1' => 1, 'table2_alias-key3' => 4});
2275

            
2276
test 'type option'; # DEPRECATED!
2277
$dbi = DBIx::Custom->connect(
2278
    data_source => 'dbi:SQLite:dbname=:memory:',
2279
    dbi_option => {
2280
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2281
    }
2282
);
2283
my $binary = pack("I3", 1, 2, 3);
2284
$dbi->execute('create table table1(key1, key2)');
2285
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
2286
$result = $dbi->select(table => 'table1');
2287
$row   = $result->one;
2288
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2289
$result = $dbi->execute('select length(key1) as key1_length from table1');
2290
$row = $result->one;
2291
is($row->{key1_length}, length $binary);
2292

            
2293
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]);
2294
$result = $dbi->select(table => 'table1');
2295
$row   = $result->one;
2296
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2297
$result = $dbi->execute('select length(key1) as key1_length from table1');
2298
$row = $result->one;
2299
is($row->{key1_length}, length $binary);
2300

            
2301

            
2302
test 'bind_type option';
2303
$dbi = DBIx::Custom->connect(
2304
    data_source => 'dbi:SQLite:dbname=:memory:',
2305
    dbi_option => {
2306
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2307
    }
2308
);
2309
$binary = pack("I3", 1, 2, 3);
2310
$dbi->execute('create table table1(key1, key2)');
2311
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]);
2312
$result = $dbi->select(table => 'table1');
2313
$row   = $result->one;
2314
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2315
$result = $dbi->execute('select length(key1) as key1_length from table1');
2316
$row = $result->one;
2317
is($row->{key1_length}, length $binary);
2318

            
2319
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]);
2320
$result = $dbi->select(table => 'table1');
2321
$row   = $result->one;
2322
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2323
$result = $dbi->execute('select length(key1) as key1_length from table1');
2324
$row = $result->one;
2325
is($row->{key1_length}, length $binary);
2326

            
2327
test 'model type attribute';
2328
$dbi = DBIx::Custom->connect(
2329
    data_source => 'dbi:SQLite:dbname=:memory:',
2330
    dbi_option => {
2331
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
2332
    }
2333
);
2334
$binary = pack("I3", 1, 2, 3);
2335
$dbi->execute('create table table1(key1, key2)');
2336
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
2337
$model->insert(param => {key1 => $binary, key2 => 'あ'});
2338
$result = $dbi->select(table => 'table1');
2339
$row   = $result->one;
2340
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
2341
$result = $dbi->execute('select length(key1) as key1_length from table1');
2342
$row = $result->one;
2343
is($row->{key1_length}, length $binary);
2344

            
2345
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2346
$dbi = DBIx::Custom->connect(%memory);
2347
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2348
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2349

            
2350
$dbi->create_model(
2351
    table => 'table1',
2352
    join => [
2353
       'left outer join table2 on table1.key1 = table2.key1'
2354
    ],
2355
    primary_key => ['key1']
2356
);
2357
$model2 = $dbi->create_model(
2358
    table => 'table2'
2359
);
2360
$dbi->create_model(
2361
    table => 'table3',
2362
    filter => [
2363
        key1 => {in => sub { uc $_[0] }}
2364
    ]
2365
);
2366
$dbi->setup_model;
2367
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2368
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2369
$model = $dbi->model('table1');
2370
$result = $model->select(
2371
    column => [$model->mycolumn, $model->column('table2')],
2372
    where => {'table1.key1' => 1}
2373
);
2374
is_deeply($result->one,
2375
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
2376
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
2377

            
2378
test 'model method';
2379
test 'create_model';
cleanup test
Yuki Kimoto authored on 2011-08-06
2380
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2381
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2382
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2383
$model = $dbi->create_model(
2384
    table => 'table2'
2385
);
2386
$model->method(foo => sub { shift->select(@_) });
2387
is_deeply($model->foo->one, {key1 => 1, key3 => 3});
2388

            
2389
test 'merge_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
2390
$dbi = DBIx::Custom->new;
2391
$params = [
2392
    {key1 => 1, key2 => 2, key3 => 3},
2393
    {key1 => 1, key2 => 2},
2394
    {key1 => 1}
2395
];
2396
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]);
2397
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3});
2398

            
2399
$params = [
2400
    {key1 => [1, 2], key2 => 1, key3 => [1, 2]},
2401
    {key1 => [3, 4], key2 => [2, 3], key3 => 3}
2402
];
2403
$param = $dbi->merge_param($params->[0], $params->[1]);
2404
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]});
cleanup test
Yuki Kimoto authored on 2011-08-06
2405

            
2406
test 'select() param option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2407
$dbi = DBIx::Custom->connect(%memory);
2408
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2409
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2410
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
cleanup test
Yuki Kimoto authored on 2011-08-06
2411
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2412
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2413
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2414
$rows = $dbi->select(
2415
    table => 'table1',
2416
    column => 'table1.key1 as table1_key1, key2, key3',
2417
    where   => {'table1.key2' => 3},
2418
    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2419
              ' as table2 on table1.key1 = table2.key1'],
2420
    param => {'table2.key3' => 5}
2421
)->all;
2422
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2423

            
2424

            
2425
test 'select() wrap option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2426
$dbi = DBIx::Custom->connect(%memory);
2427
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2428
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2429
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2430
$rows = $dbi->select(
2431
    table => 'table1',
2432
    column => 'key1',
2433
    wrap => ['select * from (', ') as t where key1 = 1']
2434
)->all;
2435
is_deeply($rows, [{key1 => 1}]);
2436

            
2437
eval {
2438
$dbi->select(
2439
    table => 'table1',
2440
    column => 'key1',
2441
    wrap => 'select * from ('
2442
)
2443
};
2444
like($@, qr/array/);
2445

            
2446
test 'select() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2447
$dbi = DBIx::Custom->connect(%memory);
2448
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2449
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2450
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2451
$rows = $dbi->select(
2452
    table => 'table1',
2453
    where => 'key1 = :key1 and key2 = :key2',
2454
    where_param => {key1 => 1, key2 => 2}
2455
)->all;
2456
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2457

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2458
$dbi = DBIx::Custom->connect(%memory);
2459
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2460
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2461
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2462
$rows = $dbi->select(
2463
    table => 'table1',
2464
    where => [
2465
        'key1 = :key1 and key2 = :key2',
2466
        {key1 => 1, key2 => 2}
2467
    ]
2468
)->all;
2469
is_deeply($rows, [{key1 => 1, key2 => 2}]);
2470

            
2471
test 'delete() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2472
$dbi = DBIx::Custom->connect(%memory);
2473
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2474
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2475
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2476
$dbi->delete(
2477
    table => 'table1',
2478
    where => 'key1 = :key1 and key2 = :key2',
2479
    where_param => {key1 => 1, key2 => 2}
2480
);
2481
$rows = $dbi->select(table => 'table1')->all;
2482
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2483

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2484
$dbi = DBIx::Custom->connect(%memory);
2485
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2486
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2487
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2488
$dbi->delete(
2489
    table => 'table1',
2490
    where => [
2491
        'key1 = :key1 and key2 = :key2',
2492
         {key1 => 1, key2 => 2}
2493
    ]
2494
);
2495
$rows = $dbi->select(table => 'table1')->all;
2496
is_deeply($rows, [{key1 => 2, key2 => 3}]);
2497

            
2498

            
2499
test 'update() string where';
cleanup test
Yuki Kimoto authored on 2011-08-06
2500
$dbi = DBIx::Custom->connect(%memory);
2501
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2502
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2503
$dbi->update(
2504
    table => 'table1',
2505
    param => {key1 => 5},
2506
    where => 'key1 = :key1 and key2 = :key2',
2507
    where_param => {key1 => 1, key2 => 2}
2508
);
2509
$rows = $dbi->select(table => 'table1')->all;
2510
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2511

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2512
$dbi = DBIx::Custom->connect(%memory);
2513
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2514
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2515
$dbi->update(
2516
    table => 'table1',
2517
    param => {key1 => 5},
2518
    where => [
2519
        'key1 = :key1 and key2 = :key2',
2520
        {key1 => 1, key2 => 2}
2521
    ]
2522
);
2523
$rows = $dbi->select(table => 'table1')->all;
2524
is_deeply($rows, [{key1 => 5, key2 => 2}]);
2525

            
2526
test 'insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2527
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2528
$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
2529
$dbi->insert(
2530
    primary_key => ['key1', 'key2'], 
2531
    table => 'table1',
2532
    id => [1, 2],
2533
    param => {key3 => 3}
2534
);
2535
is($dbi->select(table => 'table1')->one->{key1}, 1);
2536
is($dbi->select(table => 'table1')->one->{key2}, 2);
2537
is($dbi->select(table => 'table1')->one->{key3}, 3);
2538

            
2539
$dbi->delete_all(table => 'table1');
2540
$dbi->insert(
2541
    primary_key => 'key1', 
2542
    table => 'table1',
2543
    id => 0,
2544
    param => {key2 => 2, key3 => 3}
2545
);
2546

            
2547
is($dbi->select(table => 'table1')->one->{key1}, 0);
2548
is($dbi->select(table => 'table1')->one->{key2}, 2);
2549
is($dbi->select(table => 'table1')->one->{key3}, 3);
2550

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2551
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2552
$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
2553
$dbi->insert(
2554
    {key3 => 3},
2555
    primary_key => ['key1', 'key2'], 
2556
    table => 'table1',
2557
    id => [1, 2],
2558
);
2559
is($dbi->select(table => 'table1')->one->{key1}, 1);
2560
is($dbi->select(table => 'table1')->one->{key2}, 2);
2561
is($dbi->select(table => 'table1')->one->{key3}, 3);
2562

            
2563

            
2564
test 'model insert id and primary_key option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2565
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2566
$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
2567
$dbi->model('table1')->insert(
2568
    id => [1, 2],
2569
    param => {key3 => 3}
2570
);
2571
$result = $dbi->model('table1')->select;
2572
$row = $result->one;
2573
is($row->{key1}, 1);
2574
is($row->{key2}, 2);
2575
is($row->{key3}, 3);
2576

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2577
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2578
$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
2579
$dbi->model('table1')->insert(
2580
    {key3 => 3},
2581
    id => [1, 2]
2582
);
2583
$result = $dbi->model('table1')->select;
2584
$row = $result->one;
2585
is($row->{key1}, 1);
2586
is($row->{key2}, 2);
2587
is($row->{key3}, 3);
2588

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

            
2603
$dbi->delete_all(table => 'table1');
2604
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2605
$dbi->update(
2606
    table => 'table1',
2607
    primary_key => 'key1',
2608
    id => 0,
2609
    param => {key3 => 4}
2610
);
2611
is($dbi->select(table => 'table1')->one->{key1}, 0);
2612
is($dbi->select(table => 'table1')->one->{key2}, 2);
2613
is($dbi->select(table => 'table1')->one->{key3}, 4);
2614

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

            
2628

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

            
2643

            
2644
test 'delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2645
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2646
$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
2647
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2648
$dbi->delete(
2649
    table => 'table1',
2650
    primary_key => ['key1', 'key2'],
2651
    id => [1, 2],
2652
);
2653
is_deeply($dbi->select(table => 'table1')->all, []);
2654

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

            
2663

            
2664
test 'model delete and id option';
cleanup test
Yuki Kimoto authored on 2011-08-06
2665
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2666
$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
2667
$dbi->execute("create table table2 (key1, key2, key3)");
2668
$dbi->execute("create table table3 (key1, key2, key3)");
2669
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2670
$dbi->model('table1')->delete(id => [1, 2]);
2671
is_deeply($dbi->select(table => 'table1')->all, []);
2672
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2673
$dbi->model('table1_1')->delete(id => [1, 2]);
2674
is_deeply($dbi->select(table => 'table1')->all, []);
2675
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2676
$dbi->model('table1_3')->delete(id => [1, 2]);
2677
is_deeply($dbi->select(table => 'table1')->all, []);
2678

            
2679

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

            
2694
$dbi->delete_all(table => 'table1');
2695
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3});
2696
$result = $dbi->select(
2697
    table => 'table1',
2698
    primary_key => 'key1',
2699
    id => 0,
2700
);
2701
$row = $result->one;
2702
is($row->{key1}, 0);
2703
is($row->{key2}, 2);
2704
is($row->{key3}, 3);
2705

            
2706
$dbi->delete_all(table => 'table1');
2707
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2708
$result = $dbi->select(
2709
    table => 'table1',
2710
    primary_key => ['key1', 'key2'],
2711
    id => [1, 2]
2712
);
2713
$row = $result->one;
2714
is($row->{key1}, 1);
2715
is($row->{key2}, 2);
2716
is($row->{key3}, 3);
2717

            
2718

            
2719
test 'model select_at';
cleanup test
Yuki Kimoto authored on 2011-08-06
2720
$dbi = MyDBI6->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2721
$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
2722
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2723
$result = $dbi->model('table1')->select(id => [1, 2]);
2724
$row = $result->one;
2725
is($row->{key1}, 1);
2726
is($row->{key2}, 2);
2727
is($row->{key3}, 3);
2728

            
2729
test 'column separator is default .';
cleanup test
Yuki Kimoto authored on 2011-08-06
2730
$dbi = MyDBI7->connect(%memory);
2731
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
2732
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
2733
$dbi->setup_model;
2734
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2735
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2736
$model = $dbi->model('table1');
2737
$result = $model->select(
2738
    column => [$model->column('table2')],
2739
    where => {'table1.key1' => 1}
2740
);
2741
is_deeply($result->one,
2742
          {'table2.key1' => 1, 'table2.key3' => 3});
2743

            
2744
$result = $model->select(
2745
    column => [$model->column('table2' => [qw/key1 key3/])],
2746
    where => {'table1.key1' => 1}
2747
);
2748
is_deeply($result->one,
2749
          {'table2.key1' => 1, 'table2.key3' => 3});
2750

            
2751

            
2752
test 'type_rule from';
cleanup test
Yuki Kimoto authored on 2011-08-06
2753
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2754
$dbi->type_rule(
2755
    from1 => {
2756
        date => sub { uc $_[0] }
2757
    }
2758
);
2759
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2760
$dbi->insert({key1 => 'a'}, table => 'table1');
2761
$result = $dbi->select(table => 'table1');
2762
is($result->fetch_first->[0], 'A');
2763

            
2764
$result = $dbi->select(table => 'table1');
2765
is($result->one->{key1}, 'A');
2766

            
2767

            
2768
test 'type_rule into';
cleanup test
Yuki Kimoto authored on 2011-08-06
2769
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2770
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2771
$dbi->type_rule(
2772
    into1 => {
2773
        date => sub { uc $_[0] }
2774
    }
2775
);
2776
$dbi->insert({key1 => 'a'}, table => 'table1');
2777
$result = $dbi->select(table => 'table1');
2778
is($result->one->{key1}, 'A');
2779

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2780
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2781
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2782
$dbi->type_rule(
2783
    into1 => [
2784
         [qw/date datetime/] => sub { uc $_[0] }
2785
    ]
2786
);
2787
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
2788
$result = $dbi->select(table => 'table1');
2789
$row = $result->one;
2790
is($row->{key1}, 'A');
2791
is($row->{key2}, 'B');
2792

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2793
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2794
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2795
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
2796
$dbi->type_rule(
2797
    into1 => [
2798
        [qw/date datetime/] => sub { uc $_[0] }
2799
    ]
2800
);
2801
$result = $dbi->execute(
2802
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2803
    param => {key1 => 'a', 'table1.key2' => 'b'}
2804
);
2805
$row = $result->one;
2806
is($row->{key1}, 'a');
2807
is($row->{key2}, 'B');
2808

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2809
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2810
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2811
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
2812
$dbi->type_rule(
2813
    into1 => [
2814
        [qw/date datetime/] => sub { uc $_[0] }
2815
    ]
2816
);
2817
$result = $dbi->execute(
2818
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
2819
    param => {key1 => 'a', 'table1.key2' => 'b'},
2820
    table => 'table1'
2821
);
2822
$row = $result->one;
2823
is($row->{key1}, 'A');
2824
is($row->{key2}, 'B');
2825

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2826
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2827
$dbi->execute("create table table1 (key1 date, key2 datetime)");
2828
$dbi->register_filter(twice => sub { $_[0] * 2 });
2829
$dbi->type_rule(
2830
    from1 => {
2831
        date => 'twice',
2832
    },
2833
    into1 => {
2834
        date => 'twice',
2835
    }
2836
);
2837
$dbi->insert({key1 => 2}, table => 'table1');
2838
$result = $dbi->select(table => 'table1');
2839
is($result->fetch->[0], 8);
2840

            
2841
test 'type_rule and filter order';
cleanup test
Yuki Kimoto authored on 2011-08-06
2842
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2843
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2844
$dbi->type_rule(
2845
    into1 => {
2846
        date => sub { $_[0] . 'b' }
2847
    },
2848
    into2 => {
2849
        date => sub { $_[0] . 'c' }
2850
    },
2851
    from1 => {
2852
        date => sub { $_[0] . 'd' }
2853
    },
2854
    from2 => {
2855
        date => sub { $_[0] . 'e' }
2856
    }
2857
);
2858
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
2859
$result = $dbi->select(table => 'table1');
2860
$result->filter(key1 => sub { $_[0] . 'f' });
2861
is($result->fetch_first->[0], '1abcdef');
2862

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2863
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2864
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2865
$dbi->type_rule(
2866
    from1 => {
2867
        date => sub { $_[0] . 'p' }
2868
    },
2869
    from2 => {
2870
        date => sub { $_[0] . 'q' }
2871
    },
2872
);
2873
$dbi->insert({key1 => '1'}, table => 'table1');
2874
$result = $dbi->select(table => 'table1');
2875
$result->type_rule(
2876
    from1 => {
2877
        date => sub { $_[0] . 'd' }
2878
    },
2879
    from2 => {
2880
        date => sub { $_[0] . 'e' }
2881
    }
2882
);
2883
$result->filter(key1 => sub { $_[0] . 'f' });
2884
is($result->fetch_first->[0], '1def');
2885

            
2886
test 'type_rule_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
2887
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2888
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2889
$dbi->type_rule(
2890
    from1 => {
2891
        date => sub { $_[0] * 2 },
2892
    },
2893
    into1 => {
2894
        date => sub { $_[0] * 2 },
2895
    }
2896
);
2897
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2898
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2899
is($result->type_rule_off->fetch->[0], 2);
2900

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2901
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2902
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2903
$dbi->type_rule(
2904
    from1 => {
2905
        date => sub { $_[0] * 2 },
2906
    },
2907
    into1 => {
2908
        date => sub { $_[0] * 3 },
2909
    }
2910
);
2911
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2912
$result = $dbi->select(table => 'table1', type_rule_off => 1);
2913
is($result->one->{key1}, 4);
2914

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2915
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2916
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2917
$dbi->type_rule(
2918
    from1 => {
2919
        date => sub { $_[0] * 2 },
2920
    },
2921
    into1 => {
2922
        date => sub { $_[0] * 3 },
2923
    }
2924
);
2925
$dbi->insert({key1 => 2}, table => 'table1');
2926
$result = $dbi->select(table => 'table1');
2927
is($result->one->{key1}, 12);
2928

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2929
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2930
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2931
$dbi->type_rule(
2932
    from1 => {
2933
        date => sub { $_[0] * 2 },
2934
    },
2935
    into1 => {
2936
        date => sub { $_[0] * 3 },
2937
    }
2938
);
2939
$dbi->insert({key1 => 2}, table => 'table1');
2940
$result = $dbi->select(table => 'table1');
2941
is($result->fetch->[0], 12);
2942

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2943
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2944
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2945
$dbi->register_filter(ppp => sub { uc $_[0] });
2946
$dbi->type_rule(
2947
    into1 => {
2948
        date => 'ppp'
2949
    }
2950
);
2951
$dbi->insert({key1 => 'a'}, table => 'table1');
2952
$result = $dbi->select(table => 'table1');
2953
is($result->one->{key1}, 'A');
2954

            
2955
eval{$dbi->type_rule(
2956
    into1 => {
2957
        date => 'pp'
2958
    }
2959
)};
2960
like($@, qr/not registered/);
2961

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2962
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2963
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2964
eval {
2965
    $dbi->type_rule(
2966
        from1 => {
2967
            Date => sub { $_[0] * 2 },
2968
        }
2969
    );
2970
};
2971
like($@, qr/lower/);
2972

            
2973
eval {
2974
    $dbi->type_rule(
2975
        into1 => {
2976
            Date => sub { $_[0] * 2 },
2977
        }
2978
    );
2979
};
2980
like($@, qr/lower/);
2981

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2982
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
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
$result->type_rule_off;
2995
is($result->one->{key1}, 6);
2996

            
cleanup test
Yuki Kimoto authored on 2011-08-06
2997
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
2998
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
2999
$dbi->type_rule(
3000
    from1 => {
3001
        date => sub { $_[0] * 2 },
3002
        datetime => sub { $_[0] * 4 },
3003
    },
3004
);
3005
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3006
$result = $dbi->select(table => 'table1');
3007
$result->type_rule(
3008
    from1 => {
3009
        date => sub { $_[0] * 3 }
3010
    }
3011
);
3012
$row = $result->one;
3013
is($row->{key1}, 6);
3014
is($row->{key2}, 2);
3015

            
3016
$result = $dbi->select(table => 'table1');
3017
$result->type_rule(
3018
    from1 => {
3019
        date => sub { $_[0] * 3 }
3020
    }
3021
);
3022
$row = $result->one;
3023
is($row->{key1}, 6);
3024
is($row->{key2}, 2);
3025

            
3026
$result = $dbi->select(table => 'table1');
3027
$result->type_rule(
3028
    from1 => {
3029
        date => sub { $_[0] * 3 }
3030
    }
3031
);
3032
$row = $result->one;
3033
is($row->{key1}, 6);
3034
is($row->{key2}, 2);
3035
$result = $dbi->select(table => 'table1');
3036
$result->type_rule(
3037
    from1 => [date => sub { $_[0] * 3 }]
3038
);
3039
$row = $result->one;
3040
is($row->{key1}, 6);
3041
is($row->{key2}, 2);
3042
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
3043
$result = $dbi->select(table => 'table1');
3044
$result->type_rule(
3045
    from1 => [date => 'fivetimes']
3046
);
3047
$row = $result->one;
3048
is($row->{key1}, 10);
3049
is($row->{key2}, 2);
3050
$result = $dbi->select(table => 'table1');
3051
$result->type_rule(
3052
    from1 => [date => undef]
3053
);
3054
$row = $result->one;
3055
is($row->{key1}, 2);
3056
is($row->{key2}, 2);
3057

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3058
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3059
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3060
$dbi->type_rule(
3061
    from1 => {
3062
        date => sub { $_[0] * 2 },
3063
    },
3064
);
3065
$dbi->insert({key1 => 2}, table => 'table1');
3066
$result = $dbi->select(table => 'table1');
3067
$result->filter(key1 => sub { $_[0] * 3 });
3068
is($result->one->{key1}, 12);
3069

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3070
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3071
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3072
$dbi->type_rule(
3073
    from1 => {
3074
        date => sub { $_[0] * 2 },
3075
    },
3076
);
3077
$dbi->insert({key1 => 2}, table => 'table1');
3078
$result = $dbi->select(table => 'table1');
3079
$result->filter(key1 => sub { $_[0] * 3 });
3080
is($result->fetch->[0], 12);
3081

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3082
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3083
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3084
$dbi->type_rule(
3085
    into1 => {
3086
        date => sub { $_[0] . 'b' }
3087
    },
3088
    into2 => {
3089
        date => sub { $_[0] . 'c' }
3090
    },
3091
    from1 => {
3092
        date => sub { $_[0] . 'd' }
3093
    },
3094
    from2 => {
3095
        date => sub { $_[0] . 'e' }
3096
    }
3097
);
3098
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
3099
$result = $dbi->select(table => 'table1');
3100
is($result->type_rule_off->fetch_first->[0], '1');
3101
$result = $dbi->select(table => 'table1');
3102
is($result->type_rule_on->fetch_first->[0], '1de');
3103

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3104
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3105
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3106
$dbi->type_rule(
3107
    into1 => {
3108
        date => sub { $_[0] . 'b' }
3109
    },
3110
    into2 => {
3111
        date => sub { $_[0] . 'c' }
3112
    },
3113
    from1 => {
3114
        date => sub { $_[0] . 'd' }
3115
    },
3116
    from2 => {
3117
        date => sub { $_[0] . 'e' }
3118
    }
3119
);
3120
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
3121
$result = $dbi->select(table => 'table1');
3122
is($result->type_rule1_off->fetch_first->[0], '1ce');
3123
$result = $dbi->select(table => 'table1');
3124
is($result->type_rule1_on->fetch_first->[0], '1cde');
3125

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3126
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3127
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3128
$dbi->type_rule(
3129
    into1 => {
3130
        date => sub { $_[0] . 'b' }
3131
    },
3132
    into2 => {
3133
        date => sub { $_[0] . 'c' }
3134
    },
3135
    from1 => {
3136
        date => sub { $_[0] . 'd' }
3137
    },
3138
    from2 => {
3139
        date => sub { $_[0] . 'e' }
3140
    }
3141
);
3142
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
3143
$result = $dbi->select(table => 'table1');
3144
is($result->type_rule2_off->fetch_first->[0], '1bd');
3145
$result = $dbi->select(table => 'table1');
3146
is($result->type_rule2_on->fetch_first->[0], '1bde');
3147

            
3148
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3149
$dbi = DBIx::Custom->connect(%memory);
3150
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3151
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3152

            
3153
$dbi->create_model(
3154
    table => 'table1',
3155
    join => [
3156
       'left outer join table2 on table1.key1 = table2.key1'
3157
    ],
3158
    primary_key => ['key1'],
3159
);
3160
$model2 = $dbi->create_model(
3161
    table => 'table2',
3162
);
3163
$dbi->setup_model;
3164
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3165
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
3166
$model = $dbi->model('table1');
3167
$result = $model->select(
3168
    column => [
3169
        $model->mycolumn,
3170
        {table2 => [qw/key1 key3/]}
3171
    ],
3172
    where => {'table1.key1' => 1}
3173
);
3174
is_deeply($result->one,
3175
          {key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3});
3176
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3177

            
3178
$dbi->separator('__');
3179
$model = $dbi->model('table1');
3180
$result = $model->select(
3181
    column => [
3182
        $model->mycolumn,
3183
        {table2 => [qw/key1 key3/]}
3184
    ],
3185
    where => {'table1.key1' => 1}
3186
);
3187
is_deeply($result->one,
3188
          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
3189
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3190

            
3191
$dbi->separator('-');
3192
$model = $dbi->model('table1');
3193
$result = $model->select(
3194
    column => [
3195
        $model->mycolumn,
3196
        {table2 => [qw/key1 key3/]}
3197
    ],
3198
    where => {'table1.key1' => 1}
3199
);
3200
is_deeply($result->one,
3201
          {key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3});
3202
is_deeply($model2->select->one, {key1 => 1, key3 => 3});
3203

            
3204

            
3205
test 'filter_off';
cleanup test
Yuki Kimoto authored on 2011-08-06
3206
$dbi = DBIx::Custom->connect(%memory);
3207
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3208
$dbi->execute('create table table2 (key1 char(255), key3 char(255));');
cleanup test
Yuki Kimoto authored on 2011-08-06
3209

            
3210
$dbi->create_model(
3211
    table => 'table1',
3212
    join => [
3213
       'left outer join table2 on table1.key1 = table2.key1'
3214
    ],
3215
    primary_key => ['key1'],
3216
);
3217
$dbi->setup_model;
3218
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3219
$model = $dbi->model('table1');
3220
$result = $model->select(column => 'key1');
3221
$result->filter(key1 => sub { $_[0] * 2 });
3222
is_deeply($result->one, {key1 => 2});
3223

            
3224
test 'available_date_type';
cleanup test
Yuki Kimoto authored on 2011-08-06
3225
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3226
ok($dbi->can('available_data_type'));
3227

            
3228

            
3229
test 'select prefix option';
cleanup test
Yuki Kimoto authored on 2011-08-06
3230
$dbi = DBIx::Custom->connect(%memory);
3231
$dbi->execute($create_table_default);
cleanup test
Yuki Kimoto authored on 2011-08-06
3232
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3233
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all;
3234
is_deeply($rows, [{key1 => 1, key2 => 2}], "table");
3235

            
3236

            
3237
test 'separator';
cleanup test
Yuki Kimoto authored on 2011-08-06
3238
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3239
is($dbi->separator, '.');
3240
$dbi->separator('-');
3241
is($dbi->separator, '-');
3242
$dbi->separator('__');
3243
is($dbi->separator, '__');
3244
eval { $dbi->separator('?') };
3245
like($@, qr/Separator/);
3246

            
3247

            
3248
test 'map_param';
cleanup test
Yuki Kimoto authored on 2011-08-06
3249
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3250
$param = $dbi->map_param(
3251
    {id => 1, author => 'Ken', price => 1900},
3252
    id => 'book.id',
3253
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3254
    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
3255
);
3256
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%',
3257
  'book.price' => 1900});
3258

            
3259
$param = $dbi->map_param(
3260
    {id => 0, author => 0, price => 0},
3261
    id => 'book.id',
3262
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3263
    price => ['book.price', sub { '%' . $_[0] . '%' },
3264
      {if => sub { $_[0] eq 0 }}]
3265
);
3266
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
3267

            
3268
$param = $dbi->map_param(
3269
    {id => '', author => '', price => ''},
3270
    id => 'book.id',
3271
    author => ['book.author', sub { '%' . $_[0] . '%' }],
3272
    price => ['book.price', sub { '%' . $_[0] . '%' },
3273
      {if => sub { $_[0] eq 1 }}]
3274
);
3275
is_deeply($param, {});
3276

            
3277
$param = $dbi->map_param(
3278
    {id => undef, author => undef, price => undef},
3279
    id => 'book.id',
3280
    price => ['book.price', {if => 'exists'}]
3281
);
3282
is_deeply($param, {'book.price' => undef});
3283

            
3284
$param = $dbi->map_param(
3285
    {price => 'a'},
3286
    id => ['book.id', {if => 'exists'}],
3287
    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
3288
);
3289
is_deeply($param, {'book.price' => '%a'});
3290

            
3291

            
3292
test 'table_alias';
cleanup test
Yuki Kimoto authored on 2011-08-06
3293
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3294
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3295
$dbi->type_rule(
3296
    into1 => {
3297
        date => sub { uc $_[0] }
3298
    }
3299
);
3300
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3301
  table_alias => {table2 => 'table1'});
3302
$result = $dbi->select(table => 'table1');
3303
is($result->one->{key1}, 'A');
3304

            
3305

            
3306
test 'order';
cleanup test
Yuki Kimoto authored on 2011-08-06
3307
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3308
$dbi->execute("create table table1 (key1, key2)");
3309
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3310
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1');
3311
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
3312
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1');
3313
my $order = $dbi->order;
3314
$order->prepend('key1', 'key2 desc');
3315
$result = $dbi->select(table => 'table1', append => "$order");
3316
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1},
3317
  {key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]);
3318
$order->prepend('key1 desc');
3319
$result = $dbi->select(table => 'table1', append => "$order");
3320
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2},
3321
  {key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]);
3322

            
3323
$order = $dbi->order;
3324
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]);
3325
$result = $dbi->select(table => 'table1',
3326
  column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']],
3327
  append => "$order");
3328
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3},
3329
  {'table1-key1' => 1, 'table1-key2' => 1},
3330
  {'table1-key1' => 2, 'table1-key2' => 4},
3331
  {'table1-key1' => 2, 'table1-key2' => 2}]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3332

            
3333
test 'tag_parse';
cleanup test
Yuki Kimoto authored on 2011-08-06
3334
$dbi = DBIx::Custom->connect(%memory);
cleanup test
Yuki Kimoto authored on 2011-08-06
3335
$dbi->tag_parse(0);
cleanup test
Yuki Kimoto authored on 2011-08-06
3336
$dbi->execute("create table table1 (key1, key2)");
3337
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1');
3338
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})};
3339
ok($@);
cleanup test
Yuki Kimoto authored on 2011-08-06
3340

            
3341
test 'last_sql';
cleanup test
Yuki Kimoto authored on 2011-08-06
3342
$dbi = DBIx::Custom->connect(%memory);
3343
$dbi->execute("create table table1 (key1, key2)");
3344
$dbi->execute('select * from table1');
3345
is($dbi->last_sql, 'select * from table1;');
3346

            
3347
eval{$dbi->execute("aaa")};
3348
is($dbi->last_sql, 'aaa;');
cleanup test
Yuki Kimoto authored on 2011-08-06
3349

            
3350
test 'DBIx::Custom header';
cleanup test
Yuki Kimoto authored on 2011-08-06
3351
$dbi = DBIx::Custom->connect(%memory);
3352
$dbi->execute("create table table1 (key1, key2)");
3353
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1');
3354
is_deeply($result->header, [qw/h1 h2/]);
cleanup test
Yuki Kimoto authored on 2011-08-06
3355

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

            
3362
$source = "select * from table1 where :key1{=} and :key2{=}";
3363
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3364
$rows = $result->all;
3365
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3366

            
3367
$source = "select * from table1 where :key1{ = } and :key2{=}";
3368
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
3369
$rows = $result->all;
3370
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3371

            
3372
$source = "select * from table1 where :key1{<} and :key2{=}";
3373
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2});
3374
$rows = $result->all;
3375
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3376

            
3377
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}";
3378
$result = $dbi->execute(
3379
    $source,
3380
    param => {'table1.key1' => 1, 'table1.key2' => 1},
3381
    filter => {'table1.key2' => sub { $_[0] * 2 }}
3382
);
3383
$rows = $result->all;
3384
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
3385

            
3386
test 'high perfomance way';
cleanup test
Yuki Kimoto authored on 2011-08-06
3387
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3388
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3389
$rows = [
3390
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3391
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3392
];
3393
{
3394
    my $query;
3395
    foreach my $row (@$rows) {
3396
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3397
      $dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }});
3398
    }
3399
    is_deeply($dbi->select(table => 'table1')->all,
3400
      [
3401
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3402
          {ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3403
      ]
3404
    );
3405
}
3406

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3407
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
3408
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);");
3409
$rows = [
3410
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3411
    {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3412
];
3413
{
3414
    my $query;
3415
    my $sth;
3416
    foreach my $row (@$rows) {
3417
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
3418
      $sth ||= $query->sth;
3419
      $sth->execute(map { $row->{$_} } sort keys %$row);
3420
    }
3421
    is_deeply($dbi->select(table => 'table1')->all,
3422
      [
3423
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7},
3424
          {ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8},
3425
      ]
3426
    );
3427
}
cleanup test
Yuki Kimoto authored on 2011-08-06
3428

            
clenup test
Yuki Kimoto authored on 2011-08-06
3429
test 'result';
3430
$dbi = DBIx::Custom->connect(%memory);
3431
$dbi->execute($create_table_default);
3432
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3433
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3434

            
3435
$result = $dbi->select(table => 'table1');
3436
@rows = ();
3437
while (my $row = $result->fetch) {
3438
    push @rows, [@$row];
3439
}
3440
is_deeply(\@rows, [[1, 2], [3, 4]]);
3441

            
3442
$result = $dbi->select(table => 'table1');
3443
@rows = ();
3444
while (my $row = $result->fetch_hash) {
3445
    push @rows, {%$row};
3446
}
3447
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3448

            
3449
$result = $dbi->select(table => 'table1');
3450
$row = $result->fetch_first;
3451
is_deeply($row, [1, 2], "row");
3452
$row = $result->fetch;
3453
ok(!$row, "finished");
3454

            
3455
$result = $dbi->select(table => 'table1');
3456
$row = $result->fetch_hash_first;
3457
is_deeply($row, {key1 => 1, key2 => 2}, "row");
3458
$row = $result->fetch_hash;
3459
ok(!$row, "finished");
3460

            
3461
$dbi->execute('create table table2 (key1, key2);');
3462
$result = $dbi->select(table => 'table2');
3463
$row = $result->fetch_hash_first;
3464
ok(!$row, "no row fetch");
3465

            
3466
$dbi = DBIx::Custom->connect(%memory);
3467
$dbi->execute($create_table_default);
3468
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3469
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3470
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
3471
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
3472
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
3473
$result = $dbi->select(table => 'table1');
3474
$rows = $result->fetch_multi(2);
3475
is_deeply($rows, [[1, 2],
3476
                  [3, 4]], "fetch_multi first");
3477
$rows = $result->fetch_multi(2);
3478
is_deeply($rows, [[5, 6],
3479
                  [7, 8]], "fetch_multi secound");
3480
$rows = $result->fetch_multi(2);
3481
is_deeply($rows, [[9, 10]], "fetch_multi third");
3482
$rows = $result->fetch_multi(2);
3483
ok(!$rows);
3484

            
3485
$result = $dbi->select(table => 'table1');
3486
eval {$result->fetch_multi};
3487
like($@, qr/Row count must be specified/, "Not specified row count");
3488

            
3489
$result = $dbi->select(table => 'table1');
3490
$rows = $result->fetch_hash_multi(2);
3491
is_deeply($rows, [{key1 => 1, key2 => 2},
3492
                  {key1 => 3, key2 => 4}], "fetch_multi first");
3493
$rows = $result->fetch_hash_multi(2);
3494
is_deeply($rows, [{key1 => 5, key2 => 6},
3495
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
3496
$rows = $result->fetch_hash_multi(2);
3497
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
3498
$rows = $result->fetch_hash_multi(2);
3499
ok(!$rows);
3500

            
3501
$result = $dbi->select(table => 'table1');
3502
eval {$result->fetch_hash_multi};
3503
like($@, qr/Row count must be specified/, "Not specified row count");
3504

            
3505
$dbi = DBIx::Custom->connect(%memory);
3506
$dbi->execute($create_table_default);
3507
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
3508
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
3509

            
3510
test 'fetch_all';
3511
$result = $dbi->select(table => 'table1');
3512
$rows = $result->fetch_all;
3513
is_deeply($rows, [[1, 2], [3, 4]]);
3514

            
3515
$result = $dbi->select(table => 'table1');
3516
$rows = $result->fetch_hash_all;
3517
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
3518

            
3519
$result = $dbi->select(table => 'table1');
3520
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3521
$result->filter({key1 => 'three_times'});
3522

            
3523
$rows = $result->fetch_all;
3524
is_deeply($rows, [[3, 2], [9, 4]], "array");
3525

            
3526
$result = $dbi->select(table => 'table1');
3527
$result->dbi->filters({three_times => sub { $_[0] * 3}});
3528
$result->filter({key1 => 'three_times'});
3529
$rows = $result->fetch_hash_all;
3530
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
3531

            
cleanup test
Yuki Kimoto authored on 2011-08-06
3532
test "query_builder";
3533
$datas = [
3534
    # Basic tests
3535
    {   name            => 'placeholder basic',
3536
        source            => "a {?  k1} b {=  k2} {<> k3} {>  k4} {<  k5} {>= k6} {<= k7} {like k8}", ,
3537
        sql_expected    => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;",
3538
        columns_expected   => [qw/k1 k2 k3 k4 k5 k6 k7 k8/]
3539
    },
3540
    {
3541
        name            => 'placeholder in',
3542
        source            => "{in k1 3};",
3543
        sql_expected    => "k1 in (?, ?, ?);",
3544
        columns_expected   => [qw/k1 k1 k1/]
3545
    },
3546
    
3547
    # Table name
3548
    {
3549
        name            => 'placeholder with table name',
3550
        source            => "{= a.k1} {= a.k2}",
3551
        sql_expected    => "a.k1 = ? a.k2 = ?;",
3552
        columns_expected  => [qw/a.k1 a.k2/]
3553
    },
3554
    {   
3555
        name            => 'placeholder in with table name',
3556
        source            => "{in a.k1 2} {in b.k2 2}",
3557
        sql_expected    => "a.k1 in (?, ?) b.k2 in (?, ?);",
3558
        columns_expected  => [qw/a.k1 a.k1 b.k2 b.k2/]
3559
    },
3560
    {
3561
        name            => 'not contain tag',
3562
        source            => "aaa",
3563
        sql_expected    => "aaa;",
3564
        columns_expected  => [],
3565
    }
3566
];
3567

            
3568
for (my $i = 0; $i < @$datas; $i++) {
3569
    my $data = $datas->[$i];
3570
    my $builder = DBIx::Custom->new->query_builder;
3571
    my $query = $builder->build_query($data->{source});
3572
    is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql");
3573
    is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns");
3574
}
3575

            
3576
$builder = DBIx::Custom->new->query_builder;
3577
$ret_val = $builder->register_tag(
3578
    p => sub {
3579
        my @args = @_;
3580
        
3581
        my $expand    = "? $args[0] $args[1]";
3582
        my $columns = [2];
3583
        return [$expand, $columns];
3584
    }
3585
);
3586

            
3587
$query = $builder->build_query("{p a b}");
3588
is($query->{sql}, "? a b;", "register_tag sql");
3589
is_deeply($query->{columns}, [2], "register_tag columns");
3590
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder');
3591

            
3592
$builder = DBIx::Custom->new->query_builder;
3593

            
3594
eval{$builder->build_query('{? }')};
3595
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
3596

            
3597
eval{$builder->build_query("{a }")};
3598
like($@, qr/\QTag "a" is not registered/, "tag not exist");
3599

            
3600
$builder->register_tag({
3601
    q => 'string'
3602
});
3603

            
3604
eval{$builder->build_query("{q}", {})};
3605
like($@, qr/Tag "q" must be sub reference/, "tag not code ref");
3606

            
3607
$builder->register_tag({
3608
   r => sub {} 
3609
});
3610

            
3611
eval{$builder->build_query("{r}")};
3612
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting");
3613

            
3614
$builder->register_tag({
3615
   s => sub { return ["a", ""]} 
3616
});
3617

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

            
3621
$builder->register_tag(
3622
    t => sub {return ["a", []]}
3623
);
3624

            
3625

            
3626
test 'General error case';
3627
$builder = DBIx::Custom->new->query_builder;
3628
$builder->register_tag(
3629
    a => sub {
3630
        return ["? ? ?", ['']];
3631
    }
3632
);
3633
eval{$builder->build_query("{a}")};
3634
like($@, qr/\QPlaceholder count/, "placeholder count is invalid");
3635

            
3636

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

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

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

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

            
3652
test 'variouse source';
3653
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;";
3654
$query = $builder->build_query($source);
3655
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1");
3656

            
3657
$source = "abc;";
3658
$query = $builder->build_query($source);
3659
is($query->sql, 'abc;', "basic : 2");
3660

            
3661
$source = "{= a}";
3662
$query = $builder->build_query($source);
3663
is($query->sql, 'a = ?;', "only tag");
3664

            
3665
$source = "000;";
3666
$query = $builder->build_query($source);
3667
is($query->sql, '000;', "contain 0 value");
3668

            
3669
$source = "a {= b} }";
3670
eval{$builder->build_query($source)};
3671
like($@, qr/unexpected "}"/, "error : 1");
3672

            
3673
$source = "a {= {}";
3674
eval{$builder->build_query($source)};
3675
like($@, qr/unexpected "{"/, "error : 2");