DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
801 lines | 29.907kb
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
19
my $test;
20
sub test {
21
    $test = shift;
22
}
23

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

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

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

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

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

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

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

            
78
@rows = ();
79
while (my $row = $result->fetch) {
80
    push @rows, [@$row];
81
}
removed reconnect method
yuki-kimoto authored on 2010-05-28
82
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch");
removed register_format()
yuki-kimoto authored on 2010-05-26
83

            
84
$result = $dbi->execute($query);
85
@rows = ();
86
while (my $row = $result->fetch_hash) {
87
    push @rows, {%$row};
88
}
removed reconnect method
yuki-kimoto authored on 2010-05-28
89
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash");
removed register_format()
yuki-kimoto authored on 2010-05-26
90

            
91
$result = $dbi->execute($query);
92
$rows = $result->fetch_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
93
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all");
removed register_format()
yuki-kimoto authored on 2010-05-26
94

            
95
$result = $dbi->execute($query);
removed reconnect method
yuki-kimoto authored on 2010-05-28
96
$rows = $result->fetch_hash_all;
97
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash_all");
removed register_format()
yuki-kimoto authored on 2010-05-26
98

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

            
107

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

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

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

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

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

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

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

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

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

            
174
test 'DBIx::Custom::SQLTemplate insert tag';
175
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
176
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
177
$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
178

            
add tests
yuki-kimoto authored on 2010-08-10
179
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
180
$rows = $result->fetch_hash_all;
181
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
182

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

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

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

            
197
test 'Error case';
198
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')};
199
ok($@, "$test : connect error");
200

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

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

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

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

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
232
eval{$dbi->insert(table => 'table1', noexist => 1)};
add tests
yuki-kimoto authored on 2010-08-10
233
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
234

            
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}],
246
                  "$test : basic");
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}],
256
                  "$test : update key same as search key");
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}],
263
                  "$test : update key same as search key : param is array ref");
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},
270
              filter => {key2 => 'twice'});
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}],
275
                  "$test : filter");
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)};
add tests
yuki-kimoto authored on 2010-08-10
280
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
281

            
282
eval{$dbi->update(table => 'table1')};
283
like($@, qr/where/, "$test: not contain where");
284

            
285

            
removed register_format()
yuki-kimoto authored on 2010-05-26
286
test 'update_all';
287
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
288
$dbi->execute($CREATE_TABLE->{1});
289
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
290
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
291
$dbi->register_filter(twice => sub { $_[0] * 2 });
292
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
add tests
yuki-kimoto authored on 2010-08-10
293
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
294
$rows   = $result->fetch_hash_all;
295
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
296
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
297
                  "$test : filter");
298

            
299

            
300
test 'delete';
301
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
302
$dbi->execute($CREATE_TABLE->{0});
303
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
304
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
305
$dbi->delete(table => 'table1', where => {key1 => 1});
add tests
yuki-kimoto authored on 2010-08-10
306
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
307
$rows   = $result->fetch_hash_all;
308
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
309

            
310
$dbi->execute("delete from table1;");
311
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
312
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
313
$dbi->register_filter(twice => sub { $_[0] * 2 });
314
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
add tests
yuki-kimoto authored on 2010-08-10
315
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
316
$rows   = $result->fetch_hash_all;
317
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter");
318

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

            
321
$dbi->delete_all(table => 'table1');
322
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
323
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
324
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
325
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
326
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
327

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
328
eval{$dbi->delete(table => 'table1', noexist => 1)};
add tests
yuki-kimoto authored on 2010-08-10
329
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
330

            
331

            
removed register_format()
yuki-kimoto authored on 2010-05-26
332
test 'delete error';
333
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
334
$dbi->execute($CREATE_TABLE->{0});
335
eval{$dbi->delete(table => 'table1')};
add tests
yuki-kimoto authored on 2010-08-10
336
like($@, qr/"where" argument must be specified and contains the pairs of column name and value/,
removed register_format()
yuki-kimoto authored on 2010-05-26
337
         "$test : where key-value pairs not specified");
338

            
339
test 'delete_all';
340
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
341
$dbi->execute($CREATE_TABLE->{0});
342
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
343
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
344
$dbi->delete_all(table => 'table1');
add tests
yuki-kimoto authored on 2010-08-10
345
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
346
$rows   = $result->fetch_hash_all;
347
is_deeply($rows, [], "$test : basic");
348

            
349

            
350
test 'select';
351
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
352
$dbi->execute($CREATE_TABLE->{0});
353
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
354
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
355
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
356
is_deeply($rows, [{key1 => 1, key2 => 2},
357
                  {key1 => 3, key2 => 4}], "$test : table");
358

            
update document
yuki-kimoto authored on 2010-05-27
359
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all;
removed register_format()
yuki-kimoto authored on 2010-05-26
360
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
361

            
362
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all;
363
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key");
364

            
update document
yuki-kimoto authored on 2010-08-07
365
$rows = $dbi->select(table => 'table1', where => ['{= key1} and {= key2}', {key1 => 1, key2 => 2}])->fetch_hash_all;
366
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where string");
367

            
update document
yuki-kimoto authored on 2010-05-27
368
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all;
removed register_format()
yuki-kimoto authored on 2010-05-26
369
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
370

            
371
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all;
372
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
373

            
374
$dbi->register_filter(decrement => sub { $_[0] - 1 });
update document
yuki-kimoto authored on 2010-05-27
375
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
removed register_format()
yuki-kimoto authored on 2010-05-26
376
            ->fetch_hash_all;
377
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter");
378

            
379
$dbi->execute($CREATE_TABLE->{2});
380
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
381
$rows = $dbi->select(
382
    table => [qw/table1 table2/],
update document
yuki-kimoto authored on 2010-05-27
383
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
removed register_format()
yuki-kimoto authored on 2010-05-26
384
    where   => {'table1.key2' => 2},
added commit method
yuki-kimoto authored on 2010-05-27
385
    relation  => {'table1.key1' => 'table2.key1'}
removed register_format()
yuki-kimoto authored on 2010-05-26
386
)->fetch_hash_all;
added commit method
yuki-kimoto authored on 2010-05-27
387
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : exists where");
388

            
389
$rows = $dbi->select(
390
    table => [qw/table1 table2/],
391
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
392
    relation  => {'table1.key1' => 'table2.key1'}
393
)->fetch_hash_all;
394
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : no exists where");
removed register_format()
yuki-kimoto authored on 2010-05-26
395

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
396
eval{$dbi->select(table => 'table1', noexist => 1)};
add tests
yuki-kimoto authored on 2010-08-10
397
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
398

            
399

            
removed register_format()
yuki-kimoto authored on 2010-05-26
400
test 'fetch filter';
401
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
402
$dbi->register_filter(
403
    twice       => sub { $_[0] * 2 },
404
    three_times => sub { $_[0] * 3 }
405
);
406
$dbi->default_fetch_filter('twice');
407
$dbi->execute($CREATE_TABLE->{0});
408
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
409
$result = $dbi->select(table => 'table1');
410
$result->filter({key1 => 'three_times'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
411
$row = $result->fetch_hash_first;
removed register_format()
yuki-kimoto authored on 2010-05-26
412
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter");
413

            
414
test 'filters';
415
$dbi = DBIx::Custom->new;
416

            
update document
yuki-kimoto authored on 2010-05-27
417
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
418
   'あ', "$test : decode_utf8");
removed register_format()
yuki-kimoto authored on 2010-05-26
419

            
420
is($dbi->filters->{encode_utf8}->('あ'),
421
   encode_utf8('あ'), "$test : encode_utf8");
422

            
added commit method
yuki-kimoto authored on 2010-05-27
423
test 'transaction';
424
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
425
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
426
$dbi->dbh->begin_work;
added commit method
yuki-kimoto authored on 2010-05-27
427
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
428
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
429
$dbi->dbh->commit;
added commit method
yuki-kimoto authored on 2010-05-27
430
$result = $dbi->select(table => 'table1');
431
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
432
          "$test : commit");
433

            
434
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
435
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
436
$dbi->dbh->begin_work(0);
added commit method
yuki-kimoto authored on 2010-05-27
437
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
438
$dbi->dbh->rollback;
added commit method
yuki-kimoto authored on 2010-05-27
439

            
440
$result = $dbi->select(table => 'table1');
removed reconnect method
yuki-kimoto authored on 2010-05-28
441
ok(! $result->fetch_first, "$test: rollback");
add cache attribute
yuki-kimoto authored on 2010-06-14
442

            
443
test 'cache';
444
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
445
$dbi->execute($CREATE_TABLE->{0});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
446
$source = 'select * from table1 where {= key1} and {= key2};';
447
$dbi->create_query($source);
448
is_deeply($dbi->{_cached}->{$source}, 
add cache attribute
yuki-kimoto authored on 2010-06-14
449
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "$test : cache");
450

            
451
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
452
$dbi->execute($CREATE_TABLE->{0});
453
$dbi->{_cached} = {};
454
$dbi->cache(0);
455
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
456
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
457

            
add tests
yuki-kimoto authored on 2010-08-10
458
test 'execute';
459
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
460
$dbi->execute($CREATE_TABLE->{0});
removed experimental registe...
yuki-kimoto authored on 2010-08-24
461
{
462
    local $Carp::Verbose = 0;
463
    eval{$dbi->execute('select * frm table1')};
464
    like($@, qr/\Qselect * frm table1;/, "$test : fail prepare");
465
    like($@, qr/\.t /, "$test: fail : not verbose");
466
}
467
{
468
    local $Carp::Verbose = 1;
469
    eval{$dbi->execute('select * frm table1')};
470
    like($@, qr/Custom.*\.t /s, "$test : fail : verbose");
471
}
add tests
yuki-kimoto authored on 2010-08-10
472

            
473
eval{$dbi->execute('select * from table1', no_exists => 1)};
474
like($@, qr/\Q"no_exists" is invalid argument/, "$test : invald SQL");
475

            
476
$query = $dbi->create_query('select * from table1 where {= key1}');
477
$dbi->dbh->disconnect;
478
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
479
ok($@, "$test: execute fail");
480

            
removed experimental registe...
yuki-kimoto authored on 2010-08-24
481
{
482
    local $Carp::Verbose = 0;
483
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
484
    like($@, qr/\Q.t /, "$test : caller spec : not vebose");
485
}
486
{
487
    local $Carp::Verbose = 1;
488
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
489
    like($@, qr/QueryBuilder.*\.t /s, "$test : caller spec : not vebose");
490
}
cleanup
yuki-kimoto authored on 2010-10-17
491

            
492

            
493
test 'transaction';
494
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
495
$dbi->execute($CREATE_TABLE->{0});
496

            
497
$dbi->begin_work;
498

            
499
eval {
500
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
501
    die "Error";
502
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
503
};
504

            
505
$dbi->rollback if $@;
506

            
507
$result = $dbi->select(table => 'table1');
508
$rows = $result->fetch_hash_all;
509
is_deeply($rows, [], "$test : rollback");
510

            
511
$dbi->begin_work;
512

            
513
eval {
514
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
515
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
516
};
517

            
518
$dbi->commit unless $@;
519

            
520
$result = $dbi->select(table => 'table1');
521
$rows = $result->fetch_hash_all;
522
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
523

            
524
$dbi->dbh->{AutoCommit} = 0;
525
eval{ $dbi->begin_work };
526
ok($@, "$test : exception");
527
$dbi->dbh->{AutoCommit} = 1;
528

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

            
530
test 'helper';
531
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
532
$dbi->helper(
533
    one => sub { 1 }
534
);
535
$dbi->helper(
536
    two => sub { 2 }
537
);
538
$dbi->helper({
539
    twice => sub {
540
        my $self = shift;
541
        return $_[0] * 2;
542
    }
543
});
544

            
545
is($dbi->one, 1, "$test : first");
546
is($dbi->two, 2, "$test : second");
547
is($dbi->twice(5), 10 , "$test : second");
548

            
549
eval {$dbi->XXXXXX};
550
like($@, qr/\QCan't locate object method "XXXXXX" via "DBIx::Custom"/, "$test : not exists");
551

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
552
test 'out filter';
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
553
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
554
$dbi->execute($CREATE_TABLE->{0});
555
$dbi->register_filter(twice => sub { $_[0] * 2 });
556
$dbi->register_filter(three_times => sub { $_[0] * 3});
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
557
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
558
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
559
              'key2' => {out => 'three_times', in => 'twice'});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
560
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
561
$result = $dbi->execute($SELECT_SOURCES->{0});
562
$row   = $result->fetch_hash_first;
563
is_deeply($row, {key1 => 2, key2 => 6}, "$test : insert");
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
564
$result = $dbi->select(table => 'table1');
565
$row   = $result->fetch_hash_first;
566
is_deeply($row, {key1 => 6, key2 => 12}, "$test : insert");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
567

            
568
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
569
$dbi->execute($CREATE_TABLE->{0});
570
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
571
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
572
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
573
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
574
$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
575
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
576
$result = $dbi->execute($SELECT_SOURCES->{0});
577
$row   = $result->fetch_hash_first;
578
is_deeply($row, {key1 => 4, key2 => 2}, "$test : update");
579

            
580
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
581
$dbi->execute($CREATE_TABLE->{0});
582
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
583
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
584
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
585
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
586
$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
587
$dbi->delete(table => 'table1', where => {key1 => 1});
588
$result = $dbi->execute($SELECT_SOURCES->{0});
589
$rows   = $result->fetch_hash_all;
590
is_deeply($rows, [], "$test : delete");
591

            
592
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
593
$dbi->execute($CREATE_TABLE->{0});
594
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
595
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
596
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
597
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
598
$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
599
$result = $dbi->select(table => 'table1', where => {key1 => 1});
600
$result->filter({'key2' => 'twice'});
601
$rows   = $result->fetch_hash_all;
602
is_deeply($rows, [{key1 => 4, key2 => 4}], "$test : select");
603

            
604
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
605
$dbi->execute($CREATE_TABLE->{0});
606
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
607
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
608
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
609
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
610
$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
611
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};",
612
                        param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
613
                        table => ['table1']);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
614
$rows   = $result->fetch_hash_all;
615
is_deeply($rows, [{key1 => 4, key2 => 2}], "$test : execute");
616

            
617
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
618
$dbi->execute($CREATE_TABLE->{0});
619
$dbi->execute($CREATE_TABLE->{2});
620
$dbi->register_filter(twice => sub { $_[0] * 2 });
621
$dbi->register_filter(three_times => sub { $_[0] * 3 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
622
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
623
    'table1', 'key2' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
624
);
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
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
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 => 5, key2 => 2}, filter => {key2 => undef});
629
$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
630
$result = $dbi->select(
631
     table => ['table1', 'table2'],
632
     column => ['key2', 'key3'],
633
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
634

            
635
$result->filter({'key2' => 'twice'});
636
$rows   = $result->fetch_hash_all;
637
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join");
638

            
639
$result = $dbi->select(
640
     table => ['table1', 'table2'],
641
     column => ['key2', 'key3'],
642
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
643

            
644
$result->filter({'key2' => 'twice'});
645
$rows   = $result->fetch_hash_all;
646
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit");
647

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
648
test 'iterate_all_columns';
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
649
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
650
$dbi->execute($CREATE_TABLE->{2});
651
$dbi->execute($CREATE_TABLE->{3});
652

            
653
$infos = [];
654
$dbi->iterate_all_columns(sub {
655
    my ($table, $column, $cinfo) = @_;
656
    
657
    if ($table =~ /^table/) {
658
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
659
         push @$infos, $info;
660
    }
661
});
662
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
663
is_deeply($infos, 
664
    [
665
        ['table1', 'key1', 'key1'],
666
        ['table1', 'key2', 'key2'],
667
        ['table2', 'key1', 'key1'],
668
        ['table2', 'key3', 'key3']
669
    ]
670
    , $test
671
);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
672

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
673
test 'table';
674
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
675
$dbi->execute($CREATE_TABLE->{0});
676
$table = $dbi->table('table1');
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
677
$table->insert(param => {key1 => 1, key2 => 2});
678
$table->insert(param => {key1 => 3, key2 => 4});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
679
$rows = $table->select->fetch_hash_all;
680
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}],
681
                 "$test: select");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
682
$rows = $table->select(where => {key2 => 2}, append => 'order by key1',
683
                              column => ['key1', 'key2'])->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
684
is_deeply($rows, [{key1 => 1, key2 => 2}],
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
685
                 "$test: insert insert select");
686
$table->update(param => {key1 => 3}, where => {key2 => 2});
687
$table->update(param => {key1 => 5}, where => {key2 => 4});
688
$rows = $table->select(where => {key2 => 2})->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
689
is_deeply($rows, [{key1 => 3, key2 => 2}],
690
                 "$test: update");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
691
$table->delete(where => {key2 => 2});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
692
$rows = $table->select->fetch_hash_all;
693
is_deeply($rows, [{key1 => 5, key2 => 4}], "$test: delete");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
694
$table->update_all(param => {key1 => 3});
695
$rows = $table->select->fetch_hash_all;
696
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test: update_all");
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
697
$table->delete_all;
698
$rows = $table->select->fetch_hash_all;
699
is_deeply($rows, [], "$test: delete_all");
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
700

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
701
$dbi->dbh->do($CREATE_TABLE->{2});
702
$dbi->table('table2', ppp => sub {
703
    my $self = shift;
704
    
705
    return $self->name;
706
});
707
is($dbi->table('table2')->ppp, 'table2', "$test : helper");
708

            
add examples
Yuki Kimoto authored on 2011-01-07
709
test 'limit';
710
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
711
$dbi->execute($CREATE_TABLE->{0});
712
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
713
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
714
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
715
$dbi->query_builder->register_tag_processor(
716
    limit => sub {
717
        my ($count, $offset) = @_;
718
        
719
        my $s = '';
720
        $s .= "limit $count";
721
        $s .= " offset $offset" if defined $offset;
722
        
723
        return [$s, []];
724
    }
725
);
726
$rows = $dbi->select(
727
  table => 'table1',
728
  where => {key1 => 1},
729
  append => "order by key2 {limit 1 0}"
730
)->fetch_hash_all;
731
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
732
$rows = $dbi->select(
733
  table => 'table1',
734
  where => {key1 => 1},
735
  append => "order by key2 {limit 2 1}"
736
)->fetch_hash_all;
737
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test);
738
$rows = $dbi->select(
739
  table => 'table1',
740
  where => {key1 => 1},
741
  append => "order by key2 {limit 1}"
742
)->fetch_hash_all;
743
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
744

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
745
test 'connect super';
746
{
747
    package MyDBI;
748
    
749
    use base 'DBIx::Custom';
750
    sub connect {
751
        my $self = shift->SUPER::connect(@_);
752
        
753
        return $self;
754
    }
755
    
756
    sub new {
757
        my $self = shift->SUPER::connect(@_);
758
        
759
        return $self;
760
    }
761
}
762

            
763
$dbi = MyDBI->connect($NEW_ARGS->{0});
764
$dbi->execute($CREATE_TABLE->{0});
765
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
766
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test);
767

            
768
$dbi = MyDBI->new($NEW_ARGS->{0});
769
$dbi->execute($CREATE_TABLE->{0});
770
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
771
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
772

            
773

            
774
test 'end_filter';
775
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
776
$dbi->execute($CREATE_TABLE->{0});
777
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
778
$result = $dbi->select(table => 'table1');
779
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
780
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
781
$row = $result->fetch_first;
782
is_deeply($row, [6, 40]);
783

            
784
$result = $dbi->select(table => 'table1');
785
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
786
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
787
$row = $result->fetch_hash_first;
788
is_deeply($row, {key1 => 6, key2 => 40});
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
789

            
790

            
791
test 'empty where select';
792
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
793
$dbi->execute($CREATE_TABLE->{0});
794
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
795
$result = $dbi->select(table => 'table1', where => {});
796
$row = $result->fetch_hash_first;
797
is_deeply($row, {key1 => 1, key2 => 2});
798

            
799
$result = $dbi->select(table => 'table1', where => [' ', {}]);
800
$row = $result->fetch_hash_first;
801
is_deeply($row, {key1 => 1, key2 => 2});