DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
1319 lines | 45.271kb
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/;
7

            
8
BEGIN {
9
    eval { require DBD::SQLite; 1 }
10
        or plan skip_all => 'DBD::SQLite required';
11
    eval { DBD::SQLite->VERSION >= 1.25 }
12
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
13

            
14
    plan 'no_plan';
15
    use_ok('DBIx::Custom');
16
}
17

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

            
21
# Constant varialbes for test
22
my $CREATE_TABLE = {
23
    0 => 'create table table1 (key1 char(255), key2 char(255));',
24
    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
25
    2 => 'create table table2 (key1 char(255), key3 char(255));',
26
    3 => 'create table table1 (key1 Date, key2 datetime);'
removed register_format()
yuki-kimoto authored on 2010-05-26
27
};
28

            
add tests
yuki-kimoto authored on 2010-08-10
29
my $SELECT_SOURCES = {
removed register_format()
yuki-kimoto authored on 2010-05-26
30
    0 => 'select * from table1;'
31
};
32

            
33
my $DROP_TABLE = {
34
    0 => 'drop table table1'
35
};
36

            
37
my $NEW_ARGS = {
38
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
39
};
40

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

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

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

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

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

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

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

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

            
105

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

            
115
test 'Filter basic';
116
$dbi->execute($DROP_TABLE->{0});
117
$dbi->execute($CREATE_TABLE->{0});
118
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
119
                    three_times => sub { $_[0] * 3});
120

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

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
148
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
149
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
150
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
151
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
152
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
153

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
154
$source = "select * from table1 where {<= key1} and {like key2};";
155
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
156
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
157
$rows = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
158
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
159

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

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

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

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

            
181
test 'DBIx::Custom::SQLTemplate update tag';
182
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
183
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
184
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
185
$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
186

            
add tests
yuki-kimoto authored on 2010-08-10
187
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
188
$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
189

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

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

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

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

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

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

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

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

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

            
add tests
yuki-kimoto authored on 2010-08-10
258
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
259
$result = $dbi->execute($SELECT_SOURCES->{0});
260
$rows   = $result->fetch_hash_all;
261
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
262
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
cleanup
Yuki Kimoto authored on 2011-01-23
263
                  "update key same as search key : param is array ref");
add tests
yuki-kimoto authored on 2010-08-10
264

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

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

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

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

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

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

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

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

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

            
324

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

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

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

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

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

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

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

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

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

            
387

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

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

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

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

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

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

            
414
$dbi->execute($CREATE_TABLE->{2});
415
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
416
$rows = $dbi->select(
417
    table => [qw/table1 table2/],
update document
yuki-kimoto authored on 2010-05-27
418
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
removed register_format()
yuki-kimoto authored on 2010-05-26
419
    where   => {'table1.key2' => 2},
added commit method
yuki-kimoto authored on 2010-05-27
420
    relation  => {'table1.key1' => 'table2.key1'}
removed register_format()
yuki-kimoto authored on 2010-05-26
421
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
422
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
423

            
424
$rows = $dbi->select(
425
    table => [qw/table1 table2/],
426
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
427
    relation  => {'table1.key1' => 'table2.key1'}
428
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
429
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
430

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

            
434

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

            
449
test 'filters';
450
$dbi = DBIx::Custom->new;
451

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

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

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

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

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

            
478
test 'cache';
479
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
480
$dbi->execute($CREATE_TABLE->{0});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
481
$source = 'select * from table1 where {= key1} and {= key2};';
482
$dbi->create_query($source);
483
is_deeply($dbi->{_cached}->{$source}, 
cleanup
Yuki Kimoto authored on 2011-01-23
484
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "cache");
add cache attribute
yuki-kimoto authored on 2010-06-14
485

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

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

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

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

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

            
527

            
528
test 'transaction';
529
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
530
$dbi->execute($CREATE_TABLE->{0});
531

            
532
$dbi->begin_work;
533

            
534
eval {
535
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
536
    die "Error";
537
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
538
};
539

            
540
$dbi->rollback if $@;
541

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

            
546
$dbi->begin_work;
547

            
548
eval {
549
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
550
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
551
};
552

            
553
$dbi->commit unless $@;
554

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

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

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

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

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

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

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

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

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

            
630
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
631
$dbi->execute($CREATE_TABLE->{0});
632
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
633
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
634
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
635
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
636
$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
637
$dbi->delete(table => 'table1', where => {key1 => 1});
638
$result = $dbi->execute($SELECT_SOURCES->{0});
639
$rows   = $result->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
640
is_deeply($rows, [], "delete");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
641

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

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

            
667
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
668
$dbi->execute($CREATE_TABLE->{0});
669
$dbi->execute($CREATE_TABLE->{2});
670
$dbi->register_filter(twice => sub { $_[0] * 2 });
671
$dbi->register_filter(three_times => sub { $_[0] * 3 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
672
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
673
    'table1', 'key2' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
674
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
675
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
676
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
677
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
678
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
679
$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
680
$result = $dbi->select(
681
     table => ['table1', 'table2'],
682
     column => ['key2', 'key3'],
683
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
684

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

            
689
$result = $dbi->select(
690
     table => ['table1', 'table2'],
691
     column => ['key2', 'key3'],
692
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
693

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
698
test 'each_column';
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
699
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
700
$dbi->execute($CREATE_TABLE->{2});
701
$dbi->execute($CREATE_TABLE->{3});
702

            
703
$infos = [];
pod fix
Yuki Kimoto authored on 2011-01-21
704
$dbi->each_column(sub {
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
705
    my ($self, $table, $column, $cinfo) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
706
    
707
    if ($table =~ /^table/) {
708
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
709
         push @$infos, $info;
710
    }
711
});
712
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
713
is_deeply($infos, 
714
    [
715
        ['table1', 'key1', 'key1'],
716
        ['table1', 'key2', 'key2'],
717
        ['table2', 'key1', 'key1'],
718
        ['table2', 'key3', 'key3']
719
    ]
cleanup
Yuki Kimoto authored on 2011-01-23
720
    
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
721
);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
722

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
723
test 'table';
724
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
725
$dbi->execute($CREATE_TABLE->{0});
726
$table = $dbi->table('table1');
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
727
$table->insert(param => {key1 => 1, key2 => 2});
728
$table->insert(param => {key1 => 3, key2 => 4});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
729
$rows = $table->select->fetch_hash_all;
730
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}],
cleanup
Yuki Kimoto authored on 2011-01-23
731
                 "select");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
732
$rows = $table->select(where => {key2 => 2}, append => 'order by key1',
733
                              column => ['key1', 'key2'])->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
734
is_deeply($rows, [{key1 => 1, key2 => 2}],
cleanup
Yuki Kimoto authored on 2011-01-23
735
                 "insert insert select");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
736
$table->update(param => {key1 => 3}, where => {key2 => 2});
737
$table->update(param => {key1 => 5}, where => {key2 => 4});
738
$rows = $table->select(where => {key2 => 2})->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
739
is_deeply($rows, [{key1 => 3, key2 => 2}],
cleanup
Yuki Kimoto authored on 2011-01-23
740
                 "update");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
741
$table->delete(where => {key2 => 2});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
742
$rows = $table->select->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
743
is_deeply($rows, [{key1 => 5, key2 => 4}], "delete");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
744
$table->update_all(param => {key1 => 3});
745
$rows = $table->select->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
746
is_deeply($rows, [{key1 => 3, key2 => 4}], "update_all");
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
747
$table->delete_all;
748
$rows = $table->select->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
749
is_deeply($rows, [], "delete_all");
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
750

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
751
$dbi->dbh->do($CREATE_TABLE->{2});
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
752
$dbi->table('table2')->method(
753
    ppp => sub {
754
        my $self = shift;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
755
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
756
        return $self->name;
757
    }
758
);
759
is($dbi->table('table2')->ppp, 'table2', "method");
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
760

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
761
$dbi->table('table2')->method({
762
    qqq => sub {
763
        my $self = shift;
many changed
Yuki Kimoto authored on 2011-01-23
764
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
765
        return $self->name;
766
    }
767
});
768
is($dbi->table('table2')->qqq, 'table2', "method");
many changed
Yuki Kimoto authored on 2011-01-23
769

            
770

            
add examples
Yuki Kimoto authored on 2011-01-07
771
test 'limit';
772
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
773
$dbi->execute($CREATE_TABLE->{0});
774
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
775
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
776
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
777
$dbi->register_tag(
add examples
Yuki Kimoto authored on 2011-01-07
778
    limit => sub {
779
        my ($count, $offset) = @_;
780
        
781
        my $s = '';
782
        $s .= "limit $count";
783
        $s .= " offset $offset" if defined $offset;
784
        
785
        return [$s, []];
786
    }
787
);
788
$rows = $dbi->select(
789
  table => 'table1',
790
  where => {key1 => 1},
791
  append => "order by key2 {limit 1 0}"
792
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
793
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
794
$rows = $dbi->select(
795
  table => 'table1',
796
  where => {key1 => 1},
797
  append => "order by key2 {limit 2 1}"
798
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
799
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
add examples
Yuki Kimoto authored on 2011-01-07
800
$rows = $dbi->select(
801
  table => 'table1',
802
  where => {key1 => 1},
803
  append => "order by key2 {limit 1}"
804
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
805
is_deeply($rows, [{key1 => 1, key2 => 2}]);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
806

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
807
test 'connect super';
808
{
809
    package MyDBI;
810
    
811
    use base 'DBIx::Custom';
812
    sub connect {
813
        my $self = shift->SUPER::connect(@_);
814
        
815
        return $self;
816
    }
817
    
818
    sub new {
cleanup
Yuki Kimoto authored on 2011-01-25
819
        my $self = shift->SUPER::new(@_);
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
820
        
821
        return $self;
822
    }
823
}
824

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
836
{
837
    package MyDBI2;
838
    
839
    use base 'DBIx::Custom';
840
    sub connect {
841
        my $self = shift->SUPER::new(@_);
842
        $self->connect;
843
        
844
        return $self;
845
    }
846
}
847

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

            
853
test 'end_filter';
854
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
855
$dbi->execute($CREATE_TABLE->{0});
856
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
857
$result = $dbi->select(table => 'table1');
858
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
859
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
860
$row = $result->fetch_first;
861
is_deeply($row, [6, 40]);
862

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

            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
870
$dbi->register_filter(five_times => sub { $_[0] * 5 });
871
$dbi->apply_filter('table1',
872
    key1 => {end => sub { $_[0] * 3 } },
873
    key2 => {end => 'five_times'}
874
);
875
$result = $dbi->select(table => 'table1');
876
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
877
$row = $result->fetch_hash_first;
878
is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
879

            
880
$dbi->register_filter(five_times => sub { $_[0] * 5 });
881
$dbi->apply_filter('table1',
882
    key1 => {end => sub { $_[0] * 3 } },
883
    key2 => {end => 'five_times'}
884
);
885
$result = $dbi->select(table => 'table1');
886
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
887
$result->filter(key1 => undef);
888
$result->end_filter(key1 => undef);
889
$row = $result->fetch_hash_first;
890
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
891

            
892
test 'empty where select';
893
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
894
$dbi->execute($CREATE_TABLE->{0});
895
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
896
$result = $dbi->select(table => 'table1', where => {});
897
$row = $result->fetch_hash_first;
898
is_deeply($row, {key1 => 1, key2 => 2});
899

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1183

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

            
1189
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
1190
like($@, qr/apply_filter/);
1191

            
1192
$dbi->apply_filter(
1193

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

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

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

            
1220
eval{DBIx::Custom->connect()};
1221
like($@, qr/connect/);
1222

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

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

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

            
1263
$dbi->table('book');
1264
eval{$dbi->table('book')->no_exists};
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1265
like($@, qr/locate/);
1266

            
1267
test 'dbi_option';
1268
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:',
1269
                             dbi_option => {PrintError => 1});
1270
ok($dbi->dbh->{PrintError});
1271
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:',
1272
                             dbi_options => {PrintError => 1});
1273
ok($dbi->dbh->{PrintError});
1274

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
1275
test 'DBIx::Custom::Result stash()';
1276
$result = DBIx::Custom::Result->new;
1277
is_deeply($result->stash, {}, 'default');
1278
$result->stash->{foo} = 1;
1279
is($result->stash->{foo}, 1, 'get and set');
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
1280

            
1281
test 'base_table';
1282
$dbi = DBIx::Custom->new;
1283
$dbi->base_table->method(
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
1284
    twice => sub {
1285
        my $self = shift;
1286
        
1287
        return $_[0] * 2;
1288
    }
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
1289
);
1290
$table = $dbi->table('book');
1291
$table->method(
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
1292
    three_times => sub {
1293
        my $self = shift;
1294
        return  $_[0] * 3;
1295
    }
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
1296
);
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
1297
is($table->base_twice(1), 2, 'method');
1298
is($table->twice(1), 2, 'inherit method');
1299
is($table->three_times(1), 3, 'child table method');
1300
eval {$dbi->base_two};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
1301
ok($@);
1302

            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1303
test 'filter __ expression';
1304
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1305
$dbi->execute('create table company (id, name, location_id)');
1306
$dbi->execute('create table location (id, name)');
1307
$dbi->apply_filter('location',
1308
  name => {in => sub { uc $_[0] } }
1309
);
1310

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

            
1314
$result = $dbi->select(
1315
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
1316
    column => ['location.name as location__name']
1317
);
1318
is($result->fetch_first->[0], 'B');
1319