DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
1699 lines | 56.853kb
removed register_format()
yuki-kimoto authored on 2010-05-26
1
use Test::More;
2
use strict;
3
use warnings;
4

            
5
use utf8;
6
use Encode qw/encode_utf8 decode_utf8/;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
7
use Data::Dumper;
removed register_format()
yuki-kimoto authored on 2010-05-26
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

            
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
19
use FindBin;
20
use lib "$FindBin::Bin/dbix-custom-core-sqlite";
21

            
removed register_format()
yuki-kimoto authored on 2010-05-26
22
# Function for test name
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
23
sub test { print "# $_[0]\n" }
removed register_format()
yuki-kimoto authored on 2010-05-26
24

            
25
# Constant varialbes for test
26
my $CREATE_TABLE = {
27
    0 => 'create table table1 (key1 char(255), key2 char(255));',
28
    1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));',
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
29
    2 => 'create table table2 (key1 char(255), key3 char(255));',
30
    3 => 'create table table1 (key1 Date, key2 datetime);'
removed register_format()
yuki-kimoto authored on 2010-05-26
31
};
32

            
add tests
yuki-kimoto authored on 2010-08-10
33
my $SELECT_SOURCES = {
removed register_format()
yuki-kimoto authored on 2010-05-26
34
    0 => 'select * from table1;'
35
};
36

            
37
my $DROP_TABLE = {
38
    0 => 'drop table table1'
39
};
40

            
41
my $NEW_ARGS = {
42
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
43
};
44

            
45
# Variables
46
my $dbi;
47
my $sth;
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
48
my $source;
49
my @sources;
add tests
yuki-kimoto authored on 2010-08-10
50
my $select_SOURCE;
51
my $insert_SOURCE;
52
my $update_SOURCE;
removed register_format()
yuki-kimoto authored on 2010-05-26
53
my $params;
54
my $sql;
55
my $result;
56
my $row;
57
my @rows;
58
my $rows;
59
my $query;
60
my @queries;
61
my $select_query;
62
my $insert_query;
63
my $update_query;
64
my $ret_val;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
65
my $infos;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
66
my $model;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
67
my $where;
removed register_format()
yuki-kimoto authored on 2010-05-26
68

            
69
# Prepare table
70
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
71
$dbi->execute($CREATE_TABLE->{0});
72
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
73
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
74

            
75
test 'DBIx::Custom::Result test';
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
76
$source = "select key1, key2 from table1";
77
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
78
$result = $dbi->execute($query);
79

            
80
@rows = ();
81
while (my $row = $result->fetch) {
82
    push @rows, [@$row];
83
}
cleanup
Yuki Kimoto authored on 2011-01-23
84
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
removed register_format()
yuki-kimoto authored on 2010-05-26
85

            
86
$result = $dbi->execute($query);
87
@rows = ();
88
while (my $row = $result->fetch_hash) {
89
    push @rows, {%$row};
90
}
cleanup
Yuki Kimoto authored on 2011-01-23
91
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
removed register_format()
yuki-kimoto authored on 2010-05-26
92

            
93
$result = $dbi->execute($query);
94
$rows = $result->fetch_all;
cleanup
Yuki Kimoto authored on 2011-01-23
95
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
removed register_format()
yuki-kimoto authored on 2010-05-26
96

            
97
$result = $dbi->execute($query);
removed reconnect method
yuki-kimoto authored on 2010-05-28
98
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
99
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash_all");
removed register_format()
yuki-kimoto authored on 2010-05-26
100

            
101
test 'Insert query return value';
102
$dbi->execute($DROP_TABLE->{0});
103
$dbi->execute($CREATE_TABLE->{0});
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
104
$source = "insert into table1 {insert_param key1 key2}";
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
105
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
106
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
cleanup
Yuki Kimoto authored on 2011-01-23
107
ok($ret_val);
removed register_format()
yuki-kimoto authored on 2010-05-26
108

            
109

            
110
test 'Direct query';
111
$dbi->execute($DROP_TABLE->{0});
112
$dbi->execute($CREATE_TABLE->{0});
add tests
yuki-kimoto authored on 2010-08-10
113
$insert_SOURCE = "insert into table1 {insert_param key1 key2}";
114
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2});
115
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
116
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
117
is_deeply($rows, [{key1 => 1, key2 => 2}]);
removed register_format()
yuki-kimoto authored on 2010-05-26
118

            
119
test 'Filter basic';
120
$dbi->execute($DROP_TABLE->{0});
121
$dbi->execute($CREATE_TABLE->{0});
122
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
123
                    three_times => sub { $_[0] * 3});
124

            
add tests
yuki-kimoto authored on 2010-08-10
125
$insert_SOURCE  = "insert into table1 {insert_param key1 key2};";
126
$insert_query = $dbi->create_query($insert_SOURCE);
removed register_format()
yuki-kimoto authored on 2010-05-26
127
$insert_query->filter({key1 => 'twice'});
128
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
add tests
yuki-kimoto authored on 2010-08-10
129
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
130
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
131
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
removed register_format()
yuki-kimoto authored on 2010-05-26
132
$dbi->execute($DROP_TABLE->{0});
133

            
134
test 'Filter in';
135
$dbi->execute($CREATE_TABLE->{0});
add tests
yuki-kimoto authored on 2010-08-10
136
$insert_SOURCE  = "insert into table1 {insert_param key1 key2};";
137
$insert_query = $dbi->create_query($insert_SOURCE);
removed register_format()
yuki-kimoto authored on 2010-05-26
138
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
add tests
yuki-kimoto authored on 2010-08-10
139
$select_SOURCE = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
140
$select_query = $dbi->create_query($select_SOURCE);
removed register_format()
yuki-kimoto authored on 2010-05-26
141
$select_query->filter({'table1.key1' => 'twice'});
142
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
143
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
144
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
removed register_format()
yuki-kimoto authored on 2010-05-26
145

            
146
test 'DBIx::Custom::SQLTemplate basic tag';
147
$dbi->execute($DROP_TABLE->{0});
148
$dbi->execute($CREATE_TABLE->{1});
149
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
150
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
151

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
152
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
153
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
154
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
155
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
156
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
removed register_format()
yuki-kimoto authored on 2010-05-26
157

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
158
$source = "select * from table1 where {<= key1} and {like key2};";
159
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
160
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
161
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
162
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
removed register_format()
yuki-kimoto authored on 2010-05-26
163

            
164
test 'DIB::Custom::SQLTemplate in tag';
165
$dbi->execute($DROP_TABLE->{0});
166
$dbi->execute($CREATE_TABLE->{1});
167
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
168
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
169

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
170
$source = "select * from table1 where {in key1 2};";
171
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
172
$result = $dbi->execute($query, param => {key1 => [9, 1]});
173
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
174
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
removed register_format()
yuki-kimoto authored on 2010-05-26
175

            
176
test 'DBIx::Custom::SQLTemplate insert tag';
177
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
178
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
179
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
removed register_format()
yuki-kimoto authored on 2010-05-26
180

            
add tests
yuki-kimoto authored on 2010-08-10
181
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
182
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
183
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
removed register_format()
yuki-kimoto authored on 2010-05-26
184

            
185
test 'DBIx::Custom::SQLTemplate update tag';
186
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
187
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
188
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
189
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
removed register_format()
yuki-kimoto authored on 2010-05-26
190

            
add tests
yuki-kimoto authored on 2010-08-10
191
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
192
$dbi->execute($update_SOURCE, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
removed register_format()
yuki-kimoto authored on 2010-05-26
193

            
add tests
yuki-kimoto authored on 2010-08-10
194
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
195
$rows = $result->fetch_hash_all;
196
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
cleanup
Yuki Kimoto authored on 2011-01-23
197
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
removed register_format()
yuki-kimoto authored on 2010-05-26
198

            
199
test 'Error case';
200
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')};
cleanup
Yuki Kimoto authored on 2011-01-23
201
ok($@, "connect error");
removed register_format()
yuki-kimoto authored on 2010-05-26
202

            
203
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
204
eval{$dbi->create_query("{p }")};
cleanup
Yuki Kimoto authored on 2011-01-23
205
ok($@, "create_query invalid SQL template");
removed register_format()
yuki-kimoto authored on 2010-05-26
206

            
207
test 'insert';
208
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
209
$dbi->execute($CREATE_TABLE->{0});
210
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
211
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
add tests
yuki-kimoto authored on 2010-08-10
212
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
213
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
214
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
removed register_format()
yuki-kimoto authored on 2010-05-26
215

            
216
$dbi->execute('delete from table1');
217
$dbi->register_filter(
218
    twice       => sub { $_[0] * 2 },
219
    three_times => sub { $_[0] * 3 }
220
);
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
221
$dbi->default_bind_filter('twice');
removed register_format()
yuki-kimoto authored on 2010-05-26
222
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
add tests
yuki-kimoto authored on 2010-08-10
223
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
224
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
225
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
226
$dbi->default_bind_filter(undef);
removed register_format()
yuki-kimoto authored on 2010-05-26
227

            
228
$dbi->execute($DROP_TABLE->{0});
229
$dbi->execute($CREATE_TABLE->{0});
230
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
231
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
232
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
233

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
234
eval{$dbi->insert(table => 'table1', noexist => 1)};
cleanup
Yuki Kimoto authored on 2011-01-23
235
like($@, qr/noexist/, "invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
236

            
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
237
eval{$dbi->insert(table => 'table', param => {';' => 1})};
238
like($@, qr/safety/);
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
239

            
removed register_format()
yuki-kimoto authored on 2010-05-26
240
test 'update';
241
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
242
$dbi->execute($CREATE_TABLE->{1});
243
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
244
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
245
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
add tests
yuki-kimoto authored on 2010-08-10
246
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
247
$rows   = $result->fetch_hash_all;
248
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
249
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
cleanup
Yuki Kimoto authored on 2011-01-23
250
                  "basic");
removed register_format()
yuki-kimoto authored on 2010-05-26
251
                  
252
$dbi->execute("delete from table1");
253
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
254
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
255
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
add tests
yuki-kimoto authored on 2010-08-10
256
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
257
$rows   = $result->fetch_hash_all;
258
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
259
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
cleanup
Yuki Kimoto authored on 2011-01-23
260
                  "update key same as search key");
removed register_format()
yuki-kimoto authored on 2010-05-26
261

            
add tests
yuki-kimoto authored on 2010-08-10
262
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
263
$result = $dbi->execute($SELECT_SOURCES->{0});
264
$rows   = $result->fetch_hash_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}],
cleanup
Yuki Kimoto authored on 2011-01-23
267
                  "update key same as search key : param is array ref");
add tests
yuki-kimoto authored on 2010-08-10
268

            
removed register_format()
yuki-kimoto authored on 2010-05-26
269
$dbi->execute("delete from table1");
270
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
271
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
272
$dbi->register_filter(twice => sub { $_[0] * 2 });
273
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
many changed
Yuki Kimoto authored on 2011-01-23
274
              filter => {key2 => sub { $_[0] * 2 }});
add tests
yuki-kimoto authored on 2010-08-10
275
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
276
$rows   = $result->fetch_hash_all;
277
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
278
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
cleanup
Yuki Kimoto authored on 2011-01-23
279
                  "filter");
removed register_format()
yuki-kimoto authored on 2010-05-26
280

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

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
283
eval{$dbi->update(table => 'table1', noexist => 1)};
cleanup
Yuki Kimoto authored on 2011-01-23
284
like($@, qr/noexist/, "invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
285

            
286
eval{$dbi->update(table => 'table1')};
cleanup
Yuki Kimoto authored on 2011-01-23
287
like($@, qr/where/, "not contain where");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
288

            
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
289
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
290
$dbi->execute($CREATE_TABLE->{0});
291
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
292
$where = $dbi->where;
293
$where->clause(['and', '{= key1}', '{= key2}']);
294
$where->param({key1 => 1, key2 => 2});
295
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
296
$result = $dbi->select(table => 'table1');
297
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where');
298

            
299
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
300
$dbi->execute($CREATE_TABLE->{0});
301
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
302
$where = $dbi->where;
303
$where->clause(['and', '{= key2}']);
304
$where->param({key2 => 2});
305
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
306
$result = $dbi->select(table => 'table1');
307
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where');
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
308

            
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
309
eval{$dbi->update(table => 'table1', param => {';' => 1})};
310
like($@, qr/safety/);
311

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
315
test 'update_all';
316
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
317
$dbi->execute($CREATE_TABLE->{1});
318
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
319
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
320
$dbi->register_filter(twice => sub { $_[0] * 2 });
321
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
add tests
yuki-kimoto authored on 2010-08-10
322
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
323
$rows   = $result->fetch_hash_all;
324
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
325
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
cleanup
Yuki Kimoto authored on 2011-01-23
326
                  "filter");
removed register_format()
yuki-kimoto authored on 2010-05-26
327

            
328

            
329
test 'delete';
330
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
331
$dbi->execute($CREATE_TABLE->{0});
332
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
333
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
334
$dbi->delete(table => 'table1', where => {key1 => 1});
add tests
yuki-kimoto authored on 2010-08-10
335
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
336
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
337
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
removed register_format()
yuki-kimoto authored on 2010-05-26
338

            
339
$dbi->execute("delete from table1;");
340
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
341
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
342
$dbi->register_filter(twice => sub { $_[0] * 2 });
343
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
add tests
yuki-kimoto authored on 2010-08-10
344
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
345
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
346
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
removed register_format()
yuki-kimoto authored on 2010-05-26
347

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

            
350
$dbi->delete_all(table => 'table1');
351
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
352
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
353
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
354
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
355
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
removed register_format()
yuki-kimoto authored on 2010-05-26
356

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
357
eval{$dbi->delete(table => 'table1', noexist => 1)};
cleanup
Yuki Kimoto authored on 2011-01-23
358
like($@, qr/noexist/, "invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
359

            
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
360
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
361
$dbi->execute($CREATE_TABLE->{0});
362
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
363
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
364
$where = $dbi->where;
365
$where->clause(['and', '{= key1}', '{= key2}']);
366
$where->param({ke1 => 1, key2 => 2});
367
$dbi->delete(table => 'table1', where => $where);
368
$result = $dbi->select(table => 'table1');
369
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 4}], 'delete() where');
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
370

            
removed register_format()
yuki-kimoto authored on 2010-05-26
371
test 'delete error';
372
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
373
$dbi->execute($CREATE_TABLE->{0});
374
eval{$dbi->delete(table => 'table1')};
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
375
like($@, qr/"where" must be specified/,
cleanup
Yuki Kimoto authored on 2011-01-23
376
         "where key-value pairs not specified");
removed register_format()
yuki-kimoto authored on 2010-05-26
377

            
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
378
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
379
like($@, qr/safety/);
380

            
removed register_format()
yuki-kimoto authored on 2010-05-26
381
test 'delete_all';
382
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
383
$dbi->execute($CREATE_TABLE->{0});
384
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
385
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
386
$dbi->delete_all(table => 'table1');
add tests
yuki-kimoto authored on 2010-08-10
387
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
388
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
389
is_deeply($rows, [], "basic");
removed register_format()
yuki-kimoto authored on 2010-05-26
390

            
391

            
392
test 'select';
393
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
394
$dbi->execute($CREATE_TABLE->{0});
395
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
396
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
397
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
398
is_deeply($rows, [{key1 => 1, key2 => 2},
cleanup
Yuki Kimoto authored on 2011-01-23
399
                  {key1 => 3, key2 => 4}], "table");
removed register_format()
yuki-kimoto authored on 2010-05-26
400

            
update document
yuki-kimoto authored on 2010-05-27
401
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
402
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key");
removed register_format()
yuki-kimoto authored on 2010-05-26
403

            
404
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
405
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key");
removed register_format()
yuki-kimoto authored on 2010-05-26
406

            
update document
yuki-kimoto authored on 2010-05-27
407
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
408
is_deeply($rows, [{key1 => 3}], "table and columns and where key");
removed register_format()
yuki-kimoto authored on 2010-05-26
409

            
410
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
411
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
removed register_format()
yuki-kimoto authored on 2010-05-26
412

            
413
$dbi->register_filter(decrement => sub { $_[0] - 1 });
update document
yuki-kimoto authored on 2010-05-27
414
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
removed register_format()
yuki-kimoto authored on 2010-05-26
415
            ->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
416
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
removed register_format()
yuki-kimoto authored on 2010-05-26
417

            
418
$dbi->execute($CREATE_TABLE->{2});
419
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
420
$rows = $dbi->select(
421
    table => [qw/table1 table2/],
select method column option ...
Yuki Kimoto authored on 2011-02-22
422
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
removed register_format()
yuki-kimoto authored on 2010-05-26
423
    where   => {'table1.key2' => 2},
added commit method
yuki-kimoto authored on 2010-05-27
424
    relation  => {'table1.key1' => 'table2.key1'}
removed register_format()
yuki-kimoto authored on 2010-05-26
425
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
426
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
added commit method
yuki-kimoto authored on 2010-05-27
427

            
428
$rows = $dbi->select(
429
    table => [qw/table1 table2/],
430
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
431
    relation  => {'table1.key1' => 'table2.key1'}
432
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
433
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
removed register_format()
yuki-kimoto authored on 2010-05-26
434

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
435
eval{$dbi->select(table => 'table1', noexist => 1)};
cleanup
Yuki Kimoto authored on 2011-01-23
436
like($@, qr/noexist/, "invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
437

            
438

            
removed register_format()
yuki-kimoto authored on 2010-05-26
439
test 'fetch filter';
440
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
441
$dbi->register_filter(
442
    twice       => sub { $_[0] * 2 },
443
    three_times => sub { $_[0] * 3 }
444
);
445
$dbi->default_fetch_filter('twice');
446
$dbi->execute($CREATE_TABLE->{0});
447
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
448
$result = $dbi->select(table => 'table1');
449
$result->filter({key1 => 'three_times'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
450
$row = $result->fetch_hash_first;
cleanup
Yuki Kimoto authored on 2011-01-23
451
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
removed register_format()
yuki-kimoto authored on 2010-05-26
452

            
453
test 'filters';
454
$dbi = DBIx::Custom->new;
455

            
update document
yuki-kimoto authored on 2010-05-27
456
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
cleanup
Yuki Kimoto authored on 2011-01-23
457
   'あ', "decode_utf8");
removed register_format()
yuki-kimoto authored on 2010-05-26
458

            
459
is($dbi->filters->{encode_utf8}->('あ'),
cleanup
Yuki Kimoto authored on 2011-01-23
460
   encode_utf8('あ'), "encode_utf8");
removed register_format()
yuki-kimoto authored on 2010-05-26
461

            
added commit method
yuki-kimoto authored on 2010-05-27
462
test 'transaction';
463
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
464
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
465
$dbi->dbh->begin_work;
added commit method
yuki-kimoto authored on 2010-05-27
466
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
467
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
468
$dbi->dbh->commit;
added commit method
yuki-kimoto authored on 2010-05-27
469
$result = $dbi->select(table => 'table1');
470
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
cleanup
Yuki Kimoto authored on 2011-01-23
471
          "commit");
added commit method
yuki-kimoto authored on 2010-05-27
472

            
473
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
474
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
475
$dbi->dbh->begin_work(0);
added commit method
yuki-kimoto authored on 2010-05-27
476
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
477
$dbi->dbh->rollback;
added commit method
yuki-kimoto authored on 2010-05-27
478

            
479
$result = $dbi->select(table => 'table1');
cleanup
Yuki Kimoto authored on 2011-01-23
480
ok(! $result->fetch_first, "rollback");
add cache attribute
yuki-kimoto authored on 2010-06-14
481

            
482
test 'cache';
483
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
484
$dbi->execute($CREATE_TABLE->{0});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
485
$source = 'select * from table1 where {= key1} and {= key2};';
486
$dbi->create_query($source);
487
is_deeply($dbi->{_cached}->{$source}, 
add table tag
Yuki Kimoto authored on 2011-02-09
488
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
add cache attribute
yuki-kimoto authored on 2010-06-14
489

            
490
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
491
$dbi->execute($CREATE_TABLE->{0});
492
$dbi->{_cached} = {};
493
$dbi->cache(0);
494
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
495
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
496

            
add tests
yuki-kimoto authored on 2010-08-10
497
test 'execute';
498
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
499
$dbi->execute($CREATE_TABLE->{0});
removed experimental registe...
yuki-kimoto authored on 2010-08-24
500
{
501
    local $Carp::Verbose = 0;
502
    eval{$dbi->execute('select * frm table1')};
cleanup
Yuki Kimoto authored on 2011-01-23
503
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
504
    like($@, qr/\.t /, "fail : not verbose");
removed experimental registe...
yuki-kimoto authored on 2010-08-24
505
}
506
{
507
    local $Carp::Verbose = 1;
508
    eval{$dbi->execute('select * frm table1')};
cleanup
Yuki Kimoto authored on 2011-01-23
509
    like($@, qr/Custom.*\.t /s, "fail : verbose");
removed experimental registe...
yuki-kimoto authored on 2010-08-24
510
}
add tests
yuki-kimoto authored on 2010-08-10
511

            
512
eval{$dbi->execute('select * from table1', no_exists => 1)};
cleanup
Yuki Kimoto authored on 2011-01-23
513
like($@, qr/\Q"no_exists" is invalid argument/, "invald SQL");
add tests
yuki-kimoto authored on 2010-08-10
514

            
515
$query = $dbi->create_query('select * from table1 where {= key1}');
516
$dbi->dbh->disconnect;
517
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
cleanup
Yuki Kimoto authored on 2011-01-23
518
ok($@, "execute fail");
add tests
yuki-kimoto authored on 2010-08-10
519

            
removed experimental registe...
yuki-kimoto authored on 2010-08-24
520
{
521
    local $Carp::Verbose = 0;
522
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
cleanup
Yuki Kimoto authored on 2011-01-23
523
    like($@, qr/\Q.t /, "caller spec : not vebose");
removed experimental registe...
yuki-kimoto authored on 2010-08-24
524
}
525
{
526
    local $Carp::Verbose = 1;
527
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
cleanup
Yuki Kimoto authored on 2011-01-23
528
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
removed experimental registe...
yuki-kimoto authored on 2010-08-24
529
}
cleanup
yuki-kimoto authored on 2010-10-17
530

            
531

            
532
test 'transaction';
533
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
534
$dbi->execute($CREATE_TABLE->{0});
535

            
536
$dbi->begin_work;
537

            
538
eval {
539
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
540
    die "Error";
541
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
542
};
543

            
544
$dbi->rollback if $@;
545

            
546
$result = $dbi->select(table => 'table1');
547
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
548
is_deeply($rows, [], "rollback");
cleanup
yuki-kimoto authored on 2010-10-17
549

            
550
$dbi->begin_work;
551

            
552
eval {
553
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
554
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
555
};
556

            
557
$dbi->commit unless $@;
558

            
559
$result = $dbi->select(table => 'table1');
560
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
561
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
cleanup
yuki-kimoto authored on 2010-10-17
562

            
563
$dbi->dbh->{AutoCommit} = 0;
564
eval{ $dbi->begin_work };
cleanup
Yuki Kimoto authored on 2011-01-23
565
ok($@, "exception");
cleanup
yuki-kimoto authored on 2010-10-17
566
$dbi->dbh->{AutoCommit} = 1;
567

            
added helper method
yuki-kimoto authored on 2010-10-17
568

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
569
test 'method';
added helper method
yuki-kimoto authored on 2010-10-17
570
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
571
$dbi->method(
added helper method
yuki-kimoto authored on 2010-10-17
572
    one => sub { 1 }
573
);
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
574
$dbi->method(
added helper method
yuki-kimoto authored on 2010-10-17
575
    two => sub { 2 }
576
);
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
577
$dbi->method({
added helper method
yuki-kimoto authored on 2010-10-17
578
    twice => sub {
579
        my $self = shift;
580
        return $_[0] * 2;
581
    }
582
});
583

            
cleanup
Yuki Kimoto authored on 2011-01-23
584
is($dbi->one, 1, "first");
585
is($dbi->two, 2, "second");
586
is($dbi->twice(5), 10 , "second");
added helper method
yuki-kimoto authored on 2010-10-17
587

            
588
eval {$dbi->XXXXXX};
autoload DBI method
Yuki Kimoto authored on 2011-01-26
589
ok($@, "not exists");
added helper method
yuki-kimoto authored on 2010-10-17
590

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
591
test 'out filter';
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
592
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
593
$dbi->execute($CREATE_TABLE->{0});
594
$dbi->register_filter(twice => sub { $_[0] * 2 });
595
$dbi->register_filter(three_times => sub { $_[0] * 3});
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
596
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
597
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
598
              'key2' => {out => 'three_times', in => 'twice'});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
599
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
600
$result = $dbi->execute($SELECT_SOURCES->{0});
601
$row   = $result->fetch_hash_first;
cleanup
Yuki Kimoto authored on 2011-01-23
602
is_deeply($row, {key1 => 2, key2 => 6}, "insert");
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
603
$result = $dbi->select(table => 'table1');
604
$row   = $result->fetch_hash_first;
cleanup
Yuki Kimoto authored on 2011-01-23
605
is_deeply($row, {key1 => 6, key2 => 12}, "insert");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
606

            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
607
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
608
$dbi->execute($CREATE_TABLE->{0});
609
$dbi->register_filter(twice => sub { $_[0] * 2 });
610
$dbi->register_filter(three_times => sub { $_[0] * 3});
611
$dbi->apply_filter(
612
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
613
              'key2' => {out => 'three_times', in => 'twice'});
614
$dbi->apply_filter(
615
    'table1', 'key1' => {out => undef}
616
); 
617
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
618
$result = $dbi->execute($SELECT_SOURCES->{0});
619
$row   = $result->fetch_hash_first;
620
is_deeply($row, {key1 => 1, key2 => 6}, "insert");
621

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
622
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
623
$dbi->execute($CREATE_TABLE->{0});
624
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
625
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
626
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
627
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
628
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
629
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
630
$result = $dbi->execute($SELECT_SOURCES->{0});
631
$row   = $result->fetch_hash_first;
cleanup
Yuki Kimoto authored on 2011-01-23
632
is_deeply($row, {key1 => 4, key2 => 2}, "update");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
633

            
634
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
635
$dbi->execute($CREATE_TABLE->{0});
636
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
637
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
638
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
639
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
640
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
641
$dbi->delete(table => 'table1', where => {key1 => 1});
642
$result = $dbi->execute($SELECT_SOURCES->{0});
643
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
644
is_deeply($rows, [], "delete");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
645

            
646
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
647
$dbi->execute($CREATE_TABLE->{0});
648
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
649
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
650
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
651
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
652
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
653
$result = $dbi->select(table => 'table1', where => {key1 => 1});
654
$result->filter({'key2' => 'twice'});
655
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
656
is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
657

            
658
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
659
$dbi->execute($CREATE_TABLE->{0});
660
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
661
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
662
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
663
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
664
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
665
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};",
666
                        param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
667
                        table => ['table1']);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
668
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
669
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
670

            
add table tag
Yuki Kimoto authored on 2011-02-09
671
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
672
$dbi->execute($CREATE_TABLE->{0});
673
$dbi->register_filter(twice => sub { $_[0] * 2 });
674
$dbi->apply_filter(
675
    'table1', 'key1' => {out => 'twice', in => 'twice'}
676
);
677
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
678
$result = $dbi->execute("select * from {table table1} where {= key1} and {= key2};",
679
                        param => {key1 => 1, key2 => 2});
680
$rows   = $result->fetch_hash_all;
681
is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
682

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
683
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
684
$dbi->execute($CREATE_TABLE->{0});
685
$dbi->execute($CREATE_TABLE->{2});
686
$dbi->register_filter(twice => sub { $_[0] * 2 });
687
$dbi->register_filter(three_times => sub { $_[0] * 3 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
688
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
689
    'table1', 'key2' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
690
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
691
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
692
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
693
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
694
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
695
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
696
$result = $dbi->select(
697
     table => ['table1', 'table2'],
698
     column => ['key2', 'key3'],
699
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
700

            
701
$result->filter({'key2' => 'twice'});
702
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
703
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
704

            
705
$result = $dbi->select(
706
     table => ['table1', 'table2'],
707
     column => ['key2', 'key3'],
708
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
709

            
710
$result->filter({'key2' => 'twice'});
711
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
712
is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
713

            
pod fix
Yuki Kimoto authored on 2011-01-21
714
test 'each_column';
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
715
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
716
$dbi->execute($CREATE_TABLE->{2});
717
$dbi->execute($CREATE_TABLE->{3});
718

            
719
$infos = [];
pod fix
Yuki Kimoto authored on 2011-01-21
720
$dbi->each_column(sub {
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
721
    my ($self, $table, $column, $cinfo) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
722
    
723
    if ($table =~ /^table/) {
724
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
725
         push @$infos, $info;
726
    }
727
});
728
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
729
is_deeply($infos, 
730
    [
731
        ['table1', 'key1', 'key1'],
732
        ['table1', 'key2', 'key2'],
733
        ['table2', 'key1', 'key1'],
734
        ['table2', 'key3', 'key3']
735
    ]
cleanup
Yuki Kimoto authored on 2011-01-23
736
    
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
737
);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
738

            
add examples
Yuki Kimoto authored on 2011-01-07
739
test 'limit';
740
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
741
$dbi->execute($CREATE_TABLE->{0});
742
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
743
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
744
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
745
$dbi->register_tag(
add examples
Yuki Kimoto authored on 2011-01-07
746
    limit => sub {
747
        my ($count, $offset) = @_;
748
        
749
        my $s = '';
750
        $s .= "limit $count";
751
        $s .= " offset $offset" if defined $offset;
752
        
753
        return [$s, []];
754
    }
755
);
756
$rows = $dbi->select(
757
  table => 'table1',
758
  where => {key1 => 1},
759
  append => "order by key2 {limit 1 0}"
760
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
761
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
762
$rows = $dbi->select(
763
  table => 'table1',
764
  where => {key1 => 1},
765
  append => "order by key2 {limit 2 1}"
766
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
767
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
add examples
Yuki Kimoto authored on 2011-01-07
768
$rows = $dbi->select(
769
  table => 'table1',
770
  where => {key1 => 1},
771
  append => "order by key2 {limit 1}"
772
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
773
is_deeply($rows, [{key1 => 1, key2 => 2}]);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
774

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
775
test 'connect super';
776
{
777
    package MyDBI;
778
    
779
    use base 'DBIx::Custom';
780
    sub connect {
781
        my $self = shift->SUPER::connect(@_);
782
        
783
        return $self;
784
    }
785
    
786
    sub new {
cleanup
Yuki Kimoto authored on 2011-01-25
787
        my $self = shift->SUPER::new(@_);
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
788
        
789
        return $self;
790
    }
791
}
792

            
793
$dbi = MyDBI->connect($NEW_ARGS->{0});
794
$dbi->execute($CREATE_TABLE->{0});
795
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup
Yuki Kimoto authored on 2011-01-23
796
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
797

            
798
$dbi = MyDBI->new($NEW_ARGS->{0});
cleanup
Yuki Kimoto authored on 2011-01-25
799
$dbi->connect;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
800
$dbi->execute($CREATE_TABLE->{0});
801
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
cleanup
Yuki Kimoto authored on 2011-01-23
802
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
803

            
cleanup
Yuki Kimoto authored on 2011-01-25
804
{
805
    package MyDBI2;
806
    
807
    use base 'DBIx::Custom';
808
    sub connect {
809
        my $self = shift->SUPER::new(@_);
810
        $self->connect;
811
        
812
        return $self;
813
    }
814
}
815

            
816
$dbi = MyDBI->connect($NEW_ARGS->{0});
817
$dbi->execute($CREATE_TABLE->{0});
818
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
819
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
820

            
821
test 'end_filter';
822
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
823
$dbi->execute($CREATE_TABLE->{0});
824
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
825
$result = $dbi->select(table => 'table1');
826
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
827
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
828
$row = $result->fetch_first;
829
is_deeply($row, [6, 40]);
830

            
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
831
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
832
$dbi->execute($CREATE_TABLE->{0});
833
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
834
$result = $dbi->select(table => 'table1');
835
$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
836
$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
837
$row = $result->fetch_first;
838
is_deeply($row, [6, 12]);
839

            
840
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
841
$dbi->execute($CREATE_TABLE->{0});
842
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
843
$result = $dbi->select(table => 'table1');
844
$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
845
$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
846
$row = $result->fetch_first;
847
is_deeply($row, [6, 12]);
848

            
many changed
Yuki Kimoto authored on 2011-01-23
849
$dbi->register_filter(five_times => sub { $_[0] * 5 });
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
850
$result = $dbi->select(table => 'table1');
851
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
many changed
Yuki Kimoto authored on 2011-01-23
852
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
853
$row = $result->fetch_hash_first;
854
is_deeply($row, {key1 => 6, key2 => 40});
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
855

            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
856
$dbi->register_filter(five_times => sub { $_[0] * 5 });
857
$dbi->apply_filter('table1',
858
    key1 => {end => sub { $_[0] * 3 } },
859
    key2 => {end => 'five_times'}
860
);
861
$result = $dbi->select(table => 'table1');
862
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
863
$row = $result->fetch_hash_first;
864
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
865

            
866
$dbi->register_filter(five_times => sub { $_[0] * 5 });
867
$dbi->apply_filter('table1',
868
    key1 => {end => sub { $_[0] * 3 } },
869
    key2 => {end => 'five_times'}
870
);
871
$result = $dbi->select(table => 'table1');
872
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
873
$result->filter(key1 => undef);
874
$result->end_filter(key1 => undef);
875
$row = $result->fetch_hash_first;
876
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
877

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
878
test 'remove_end_filter';
879
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
880
$dbi->execute($CREATE_TABLE->{0});
881
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
882
$result = $dbi->select(table => 'table1');
883
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
884
$row = $result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
885
       ->remove_end_filter
886
       ->fetch_first;
887
is_deeply($row, [2, 8]);
888

            
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
889
test 'empty where select';
890
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
891
$dbi->execute($CREATE_TABLE->{0});
892
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
893
$result = $dbi->select(table => 'table1', where => {});
894
$row = $result->fetch_hash_first;
895
is_deeply($row, {key1 => 1, key2 => 2});
896

            
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
897
test 'select query option';
898
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
899
$dbi->execute($CREATE_TABLE->{0});
900
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
901
is(ref $query, 'DBIx::Custom::Query');
902
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
903
is(ref $query, 'DBIx::Custom::Query');
904
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
905
is(ref $query, 'DBIx::Custom::Query');
906
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
907
is(ref $query, 'DBIx::Custom::Query');
908

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
909
test 'DBIx::Custom::Where';
experimental extended select...
Yuki Kimoto authored on 2011-01-17
910
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
911
$dbi->execute($CREATE_TABLE->{0});
912
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
913
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
914
$where = $dbi->where->clause(['and', '{= key1}', '{= key2}']);
915
is("$where", "where ( {= key1} and {= key2} )", 'no param');
916

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
917
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
918
             ->clause(['and', '{= key1}', '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
919
             ->param({key1 => 1});
added test
Yuki Kimoto authored on 2011-01-19
920

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
921
$result = $dbi->select(
922
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
923
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
924
);
925
$row = $result->fetch_hash_all;
926
is_deeply($row, [{key1 => 1, key2 => 2}]);
927

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
928
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
929
             ->clause(['and', '{= key1}', '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
930
             ->param({key1 => 1, key2 => 2});
experimental extended select...
Yuki Kimoto authored on 2011-01-17
931
$result = $dbi->select(
932
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
933
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
934
);
935
$row = $result->fetch_hash_all;
936
is_deeply($row, [{key1 => 1, key2 => 2}]);
937

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
938
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
939
             ->clause(['and', '{= key1}', '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
940
             ->param({});
experimental extended select...
Yuki Kimoto authored on 2011-01-17
941
$result = $dbi->select(
942
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
943
    where => $where,
experimental extended select...
Yuki Kimoto authored on 2011-01-17
944
);
945
$row = $result->fetch_hash_all;
946
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
947

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
948
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
949
             ->clause(['and', ['or', '{> key1}', '{< key1}'], '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
950
             ->param({key1 => [0, 3], key2 => 2});
experimental extended select...
Yuki Kimoto authored on 2011-01-17
951
$result = $dbi->select(
952
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
953
    where => $where,
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
954
); 
experimental extended select...
Yuki Kimoto authored on 2011-01-17
955
$row = $result->fetch_hash_all;
956
is_deeply($row, [{key1 => 1, key2 => 2}]);
957

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
958
$where = $dbi->where;
959
$result = $dbi->select(
960
    table => 'table1',
961
    where => $where
962
);
963
$row = $result->fetch_hash_all;
964
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
965

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
966
eval {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
967
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
968
             ->clause(['uuu']);
experimental extended select...
Yuki Kimoto authored on 2011-01-17
969
$result = $dbi->select(
970
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
971
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
972
);
973
};
974
ok($@);
975

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
976
$where = $dbi->where;
977
is("$where", '');
978

            
added test
Yuki Kimoto authored on 2011-01-19
979
$where = $dbi->where
980
             ->clause(['or', ('{= key1}') x 2])
981
             ->param({key1 => [1, 3]});
982
$result = $dbi->select(
983
    table => 'table1',
984
    where => $where,
985
);
986
$row = $result->fetch_hash_all;
987
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
988

            
989
$where = $dbi->where
990
             ->clause(['or', ('{= key1}') x 2])
991
             ->param({key1 => [1]});
992
$result = $dbi->select(
993
    table => 'table1',
994
    where => $where,
995
);
996
$row = $result->fetch_hash_all;
997
is_deeply($row, [{key1 => 1, key2 => 2}]);
998

            
999
$where = $dbi->where
1000
             ->clause(['or', ('{= key1}') x 2])
1001
             ->param({key1 => 1});
1002
$result = $dbi->select(
1003
    table => 'table1',
1004
    where => $where,
1005
);
1006
$row = $result->fetch_hash_all;
1007
is_deeply($row, [{key1 => 1, key2 => 2}]);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1008

            
many changed
Yuki Kimoto authored on 2011-01-23
1009
$where = $dbi->where
1010
             ->clause('{= key1}')
1011
             ->param({key1 => 1});
1012
$result = $dbi->select(
1013
    table => 'table1',
1014
    where => $where,
1015
);
1016
$row = $result->fetch_hash_all;
1017
is_deeply($row, [{key1 => 1, key2 => 2}]);
1018

            
1019
$where = $dbi->where
1020
             ->clause('{= key1} {= key2}')
1021
             ->param({key1 => 1});
1022
eval{$where->to_string};
1023
like($@, qr/one column/);
1024

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1025
$where = $dbi->where
1026
             ->clause('{= key1}')
1027
             ->param([]);
1028
eval{$where->to_string};
1029
like($@, qr/Parameter/);
1030

            
1031
$where = $dbi->where
1032
             ->clause(['or', ('{= key1}') x 3])
1033
             ->param({key1 => [$dbi->not_exists, 1, 3]});
1034
$result = $dbi->select(
1035
    table => 'table1',
1036
    where => $where,
1037
);
1038
$row = $result->fetch_hash_all;
1039
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1040

            
1041
$where = $dbi->where
1042
             ->clause(['or', ('{= key1}') x 3])
1043
             ->param({key1 => [1, $dbi->not_exists, 3]});
1044
$result = $dbi->select(
1045
    table => 'table1',
1046
    where => $where,
1047
);
1048
$row = $result->fetch_hash_all;
1049
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1050

            
1051
$where = $dbi->where
1052
             ->clause(['or', ('{= key1}') x 3])
1053
             ->param({key1 => [1, 3, $dbi->not_exists]});
1054
$result = $dbi->select(
1055
    table => 'table1',
1056
    where => $where,
1057
);
1058
$row = $result->fetch_hash_all;
1059
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1060

            
1061
$where = $dbi->where
1062
             ->clause(['or', ('{= key1}') x 3])
1063
             ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
1064
$result = $dbi->select(
1065
    table => 'table1',
1066
    where => $where,
1067
);
1068
$row = $result->fetch_hash_all;
1069
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1070

            
1071
$where = $dbi->where
1072
             ->clause(['or', ('{= key1}') x 3])
1073
             ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
1074
$result = $dbi->select(
1075
    table => 'table1',
1076
    where => $where,
1077
);
1078
$row = $result->fetch_hash_all;
1079
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1080

            
1081
$where = $dbi->where
1082
             ->clause(['or', ('{= key1}') x 3])
1083
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
1084
$result = $dbi->select(
1085
    table => 'table1',
1086
    where => $where,
1087
);
1088
$row = $result->fetch_hash_all;
1089
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1090

            
1091
$where = $dbi->where
1092
             ->clause(['or', ('{= key1}') x 3])
1093
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
1094
$result = $dbi->select(
1095
    table => 'table1',
1096
    where => $where,
1097
);
1098
$row = $result->fetch_hash_all;
1099
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1100

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
1101
$where = $dbi->where
1102
             ->clause(['or', ('{= key1}') x 3])
1103
             ->param({key1 => []});
1104
$result = $dbi->select(
1105
    table => 'table1',
1106
    where => $where,
1107
);
1108
$row = $result->fetch_hash_all;
1109
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
1110

            
1111
$where = $dbi->where
1112
             ->clause(['and', '{> key1}', '{< key1}' ])
1113
             ->param({key1 => [2, $dbi->not_exists]});
1114
$result = $dbi->select(
1115
    table => 'table1',
1116
    where => $where,
1117
);
1118
$row = $result->fetch_hash_all;
1119
is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
1120

            
1121
$where = $dbi->where
1122
             ->clause(['and', '{> key1}', '{< key1}' ])
1123
             ->param({key1 => [$dbi->not_exists, 2]});
1124
$result = $dbi->select(
1125
    table => 'table1',
1126
    where => $where,
1127
);
1128
$row = $result->fetch_hash_all;
1129
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1130

            
1131
$where = $dbi->where
1132
             ->clause(['and', '{> key1}', '{< key1}' ])
1133
             ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
1134
$result = $dbi->select(
1135
    table => 'table1',
1136
    where => $where,
1137
);
1138
$row = $result->fetch_hash_all;
1139
is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
1140

            
1141
$where = $dbi->where
1142
             ->clause(['and', '{> key1}', '{< key1}' ])
1143
             ->param({key1 => [0, 2]});
1144
$result = $dbi->select(
1145
    table => 'table1',
1146
    where => $where,
1147
);
1148
$row = $result->fetch_hash_all;
1149
is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
1150

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1151
test 'dbi_option default';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1152
$dbi = DBIx::Custom->new;
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1153
is_deeply($dbi->dbi_option, {});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1154

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1155
test 'register_tag_processor';
1156
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1157
$dbi->register_tag_processor(
1158
    a => sub { 1 }
1159
);
1160
is($dbi->query_builder->tag_processors->{a}->(), 1);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1161

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1162
test 'register_tag';
1163
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1164
$dbi->register_tag(
1165
    b => sub { 2 }
1166
);
1167
is($dbi->query_builder->tags->{b}->(), 2);
1168

            
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
1169
test 'table not specify exception';
1170
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1171
eval {$dbi->insert};
1172
like($@, qr/table/);
1173
eval {$dbi->update};
1174
like($@, qr/table/);
1175
eval {$dbi->delete};
1176
like($@, qr/table/);
1177
eval {$dbi->select};
1178
like($@, qr/table/);
many changed
Yuki Kimoto authored on 2011-01-23
1179

            
1180

            
1181
test 'more tests';
1182
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1183
eval{$dbi->apply_filter('table', 'column', [])};
1184
like($@, qr/apply_filter/);
1185

            
1186
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1187
like($@, qr/apply_filter/);
1188

            
1189
$dbi->apply_filter(
1190

            
1191
);
1192
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1193
$dbi->execute($CREATE_TABLE->{0});
1194
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1195
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1196
$dbi->apply_filter('table1', 'key2', 
1197
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
1198
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->fetch_hash_all;
1199
is_deeply($rows, [{key1 => 1, key2 => 6}]);
1200

            
1201
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1202
$dbi->execute($CREATE_TABLE->{0});
1203
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1204
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
1205
$dbi->apply_filter('table1', 'key2', {});
1206
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->fetch_hash_all;
1207
is_deeply($rows, [{key1 => 1, key2 => 2}]);
1208

            
1209
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1210
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
1211
like($@, qr/not registered/);
1212
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
1213
like($@, qr/not registered/);
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
1214
$dbi->method({one => sub { 1 }});
many changed
Yuki Kimoto authored on 2011-01-23
1215
is($dbi->one, 1);
1216

            
1217
eval{DBIx::Custom->connect()};
1218
like($@, qr/connect/);
1219

            
1220
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1221
$dbi->execute($CREATE_TABLE->{0});
1222
$dbi->register_filter(twice => sub { $_[0] * 2 });
1223
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1224
             filter => {key1 => 'twice'});
1225
$row = $dbi->select(table => 'table1')->fetch_hash_first;
1226
is_deeply($row, {key1 => 2, key2 => 2});
1227
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1228
             filter => {key1 => 'no'}) };
1229
like($@, qr//);
1230

            
1231
$dbi->register_filter(one => sub { });
1232
$dbi->default_fetch_filter('one');
1233
ok($dbi->default_fetch_filter);
1234
$dbi->default_bind_filter('one');
1235
ok($dbi->default_bind_filter);
1236
eval{$dbi->default_fetch_filter('no')};
1237
like($@, qr/not registered/);
1238
eval{$dbi->default_bind_filter('no')};
1239
like($@, qr/not registered/);
1240
$dbi->default_bind_filter(undef);
1241
ok(!defined $dbi->default_bind_filter);
1242
$dbi->default_fetch_filter(undef);
1243
ok(!defined $dbi->default_fetch_filter);
1244
eval {$dbi->execute('select * from table1 {= author') };
1245
like($@, qr/Tag not finished/);
1246

            
1247
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1248
$dbi->execute($CREATE_TABLE->{0});
1249
$dbi->register_filter(one => sub { 1 });
1250
$result = $dbi->select(table => 'table1');
1251
eval {$result->filter(key1 => 'no')};
1252
like($@, qr/not registered/);
1253
eval {$result->end_filter(key1 => 'no')};
1254
like($@, qr/not registered/);
1255
$result->default_filter(undef);
1256
ok(!defined $result->default_filter);
1257
$result->default_filter('one');
1258
is($result->default_filter->(), 1);
1259

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1260
test 'dbi_option';
1261
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:',
1262
                             dbi_option => {PrintError => 1});
1263
ok($dbi->dbh->{PrintError});
1264
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:',
1265
                             dbi_options => {PrintError => 1});
1266
ok($dbi->dbh->{PrintError});
1267

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
1268
test 'DBIx::Custom::Result stash()';
1269
$result = DBIx::Custom::Result->new;
1270
is_deeply($result->stash, {}, 'default');
1271
$result->stash->{foo} = 1;
1272
is($result->stash->{foo}, 1, 'get and set');
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
1273

            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1274
test 'filter __ expression';
1275
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1276
$dbi->execute('create table company (id, name, location_id)');
1277
$dbi->execute('create table location (id, name)');
1278
$dbi->apply_filter('location',
1279
  name => {in => sub { uc $_[0] } }
1280
);
1281

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

            
1285
$result = $dbi->select(
1286
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1287
    column => ['location.name as location__name']
1288
);
1289
is($result->fetch_first->[0], 'B');
1290

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1291
$result = $dbi->select(
1292
    table => 'company', relation => {'company.location_id' => 'location.id'},
1293
    column => ['location.name as location__name']
1294
);
1295
is($result->fetch_first->[0], 'B');
1296

            
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1297
test 'selection';
1298
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1299
$dbi->execute($CREATE_TABLE->{0});
1300
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1301
$result = $dbi->select(selection => '* from table1', where => {key1 => 1});
1302
is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}]);
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1303

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1304
test 'Model class';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1305
use MyDBI1;
1306
$dbi = MyDBI1->connect($NEW_ARGS->{0});
1307
$dbi->execute("create table book (title, author)");
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1308
$model = $dbi->model('book');
1309
$model->insert({title => 'a', author => 'b'});
1310
is_deeply($model->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1311
$dbi->execute("create table company (name)");
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1312
$model = $dbi->model('company');
1313
$model->insert({name => 'a'});
1314
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'basic');
add models() attribute
Yuki Kimoto authored on 2011-02-21
1315
is($dbi->models->{'book'}, $dbi->model('book'));
1316
is($dbi->models->{'company'}, $dbi->model('company'));
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1317

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1318
$dbi->model('book');
1319
eval{$dbi->model('book')->no_exists};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1320
like($@, qr/locate/);
1321

            
1322
{
1323
    package MyDBI4;
1324

            
1325
    use strict;
1326
    use warnings;
1327

            
1328
    use base 'DBIx::Custom';
1329

            
1330
    sub connect {
1331
        my $self = shift->SUPER::connect(@_);
1332
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1333
        $self->include_model(
1334
            MyModel2 => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1335
                'book',
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1336
                {class => 'Company', name => 'company'}
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1337
            ]
1338
        );
1339
    }
1340

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1341
    package MyModel2::Base1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1342

            
1343
    use strict;
1344
    use warnings;
1345

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1346
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1347

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1348
    package MyModel2::book;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1349

            
1350
    use strict;
1351
    use warnings;
1352

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1353
    use base 'MyModel2::Base1';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1354

            
1355
    sub insert {
1356
        my ($self, $param) = @_;
1357
        
1358
        return $self->SUPER::insert(param => $param);
1359
    }
1360

            
1361
    sub list { shift->select; }
1362

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1363
    package MyModel2::Company;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1364

            
1365
    use strict;
1366
    use warnings;
1367

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1368
    use base 'MyModel2::Base1';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1369

            
1370
    sub insert {
1371
        my ($self, $param) = @_;
1372
        
1373
        return $self->SUPER::insert(param => $param);
1374
    }
1375

            
1376
    sub list { shift->select; }
1377
}
1378
$dbi = MyDBI4->connect($NEW_ARGS->{0});
1379
$dbi->execute("create table book (title, author)");
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1380
$model = $dbi->model('book');
1381
$model->insert({title => 'a', author => 'b'});
1382
is_deeply($model->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1383
$dbi->execute("create table company (name)");
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1384
$model = $dbi->model('company');
1385
$model->insert({name => 'a'});
1386
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'basic');
1387

            
1388
{
1389
     package MyDBI5;
1390

            
1391
    use strict;
1392
    use warnings;
1393

            
1394
    use base 'DBIx::Custom';
1395

            
1396
    sub connect {
1397
        my $self = shift->SUPER::connect(@_);
1398
        
1399
        $self->include_model('MyModel4');
1400
    }
1401
}
1402
$dbi = MyDBI5->connect($NEW_ARGS->{0});
1403
$dbi->execute("create table company (name)");
1404
$model = $dbi->model('company');
1405
$model->insert({name => 'a'});
1406
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'include all model');
1407
$model = $dbi->model('book');
1408
is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'include all model');
1409

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
1410
test 'primary_key';
1411
use MyDBI1;
1412
$dbi = MyDBI1->connect($NEW_ARGS->{0});
1413
$model = $dbi->model('book');
1414
$model->primary_key(['id', 'number']);
1415
is_deeply($model->primary_key, ['id', 'number']);
1416

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
1417
test 'columns';
1418
use MyDBI1;
1419
$dbi = MyDBI1->connect($NEW_ARGS->{0});
1420
$model = $dbi->model('book');
1421
$model->columns(['id', 'number']);
1422
is_deeply($model->columns, ['id', 'number']);
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1423

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1424
test 'setup_model';
1425
use MyDBI1;
1426
$dbi = MyDBI1->connect($NEW_ARGS->{0});
1427
$dbi->execute('create table book (id)');
1428
$dbi->execute('create table company (id, name);');
1429
$dbi->execute('create table test (id, name, primary key (id, name));');
1430
$dbi->setup_model;
1431
is_deeply($dbi->model('book')->columns, ['id']);
1432
is_deeply($dbi->model('company')->columns, ['id', 'name']);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1433

            
1434
test 'delete_at';
1435
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1436
$dbi->execute($CREATE_TABLE->{1});
1437
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1438
$dbi->delete_at(
1439
    table => 'table1',
1440
    primary_key => ['key1', 'key2'],
1441
    where => [1, 2],
1442
);
1443
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
1444

            
1445
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1446
$dbi->delete_at(
1447
    table => 'table1',
1448
    primary_key => 'key1',
1449
    where => 1,
1450
);
1451
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
1452

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1453
test 'insert_at';
1454
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1455
$dbi->execute($CREATE_TABLE->{1});
1456
$dbi->insert_at(
1457
    primary_key => ['key1', 'key2'], 
1458
    table => 'table1',
1459
    where => [1, 2],
1460
    param => {key3 => 3}
1461
);
1462
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1463
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1464
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3);
1465

            
1466
$dbi->delete_all(table => 'table1');
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1467
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1468
$dbi->insert_at(
1469
    primary_key => 'key1', 
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1470
    table => 'table1',
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1471
    where => 1,
1472
    param => {key2 => 2, key3 => 3}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1473
);
1474

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1475
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1476
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1477
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 3);
1478

            
1479
eval {
1480
    $dbi->insert_at(
1481
        table => 'table1',
1482
        primary_key => ['key1', 'key2'],
1483
        where => {},
1484
        param => {key1 => 1, key2 => 2, key3 => 3},
1485
    );
1486
};
1487
like($@, qr/must be/);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1488

            
1489
test 'update_at';
1490
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1491
$dbi->execute($CREATE_TABLE->{1});
1492
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1493
$dbi->update_at(
1494
    table => 'table1',
1495
    primary_key => ['key1', 'key2'],
1496
    where => [1, 2],
1497
    param => {key3 => 4}
1498
);
1499
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1500
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1501
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4);
1502

            
1503
$dbi->delete_all(table => 'table1');
1504
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1505
$dbi->update_at(
1506
    table => 'table1',
1507
    primary_key => 'key1',
1508
    where => 1,
1509
    param => {key3 => 4}
1510
);
1511
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
1512
is($dbi->select(table => 'table1')->fetch_hash_first->{key2}, 2);
1513
is($dbi->select(table => 'table1')->fetch_hash_first->{key3}, 4);
1514

            
1515
test 'select_at';
1516
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1517
$dbi->execute($CREATE_TABLE->{1});
1518
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1519
$result = $dbi->select_at(
1520
    table => 'table1',
1521
    primary_key => ['key1', 'key2'],
1522
    where => [1, 2]
1523
);
1524
$row = $result->fetch_hash_first;
1525
is($row->{key1}, 1);
1526
is($row->{key2}, 2);
1527
is($row->{key3}, 3);
1528

            
1529
$dbi->delete_all(table => 'table1');
1530
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1531
$result = $dbi->select_at(
1532
    table => 'table1',
1533
    primary_key => 'key1',
1534
    where => 1,
1535
);
1536
$row = $result->fetch_hash_first;
1537
is($row->{key1}, 1);
1538
is($row->{key2}, 2);
1539
is($row->{key3}, 3);
1540

            
1541
$dbi->delete_all(table => 'table1');
1542
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1543
$result = $dbi->select_at(
1544
    table => 'table1',
1545
    primary_key => ['key1', 'key2'],
1546
    param => {key1 => 1, key2 => 2},
1547
);
1548
$row = $result->fetch_hash_first;
1549
is($row->{key1}, 1);
1550
is($row->{key2}, 2);
1551
is($row->{key3}, 3);
1552

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1553
eval {
1554
    $result = $dbi->select_at(
1555
        table => 'table1',
1556
        primary_key => ['key1', 'key2'],
1557
        where => {},
1558
        param => {key1 => 1, key2 => 2},
1559
    );
1560
};
1561
like($@, qr/must be/);
1562

            
1563
eval {
1564
    $result = $dbi->update_at(
1565
        table => 'table1',
1566
        primary_key => ['key1', 'key2'],
1567
        where => {},
1568
        param => {key1 => 1, key2 => 2},
1569
    );
1570
};
1571
like($@, qr/must be/);
1572

            
1573
eval {
1574
    $result = $dbi->delete_at(
1575
        table => 'table1',
1576
        primary_key => ['key1', 'key2'],
1577
        where => {},
1578
        param => {key1 => 1, key2 => 2},
1579
    );
1580
};
1581
like($@, qr/must be/);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1582

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1583
test 'columns';
1584
use MyDBI1;
1585
$dbi = MyDBI1->connect($NEW_ARGS->{0});
1586
$model = $dbi->model('book');
1587
$model->relation({'book.id' => 'company.id'});
1588
is_deeply($model->relation, {'book.id' => 'company.id'});
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1589

            
1590

            
1591
test 'model delete_at';
1592
{
1593
    package MyDBI6;
1594
    
1595
    use base 'DBIx::Custom';
1596
    
1597
    sub connect {
1598
        my $self = shift->SUPER::connect(@_);
1599
        
1600
        $self->include_model('MyModel5');
1601
        
1602
        return $self;
1603
    }
1604
}
1605
$dbi = MyDBI6->connect($NEW_ARGS->{0});
1606
$dbi->execute($CREATE_TABLE->{1});
1607
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1608
$dbi->model('table1')->delete_at(where => [1, 2]);
1609
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1610
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1611
$dbi->model('table1_1')->delete_at(where => [1, 2]);
1612
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
1613
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1614
$dbi->model('table1_3')->delete_at(where => [1, 2]);
1615
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1616

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1617
test 'model insert_at';
1618
$dbi = MyDBI6->connect($NEW_ARGS->{0});
1619
$dbi->execute($CREATE_TABLE->{1});
1620
$dbi->model('table1')->insert_at(
1621
    where => [1, 2],
1622
    param => {key3 => 3}
1623
);
1624
$result = $dbi->model('table1')->select;
1625
$row = $result->fetch_hash_first;
1626
is($row->{key1}, 1);
1627
is($row->{key2}, 2);
1628
is($row->{key3}, 3);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1629

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1630
test 'model update_at';
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1631
$dbi = MyDBI6->connect($NEW_ARGS->{0});
1632
$dbi->execute($CREATE_TABLE->{1});
1633
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1634
$dbi->model('table1')->update_at(
1635
    where => [1, 2],
1636
    param => {key3 => 4}
1637
);
1638
$result = $dbi->model('table1')->select;
1639
$row = $result->fetch_hash_first;
1640
is($row->{key1}, 1);
1641
is($row->{key2}, 2);
1642
is($row->{key3}, 4);
1643

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1644
test 'model select_at';
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1645
$dbi = MyDBI6->connect($NEW_ARGS->{0});
1646
$dbi->execute($CREATE_TABLE->{1});
1647
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1648
$result = $dbi->model('table1')->select_at(where => [1, 2]);
1649
$row = $result->fetch_hash_first;
1650
is($row->{key1}, 1);
1651
is($row->{key2}, 2);
1652
is($row->{key3}, 3);
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
1653

            
1654

            
1655
test 'model select relation';
1656
{
1657
    package MyDBI7;
1658
    
1659
    use base 'DBIx::Custom';
1660
    
1661
    sub connect {
1662
        my $self = shift->SUPER::connect(@_);
1663
        
1664
        $self->include_model('MyModel6');
1665
        
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
1666
        
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
1667
        return $self;
1668
    }
1669
}
1670
$dbi = MyDBI7->connect($NEW_ARGS->{0});
1671
$dbi->execute($CREATE_TABLE->{0});
1672
$dbi->execute($CREATE_TABLE->{2});
1673
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1674
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1675
$result = $dbi->model('table1')->select(column => ['key3'], where => {'table1.key1' => 1});
1676
is($result->fetch_hash_first->{key3}, 3);
1677
$result = $dbi->model('table1')->select_at(column => ['key3'], where => [1]);
1678
is($result->fetch_hash_first->{key3}, 3);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
1679
$dbi->execute('create table table3 (key1);');
1680
$dbi->model('table3')->insert(param => {key1 => 'a'});
1681
is_deeply($dbi->model('table3')->select(where => {key1 => 'a'})->fetch_hash_first,
1682
   {key1 => 'A'});
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
1683

            
1684
test 'column_clause';
1685
$dbi = MyDBI7->connect($NEW_ARGS->{0});
1686
$dbi->execute($CREATE_TABLE->{0});
1687
$dbi->execute($CREATE_TABLE->{2});
1688
$dbi->setup_model;
1689
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1690
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1691
$model = $dbi->model('table1');
1692
$result = $model->select(column => $model->column_clause, where => {'table1.key1' => 1});
1693
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
1694
$result = $model->select(column => $model->column_clause(remove => ['key1']), where => {'table1.key1' => 1});
1695
is_deeply($result->fetch_hash_first, {key2 => 2});
1696
$result = $model->select(column => $model->column_clause(add => ['key3']), where => {'table1.key1' => 1});
1697
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2, key3 => 3});
1698

            
1699