DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
736 lines | 28.596kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
use Test::More;
2
use strict;
3
use warnings;
4

            
5
BEGIN {
6
    eval { require DBD::SQLite; 1 }
7
        or plan skip_all => 'DBD::SQLite required';
8
    eval { DBD::SQLite->VERSION >= 1.25 }
9
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
10

            
11
    plan 'no_plan';
12
    use_ok('DBIx::Custom');
13
}
14

            
15
# Function for test name
16
my $test;
17
sub test {
18
    $test = shift;
19
}
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));',
25
    2 => 'create table table2 (key1 char(255), key3 char(255));'
26
};
27

            
28
my $SELECT_TMPL = {
29
    0 => 'select * from table1;'
30
};
31

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

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

            
40
# Variables for test
41
my $dbi;
42
my $sth;
43
my $tmpl;
44
my @tmpls;
45
my $select_tmpl;
46
my $insert_tmpl;
47
my $update_tmpl;
48
my $params;
49
my $sql;
50
my $result;
51
my @rows;
52
my $rows;
53
my $query;
54
my @queries;
55
my $select_query;
56
my $insert_query;
57
my $update_query;
58
my $ret_val;
59

            
60

            
61
test 'disconnect';
62
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
63
$dbi->connect;
64
$dbi->disconnect;
65
ok(!$dbi->dbh, $test);
66

            
67

            
68
test 'connected';
69
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
70
ok(!$dbi->connected, "$test : not connected");
71
$dbi->connect;
72
ok($dbi->connected, "$test : connected");
73

            
74

            
75
test 'preapare';
76
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
77
$sth = $dbi->prepare($CREATE_TABLE->{0});
78
ok($sth, "$test : auto connect");
79
$sth->execute;
80
$sth = $dbi->prepare($DROP_TABLE->{0});
81
ok($sth, "$test : basic");
82

            
83

            
84
test 'do';
85
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
86
$ret_val = $dbi->do($CREATE_TABLE->{0});
87
ok(defined $ret_val, "$test : auto connect");
88
$ret_val = $dbi->do($DROP_TABLE->{0});
89
ok(defined $ret_val, "$test : basic");
90

            
version 0.0901
yuki-kimoto authored on 2009-12-17
91
test 'create_table';
92
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
93
$ret_val = $dbi->create_table(
94
                   'table1',
95
                   'key1 char(255)',
96
                   'key2 char(255)'
97
                 );
98
ok(defined $ret_val, "$test : create_table");
99

            
cleanup
yuki-kimoto authored on 2010-02-11
100
$dbi->insert('table1', {key1 => 1, key2 => 2});
version 0.0901
yuki-kimoto authored on 2009-12-17
101
ok(!$@, "$test : table exist");
102

            
103
$ret_val = $dbi->drop_table('table1');
104
ok(defined $ret_val, "$test : drop table");
105

            
106
eval{$dbi->select('table1')};
107
ok($@, "$test : table not exist");
packaging one directory
yuki-kimoto authored on 2009-11-16
108

            
109
# Prepare table
110
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
111
$dbi->connect;
112
$dbi->do($CREATE_TABLE->{0});
113
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
114
$sth->execute(1, 2);
115
$sth->execute(3, 4);
116

            
117

            
118
test 'DBIx::Custom::Result test';
119
$tmpl = "select key1, key2 from table1";
120
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
121
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
122

            
123
@rows = ();
124
while (my $row = $result->fetch) {
125
    push @rows, [@$row];
126
}
127
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
128

            
version 0.0901
yuki-kimoto authored on 2009-12-17
129
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
130
@rows = ();
131
while (my @row = $result->fetch) {
132
    push @rows, [@row];
133
}
134
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
135

            
version 0.0901
yuki-kimoto authored on 2009-12-17
136
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
137
@rows = ();
138
while (my $row = $result->fetch_hash) {
139
    push @rows, {%$row};
140
}
141
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
142

            
version 0.0901
yuki-kimoto authored on 2009-12-17
143
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
144
@rows = ();
145
while (my %row = $result->fetch_hash) {
146
    push @rows, {%row};
147
}
148
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
149

            
version 0.0901
yuki-kimoto authored on 2009-12-17
150
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
151
$rows = $result->fetch_all;
152
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
153

            
version 0.0901
yuki-kimoto authored on 2009-12-17
154
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
155
@rows = $result->fetch_all;
156
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context");
157

            
version 0.0901
yuki-kimoto authored on 2009-12-17
158
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
159
@rows = $result->fetch_hash_all;
160
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context");
161

            
version 0.0901
yuki-kimoto authored on 2009-12-17
162
$result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
163
@rows = $result->fetch_all;
164
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context");
165

            
166

            
167
test 'Insert query return value';
168
$dbi->do($DROP_TABLE->{0});
169
$dbi->do($CREATE_TABLE->{0});
170
$tmpl = "insert into table1 {insert key1 key2}";
171
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
172
$ret_val = $dbi->query($query, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
173
ok($ret_val, $test);
174

            
175

            
version 0.0901
yuki-kimoto authored on 2009-12-17
176
test 'Direct query';
packaging one directory
yuki-kimoto authored on 2009-11-16
177
$dbi->do($DROP_TABLE->{0});
178
$dbi->do($CREATE_TABLE->{0});
179
$insert_tmpl = "insert into table1 {insert key1 key2}";
version 0.0901
yuki-kimoto authored on 2009-12-17
180
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}, sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
181
    my $query = shift;
182
    $query->bind_filter(sub {
183
        my ($value, $key) = @_;
184
        if ($key eq 'key2') {
185
            return $value + 1;
186
        }
187
        return $value;
188
    });
189
});
version 0.0901
yuki-kimoto authored on 2009-12-17
190
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
191
$rows = $result->fetch_hash_all;
192
is_deeply($rows, [{key1 => 1, key2 => 3}], $test);
193

            
194

            
195
test 'Filter basic';
196
$dbi->do($DROP_TABLE->{0});
197
$dbi->do($CREATE_TABLE->{0});
198

            
199
$insert_tmpl  = "insert into table1 {insert key1 key2};";
200
$insert_query = $dbi->create_query($insert_tmpl);
201
$insert_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
202
    my ($value, $key, $dbi, $infos) = @_;
203
    my ($table, $column) = @{$infos}{qw/table column/};
204
    
205
    if ($key eq 'key1' && $table eq '' && $column eq 'key1'
206
        && $dbi->isa('DBIx::Custom'))
207
    {
packaging one directory
yuki-kimoto authored on 2009-11-16
208
        return $value * 2;
209
    }
210
    return $value;
211
});
version 0.0901
yuki-kimoto authored on 2009-12-17
212
$dbi->query($insert_query, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
213
$select_query = $dbi->create_query($SELECT_TMPL->{0});
214
$select_query->fetch_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
215
    my ($value, $key, $dbi, $infos) = @_;
216
    my ($type, $sth, $i) = @{$infos}{qw/type sth index/};
217
    
218
    if ($key eq 'key2' && $type =~ /char/ && ref $sth eq 'DBI::st' 
219
        && $i == 1 && $dbi->isa('DBIx::Custom'))
220
    {
packaging one directory
yuki-kimoto authored on 2009-11-16
221
        return $value * 3;
222
    }
223
    return $value;
224
});
version 0.0901
yuki-kimoto authored on 2009-12-17
225
$result = $dbi->query($select_query);
packaging one directory
yuki-kimoto authored on 2009-11-16
226
$rows = $result->fetch_hash_all;
227
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
228

            
229
$dbi->do("delete from table1;");
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
230
$insert_query->no_bind_filters(['key1']);
231
$select_query->no_fetch_filters(['key2']);
version 0.0901
yuki-kimoto authored on 2009-12-17
232
$dbi->query($insert_query, {key1 => 1, key2 => 2});
233
$result = $dbi->query($select_query);
packaging one directory
yuki-kimoto authored on 2009-11-16
234
$rows = $result->fetch_hash_all;
235
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters");
236

            
237
$dbi->do($DROP_TABLE->{0});
238
$dbi->do($CREATE_TABLE->{0});
239
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
240
$insert_query = $dbi->create_query($insert_tmpl);
241
$insert_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
242
    my ($value, $key, $dbi, $infos) = @_;
243
    my ($table, $column) = @{$infos}{qw/table column/};
244
    
packaging one directory
yuki-kimoto authored on 2009-11-16
245
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
246
        return $value * 3;
247
    }
248
    return $value;
249
});
version 0.0901
yuki-kimoto authored on 2009-12-17
250
$dbi->query($insert_query, {table1 => {key1 => 1, key2 => 2}});
packaging one directory
yuki-kimoto authored on 2009-11-16
251
$select_query = $dbi->create_query($SELECT_TMPL->{0});
version 0.0901
yuki-kimoto authored on 2009-12-17
252
$result       = $dbi->query($select_query);
packaging one directory
yuki-kimoto authored on 2009-11-16
253
$rows = $result->fetch_hash_all;
254
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
255

            
256
test 'Filter in';
257
$insert_tmpl  = "insert into table1 {insert key1 key2};";
258
$insert_query = $dbi->create_query($insert_tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
259
$dbi->query($insert_query, {key1 => 2, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
260
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
261
$select_query = $dbi->create_query($select_tmpl);
262
$select_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
263
    my ($value, $key, $dbi, $infos) = @_;
264
    my ($table, $column) = @{$infos}{qw/table column/};
265
    
packaging one directory
yuki-kimoto authored on 2009-11-16
266
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') {
267
        return $value * 2;
268
    }
269
    return $value;
270
});
version 0.0901
yuki-kimoto authored on 2009-12-17
271
$result = $dbi->query($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}});
packaging one directory
yuki-kimoto authored on 2009-11-16
272
$rows = $result->fetch_hash_all;
273
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter");
274

            
275

            
276
test 'DBIx::Custom::SQL::Template basic tag';
277
$dbi->do($DROP_TABLE->{0});
278
$dbi->do($CREATE_TABLE->{1});
279
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
280
$sth->execute(1, 2, 3, 4, 5);
281
$sth->execute(6, 7, 8, 9, 10);
282

            
283
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
284
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
285
$result = $dbi->query($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
286
$rows = $result->fetch_hash_all;
287
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
288

            
289
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
290
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
291
$result = $dbi->query($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
packaging one directory
yuki-kimoto authored on 2009-11-16
292
$rows = $result->fetch_hash_all;
293
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table");
294

            
295
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
296
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
297
$result = $dbi->query($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
298
$rows = $result->fetch_hash_all;
299
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot");
300

            
301
$tmpl = "select * from table1 where {<= key1} and {like key2};";
302
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
303
$result = $dbi->query($query, {key1 => 1, key2 => '%2%'});
packaging one directory
yuki-kimoto authored on 2009-11-16
304
$rows = $result->fetch_hash_all;
305
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
306

            
307
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
308
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
309
$result = $dbi->query($query, {table1 => {key1 => 1, key2 => '%2%'}});
packaging one directory
yuki-kimoto authored on 2009-11-16
310
$rows = $result->fetch_hash_all;
311
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table");
312

            
313
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
314
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
315
$result = $dbi->query($query, {'table1.key1' => 1, 'table1.key2' => '%2%'});
packaging one directory
yuki-kimoto authored on 2009-11-16
316
$rows = $result->fetch_hash_all;
317
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot");
318

            
319

            
320
test 'DIB::Custom::SQL::Template in tag';
321
$dbi->do($DROP_TABLE->{0});
322
$dbi->do($CREATE_TABLE->{1});
323
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
324
$sth->execute(1, 2, 3, 4, 5);
325
$sth->execute(6, 7, 8, 9, 10);
326

            
327
$tmpl = "select * from table1 where {in key1 2};";
328
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
329
$result = $dbi->query($query, {key1 => [9, 1]});
packaging one directory
yuki-kimoto authored on 2009-11-16
330
$rows = $result->fetch_hash_all;
331
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
332

            
333
$tmpl = "select * from table1 where {in table1.key1 2};";
334
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
335
$result = $dbi->query($query, {table1 => {key1 => [9, 1]}});
packaging one directory
yuki-kimoto authored on 2009-11-16
336
$rows = $result->fetch_hash_all;
337
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table");
338

            
339
$tmpl = "select * from table1 where {in table1.key1 2};";
340
$query = $dbi->create_query($tmpl);
version 0.0901
yuki-kimoto authored on 2009-12-17
341
$result = $dbi->query($query, {'table1.key1' => [9, 1]});
packaging one directory
yuki-kimoto authored on 2009-11-16
342
$rows = $result->fetch_hash_all;
343
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot");
344

            
345

            
346
test 'DBIx::Custom::SQL::Template insert tag';
347
$dbi->do("delete from table1");
348
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
349
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
350

            
version 0.0901
yuki-kimoto authored on 2009-12-17
351
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
352
$rows = $result->fetch_hash_all;
353
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
354

            
355
$dbi->do("delete from table1");
356
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
357
$dbi->query($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
358
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
359
$rows = $result->fetch_hash_all;
360
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
361

            
362
$dbi->do("delete from table1");
363
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
364
$dbi->query($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5});
365
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
366
$rows = $result->fetch_hash_all;
367
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot");
368

            
369

            
370
test 'DBIx::Custom::SQL::Template update tag';
371
$dbi->do("delete from table1");
372
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
version 0.0901
yuki-kimoto authored on 2009-12-17
373
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
374
$dbi->query($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
packaging one directory
yuki-kimoto authored on 2009-11-16
375

            
376
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
377
$dbi->query($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
378

            
version 0.0901
yuki-kimoto authored on 2009-12-17
379
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
380
$rows = $result->fetch_hash_all;
381
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
382
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
383

            
384
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
385
$dbi->query($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}});
386
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
387
$rows = $result->fetch_hash_all;
388
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5},
389
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name");
390

            
391
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
version 0.0901
yuki-kimoto authored on 2009-12-17
392
$dbi->query($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5});
393
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
394
$rows = $result->fetch_hash_all;
395
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5},
396
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot");
397

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
398
test 'transaction';
packaging one directory
yuki-kimoto authored on 2009-11-16
399
$dbi->do($DROP_TABLE->{0});
400
$dbi->do($CREATE_TABLE->{0});
remove run_transaction().
yuki-kimoto authored on 2010-01-30
401
$dbi->transaction->run(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
402
    $insert_tmpl = 'insert into table1 {insert key1 key2}';
version 0.0901
yuki-kimoto authored on 2009-12-17
403
    $dbi->query($insert_tmpl, {key1 => 1, key2 => 2});
404
    $dbi->query($insert_tmpl, {key1 => 3, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
405
});
version 0.0901
yuki-kimoto authored on 2009-12-17
406
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
407
$rows   = $result->fetch_hash_all;
408
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
409

            
410
$dbi->do($DROP_TABLE->{0});
411
$dbi->do($CREATE_TABLE->{0});
412
$dbi->dbh->{RaiseError} = 0;
413
eval{
remove run_transaction().
yuki-kimoto authored on 2010-01-30
414
    $dbi->transaction->run(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
415
        $insert_tmpl = 'insert into table1 {insert key1 key2}';
version 0.0901
yuki-kimoto authored on 2009-12-17
416
        $dbi->query($insert_tmpl, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
417
        die "Fatal Error";
version 0.0901
yuki-kimoto authored on 2009-12-17
418
        $dbi->query($insert_tmpl, {key1 => 3, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
419
    })
420
};
421
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message");
422
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value");
version 0.0901
yuki-kimoto authored on 2009-12-17
423
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
424
$rows   = $result->fetch_hash_all;
425
is_deeply($rows, [], "$test : rollback");
426

            
427

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
428

            
packaging one directory
yuki-kimoto authored on 2009-11-16
429
test 'Error case';
430
$dbi = DBIx::Custom->new;
remove run_transaction().
yuki-kimoto authored on 2010-01-30
431
eval{$dbi->transaction->run};
packaging one directory
yuki-kimoto authored on 2009-11-16
432
like($@, qr/Not yet connect to database/, "$test : Yet Connected");
433

            
434
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit');
435
eval{$dbi->connect;};
436
ok($@, "$test : connect error");
437

            
438
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
439
$dbi->connect;
440
$dbi->dbh->{AutoCommit} = 0;
remove run_transaction().
yuki-kimoto authored on 2010-01-30
441
eval{$dbi->transaction->run};
packaging one directory
yuki-kimoto authored on 2009-11-16
442
like($@, qr/AutoCommit must be true before transaction start/,
remove run_transaction().
yuki-kimoto authored on 2010-01-30
443
         "$test : transaction auto commit is false");
packaging one directory
yuki-kimoto authored on 2009-11-16
444

            
445
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
446
$sql = 'laksjdf';
447
eval{$dbi->prepare($sql)};
448
like($@, qr/$sql/, "$test : prepare fail");
449

            
450
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
451
$sql = 'laksjdf';
452
eval{$dbi->do($sql, qw/1 2 3/)};
453
like($@, qr/$sql/, "$test : do fail");
454

            
455
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
456
eval{$dbi->create_query("{p }")};
457
ok($@, "$test : create_query invalid SQL template");
458

            
459
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
460
$dbi->do($CREATE_TABLE->{0});
461
$query = $dbi->create_query("select * from table1 where {= key1}");
version 0.0901
yuki-kimoto authored on 2009-12-17
462
eval{$dbi->query($query, {key2 => 1})};
packaging one directory
yuki-kimoto authored on 2009-11-16
463
like($@, qr/Corresponding key is not found in your parameters/, 
464
        "$test : execute corresponding key not found");
465

            
466

            
467
test 'insert';
468
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
469
$dbi->do($CREATE_TABLE->{0});
470
$dbi->insert('table1', {key1 => 1, key2 => 2});
471
$dbi->insert('table1', {key1 => 3, key2 => 4});
version 0.0901
yuki-kimoto authored on 2009-12-17
472
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
473
$rows   = $result->fetch_hash_all;
474
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
475

            
476
$dbi->do('delete from table1');
477
$dbi->insert('table1', {key1 => 1, key2 => 2}, sub {
478
    my $query = shift;
479
    $query->bind_filter(sub {
480
        my ($value, $key) = @_;
481
        if ($key eq 'key1') {
482
            return $value * 3;
483
        }
484
        return $value;
485
    });
486
});
version 0.0901
yuki-kimoto authored on 2009-12-17
487
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
488
$rows   = $result->fetch_hash_all;
489
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
490

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
491
$dbi->insert('table1', {key1 => 1, key2 => 2}, '   ', sub {
492
    my $query = shift;
493
    like($query->sql, qr/insert into table1 \(.+\) values \(\?, \?\)    ;/, 
494
        "$test: append statement");
495
});
packaging one directory
yuki-kimoto authored on 2009-11-16
496

            
497
test 'insert error';
498
eval{$dbi->insert('table1')};
499
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed");
500

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
501
eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, '', 'aaa')};
packaging one directory
yuki-kimoto authored on 2009-11-16
502
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref");
503

            
504

            
505
test 'update';
506
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
507
$dbi->do($CREATE_TABLE->{1});
508
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
509
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
510
$dbi->update('table1', {key2 => 11}, {key1 => 1});
version 0.0901
yuki-kimoto authored on 2009-12-17
511
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
512
$rows   = $result->fetch_hash_all;
513
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
514
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
515
                  "$test : basic");
516
                  
add DBIx::Custom::Column
yuki-kimoto authored on 2010-02-11
517
#$dbi->do("delete from table1");
518
#$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
519
#$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
520
#$dbi->update('table1', {key2 => 12}, {key2 => 2, key3 => 3});
521
#$result = $dbi->query($SELECT_TMPL->{0});
522
#$rows   = $result->fetch_hash_all;
523
#is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
524
#                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
525
#                  "$test : update key same as search key");
packaging one directory
yuki-kimoto authored on 2009-11-16
526

            
527
$dbi->do("delete from table1");
528
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
529
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
530
$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub {
531
    my $query = shift;
532
    $query->bind_filter(sub {
533
        my ($value, $key) = @_;
534
        if ($key eq 'key2') {
535
            return $value * 2;
536
        }
537
        return $value;
538
    });
539
});
version 0.0901
yuki-kimoto authored on 2009-12-17
540
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
541
$rows   = $result->fetch_hash_all;
542
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
543
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
544
                  "$test : query edit callback");
545

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
546
$dbi->update('table1', {key2 => 11}, {key1 => 1}, '   ', sub {
547
    my $query = shift;
548
    is($query->sql, 'update table1 set key2 = ? where key1 = ?    ;',
549
       "$test: append statement");
550
});
551

            
packaging one directory
yuki-kimoto authored on 2009-11-16
552

            
553
test 'update error';
554
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
555
$dbi->do($CREATE_TABLE->{1});
556
eval{$dbi->update('table1')};
557
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/,
558
         "$test : update key-value pairs not specified");
559

            
560
eval{$dbi->update('table1', {key2 => 1})};
561
like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/,
562
         "$test : where key-value pairs not specified");
563

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
564
eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, '', 'aaa')};
packaging one directory
yuki-kimoto authored on 2009-11-16
565
like($@, qr/Query edit callback must be code reference/, 
566
         "$test : query edit callback not code reference");
567

            
568

            
569
test 'update_all';
570
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
571
$dbi->do($CREATE_TABLE->{1});
572
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
573
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
574
$dbi->update_all('table1', {key2 => 10}, sub {
575
    my $query = shift;
576
    $query->bind_filter(sub {
577
        my ($value, $key) = @_;
578
        return $value * 2;
579
    })
580
});
version 0.0901
yuki-kimoto authored on 2009-12-17
581
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
582
$rows   = $result->fetch_hash_all;
583
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
584
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
585
                  "$test : query edit callback");
586

            
587

            
588
test 'delete';
589
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
590
$dbi->do($CREATE_TABLE->{0});
591
$dbi->insert('table1', {key1 => 1, key2 => 2});
592
$dbi->insert('table1', {key1 => 3, key2 => 4});
593
$dbi->delete('table1', {key1 => 1});
version 0.0901
yuki-kimoto authored on 2009-12-17
594
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
595
$rows   = $result->fetch_hash_all;
596
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
597

            
598
$dbi->do("delete from table1;");
599
$dbi->insert('table1', {key1 => 1, key2 => 2});
600
$dbi->insert('table1', {key1 => 3, key2 => 4});
601
$dbi->delete('table1', {key2 => 1}, sub {
602
    my $query = shift;
603
    $query->bind_filter(sub {
604
        my ($value, $key) = @_;
605
        return $value * 2;
606
    });
607
});
version 0.0901
yuki-kimoto authored on 2009-12-17
608
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
609
$rows   = $result->fetch_hash_all;
610
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
611

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
612
$dbi->delete('table1', {key1 => 1}, '   ', sub {
613
    my $query = shift;
614
    is($query->sql, 'delete from table1 where key1 = ?    ;',
615
       "$test: append statement");
616
});
617

            
618

            
packaging one directory
yuki-kimoto authored on 2009-11-16
619
$dbi->delete_all('table1');
620
$dbi->insert('table1', {key1 => 1, key2 => 2});
621
$dbi->insert('table1', {key1 => 3, key2 => 4});
622
$dbi->delete('table1', {key1 => 1, key2 => 2});
623
$rows = $dbi->select('table1')->fetch_hash_all;
624
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
625

            
626

            
627
test 'delete error';
628
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
629
$dbi->do($CREATE_TABLE->{0});
630
eval{$dbi->delete('table1')};
631
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
632
         "$test : where key-value pairs not specified");
633

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
634
eval{$dbi->delete('table1', {key1 => 1}, '', 'aaa')};
packaging one directory
yuki-kimoto authored on 2009-11-16
635
like($@, qr/Query edit callback must be code reference/, 
636
         "$test : query edit callback not code ref");
637

            
638

            
639
test 'delete_all';
640
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
641
$dbi->do($CREATE_TABLE->{0});
642
$dbi->insert('table1', {key1 => 1, key2 => 2});
643
$dbi->insert('table1', {key1 => 3, key2 => 4});
644
$dbi->delete_all('table1');
version 0.0901
yuki-kimoto authored on 2009-12-17
645
$result = $dbi->query($SELECT_TMPL->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
646
$rows   = $result->fetch_hash_all;
647
is_deeply($rows, [], "$test : basic");
648

            
649

            
650
test 'select';
651
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
652
$dbi->do($CREATE_TABLE->{0});
653
$dbi->insert('table1', {key1 => 1, key2 => 2});
654
$dbi->insert('table1', {key1 => 3, key2 => 4});
655
$rows = $dbi->select('table1')->fetch_hash_all;
656
is_deeply($rows, [{key1 => 1, key2 => 2},
657
                  {key1 => 3, key2 => 4}], "$test : table");
658

            
659
$rows = $dbi->select('table1', ['key1'])->fetch_hash_all;
660
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
661

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

            
665
$rows = $dbi->select('table1', ['key1'], {key1 => 3})->fetch_hash_all;
666
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
667

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

            
671
$rows = $dbi->select('table1', {key1 => 2}, sub {
672
    my $query = shift;
673
    $query->bind_filter(sub {
674
        my ($value, $key) = @_;
675
        if ($key eq 'key1') {
676
            return $value - 1;
677
        }
678
        return $value;
679
    });
680
})->fetch_hash_all;
681
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
682

            
683
$dbi->do($CREATE_TABLE->{2});
684
$dbi->insert('table2', {key1 => 1, key3 => 5});
685
$rows = $dbi->select([qw/table1 table2/],
686
                     ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
687
                     {'table1.key2' => 2},
688
                     "where table1.key1 = table2.key1")->fetch_hash_all;
689
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
690

            
691
test 'Cache';
692
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
693
DBIx::Custom->query_cache_max(2);
694
$dbi->do($CREATE_TABLE->{0});
cleanup
yuki-kimoto authored on 2009-12-22
695
delete $DBIx::Custom::CLASS_ATTRS->{_query_caches};
696
delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys};
packaging one directory
yuki-kimoto authored on 2009-11-16
697
$tmpls[0] = "insert into table1 {insert key1 key2}";
698
$queries[0] = $dbi->create_query($tmpls[0]);
699
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
700
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
701
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
702

            
703
$tmpls[1] = "select * from table1";
704
$queries[1] = $dbi->create_query($tmpls[1]);
705
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
706
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
707
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
708
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos second");
709
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
710

            
711
$tmpls[2] = "select key1, key2 from table1";
712
$queries[2] = $dbi->create_query($tmpls[2]);
713
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
714
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
715
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
716
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
717
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
718
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
719

            
720
$queries[1] = $dbi->create_query($tmpls[1]);
721
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
722
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
fix timeformat tests
yuki-kimoto authored on 2009-11-23
723
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
724
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
fix timeformat tests
yuki-kimoto authored on 2009-11-23
725
is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
726
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
727

            
fix timeformat tests
yuki-kimoto authored on 2009-11-23
728
$query = $dbi->create_query($tmpls[0]);
729
$query->bind_filter('aaa');
730
$query = $dbi->create_query($tmpls[0]);
731
ok(!$query->bind_filter, "$test : only cached sql and key_infos");
732
$query->bind_filter('bbb');
733
$query = $dbi->create_query($tmpls[0]);
734
ok(!$query->bind_filter, "$test : only cached sql and key_infos");
735

            
736