DBIx-Custom / t / dbi-custom-core-sqlite.t /
Newer Older
758 lines | 30.602kb
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

            
91

            
92
# Prepare table
93
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
94
$dbi->connect;
95
$dbi->do($CREATE_TABLE->{0});
96
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
97
$sth->execute(1, 2);
98
$sth->execute(3, 4);
99

            
100

            
101
test 'DBIx::Custom::Result test';
102
$tmpl = "select key1, key2 from table1";
103
$query = $dbi->create_query($tmpl);
104
$result = $dbi->execute($query);
105

            
106
@rows = ();
107
while (my $row = $result->fetch) {
108
    push @rows, [@$row];
109
}
110
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
111

            
112
$result = $dbi->execute($query);
113
@rows = ();
114
while (my @row = $result->fetch) {
115
    push @rows, [@row];
116
}
117
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
118

            
119
$result = $dbi->execute($query);
120
@rows = ();
121
while (my $row = $result->fetch_hash) {
122
    push @rows, {%$row};
123
}
124
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
125

            
126
$result = $dbi->execute($query);
127
@rows = ();
128
while (my %row = $result->fetch_hash) {
129
    push @rows, {%row};
130
}
131
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
132

            
133
$result = $dbi->execute($query);
134
$rows = $result->fetch_all;
135
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
136

            
137
$result = $dbi->execute($query);
138
@rows = $result->fetch_all;
139
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context");
140

            
141
$result = $dbi->execute($query);
142
@rows = $result->fetch_hash_all;
143
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context");
144

            
145
$result = $dbi->execute($query);
146
@rows = $result->fetch_all;
147
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context");
148

            
149

            
150
test 'Insert query return value';
151
$dbi->do($DROP_TABLE->{0});
152
$dbi->do($CREATE_TABLE->{0});
153
$tmpl = "insert into table1 {insert key1 key2}";
154
$query = $dbi->create_query($tmpl);
155
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2});
156
ok($ret_val, $test);
157

            
158

            
159
test 'Direct execute';
160
$dbi->do($DROP_TABLE->{0});
161
$dbi->do($CREATE_TABLE->{0});
162
$insert_tmpl = "insert into table1 {insert key1 key2}";
163
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub {
164
    my $query = shift;
165
    $query->bind_filter(sub {
166
        my ($value, $key) = @_;
167
        if ($key eq 'key2') {
168
            return $value + 1;
169
        }
170
        return $value;
171
    });
172
});
173
$result = $dbi->execute($SELECT_TMPL->{0});
174
$rows = $result->fetch_hash_all;
175
is_deeply($rows, [{key1 => 1, key2 => 3}], $test);
176

            
177

            
178
test 'Filter basic';
179
$dbi->do($DROP_TABLE->{0});
180
$dbi->do($CREATE_TABLE->{0});
181

            
182
$insert_tmpl  = "insert into table1 {insert key1 key2};";
183
$insert_query = $dbi->create_query($insert_tmpl);
184
$insert_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
185
    my ($value, $key, $dbi, $infos) = @_;
186
    my ($table, $column) = @{$infos}{qw/table column/};
187
    
188
    if ($key eq 'key1' && $table eq '' && $column eq 'key1'
189
        && $dbi->isa('DBIx::Custom'))
190
    {
packaging one directory
yuki-kimoto authored on 2009-11-16
191
        return $value * 2;
192
    }
193
    return $value;
194
});
195
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
196
$select_query = $dbi->create_query($SELECT_TMPL->{0});
197
$select_query->fetch_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
198
    my ($value, $key, $dbi, $infos) = @_;
199
    my ($type, $sth, $i) = @{$infos}{qw/type sth index/};
200
    
201
    if ($key eq 'key2' && $type =~ /char/ && ref $sth eq 'DBI::st' 
202
        && $i == 1 && $dbi->isa('DBIx::Custom'))
203
    {
packaging one directory
yuki-kimoto authored on 2009-11-16
204
        return $value * 3;
205
    }
206
    return $value;
207
});
208
$result = $dbi->execute($select_query);
209
$rows = $result->fetch_hash_all;
210
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
211

            
212
$dbi->do("delete from table1;");
213
$insert_query->no_bind_filters('key1');
214
$select_query->no_fetch_filters('key2');
215
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
216
$result = $dbi->execute($select_query);
217
$rows = $result->fetch_hash_all;
218
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters");
219

            
220
$dbi->do($DROP_TABLE->{0});
221
$dbi->do($CREATE_TABLE->{0});
222
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
223
$insert_query = $dbi->create_query($insert_tmpl);
224
$insert_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
225
    my ($value, $key, $dbi, $infos) = @_;
226
    my ($table, $column) = @{$infos}{qw/table column/};
227
    
packaging one directory
yuki-kimoto authored on 2009-11-16
228
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
229
        return $value * 3;
230
    }
231
    return $value;
232
});
233
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}});
234
$select_query = $dbi->create_query($SELECT_TMPL->{0});
235
$result       = $dbi->execute($select_query);
236
$rows = $result->fetch_hash_all;
237
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
238

            
239
test 'Filter in';
240
$insert_tmpl  = "insert into table1 {insert key1 key2};";
241
$insert_query = $dbi->create_query($insert_tmpl);
242
$dbi->execute($insert_query, {key1 => 2, key2 => 4});
243
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
244
$select_query = $dbi->create_query($select_tmpl);
245
$select_query->bind_filter(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
246
    my ($value, $key, $dbi, $infos) = @_;
247
    my ($table, $column) = @{$infos}{qw/table column/};
248
    
packaging one directory
yuki-kimoto authored on 2009-11-16
249
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') {
250
        return $value * 2;
251
    }
252
    return $value;
253
});
254
$result = $dbi->execute($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}});
255
$rows = $result->fetch_hash_all;
256
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter");
257

            
258

            
259
test 'DBIx::Custom::SQL::Template basic tag';
260
$dbi->do($DROP_TABLE->{0});
261
$dbi->do($CREATE_TABLE->{1});
262
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
263
$sth->execute(1, 2, 3, 4, 5);
264
$sth->execute(6, 7, 8, 9, 10);
265

            
266
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
267
$query = $dbi->create_query($tmpl);
268
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
269
$rows = $result->fetch_hash_all;
270
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
271

            
272
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
273
$query = $dbi->create_query($tmpl);
274
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
275
$rows = $result->fetch_hash_all;
276
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table");
277

            
278
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
279
$query = $dbi->create_query($tmpl);
280
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5});
281
$rows = $result->fetch_hash_all;
282
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot");
283

            
284
$tmpl = "select * from table1 where {<= key1} and {like key2};";
285
$query = $dbi->create_query($tmpl);
286
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'});
287
$rows = $result->fetch_hash_all;
288
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
289

            
290
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
291
$query = $dbi->create_query($tmpl);
292
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => '%2%'}});
293
$rows = $result->fetch_hash_all;
294
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table");
295

            
296
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
297
$query = $dbi->create_query($tmpl);
298
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => '%2%'});
299
$rows = $result->fetch_hash_all;
300
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot");
301

            
302

            
303
test 'DIB::Custom::SQL::Template in tag';
304
$dbi->do($DROP_TABLE->{0});
305
$dbi->do($CREATE_TABLE->{1});
306
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
307
$sth->execute(1, 2, 3, 4, 5);
308
$sth->execute(6, 7, 8, 9, 10);
309

            
310
$tmpl = "select * from table1 where {in key1 2};";
311
$query = $dbi->create_query($tmpl);
312
$result = $dbi->execute($query, {key1 => [9, 1]});
313
$rows = $result->fetch_hash_all;
314
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
315

            
316
$tmpl = "select * from table1 where {in table1.key1 2};";
317
$query = $dbi->create_query($tmpl);
318
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}});
319
$rows = $result->fetch_hash_all;
320
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table");
321

            
322
$tmpl = "select * from table1 where {in table1.key1 2};";
323
$query = $dbi->create_query($tmpl);
324
$result = $dbi->execute($query, {'table1.key1' => [9, 1]});
325
$rows = $result->fetch_hash_all;
326
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot");
327

            
328

            
329
test 'DBIx::Custom::SQL::Template insert tag';
330
$dbi->do("delete from table1");
331
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
332
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
333

            
334
$result = $dbi->execute($SELECT_TMPL->{0});
335
$rows = $result->fetch_hash_all;
336
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
337

            
338
$dbi->do("delete from table1");
339
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
340
$result = $dbi->execute($SELECT_TMPL->{0});
341
$rows = $result->fetch_hash_all;
342
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert");
343

            
344
$dbi->do("delete from table1");
345
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
346
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
347
$result = $dbi->execute($SELECT_TMPL->{0});
348
$rows = $result->fetch_hash_all;
349
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
350

            
351
$dbi->do("delete from table1");
352
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
353
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5});
354
$result = $dbi->execute($SELECT_TMPL->{0});
355
$rows = $result->fetch_hash_all;
356
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot");
357

            
358
$dbi->do("delete from table1");
359
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}});
360
$result = $dbi->execute($SELECT_TMPL->{0});
361
$rows = $result->fetch_hash_all;
362
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name");
363

            
364
$dbi->do("delete from table1");
365
$dbi->execute($insert_tmpl, {'#insert' => {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}});
366
$result = $dbi->execute($SELECT_TMPL->{0});
367
$rows = $result->fetch_hash_all;
368
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name dot");
369

            
370

            
371
test 'DBIx::Custom::SQL::Template update tag';
372
$dbi->do("delete from table1");
373
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
374
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
375
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
376

            
377
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}';
378
$dbi->execute($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
379

            
380
$result = $dbi->execute($SELECT_TMPL->{0});
381
$rows = $result->fetch_hash_all;
382
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
383
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
384

            
385
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5});
386
$result = $dbi->execute($SELECT_TMPL->{0});
387
$rows = $result->fetch_hash_all;
388
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5},
389
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update");
390

            
391
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
392
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}});
393
$result = $dbi->execute($SELECT_TMPL->{0});
394
$rows = $result->fetch_hash_all;
395
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5},
396
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name");
397

            
398
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
399
$dbi->execute($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5});
400
$result = $dbi->execute($SELECT_TMPL->{0});
401
$rows = $result->fetch_hash_all;
402
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5},
403
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot");
404

            
405
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}});
406
$result = $dbi->execute($SELECT_TMPL->{0});
407
$rows = $result->fetch_hash_all;
408
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5},
409
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name");
410

            
411
$dbi->execute($update_tmpl, {'#update' => {'table1.key1' => 6, 'table1.key2' => 6, 'table1.key3' => 6, 'table1.key4' => 6}, 'table1.key5' => 5});
412
$result = $dbi->execute($SELECT_TMPL->{0});
413
$rows = $result->fetch_hash_all;
414
is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5},
415
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name dot");
416

            
417

            
418
test 'run_tansaction';
419
$dbi->do($DROP_TABLE->{0});
420
$dbi->do($CREATE_TABLE->{0});
421
$dbi->run_transaction(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
422
    my $dbi = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
423
    $insert_tmpl = 'insert into table1 {insert key1 key2}';
424
    $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
425
    $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
426
});
427
$result = $dbi->execute($SELECT_TMPL->{0});
428
$rows   = $result->fetch_hash_all;
429
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
430

            
431
$dbi->do($DROP_TABLE->{0});
432
$dbi->do($CREATE_TABLE->{0});
433
$dbi->dbh->{RaiseError} = 0;
434
eval{
435
    $dbi->run_transaction(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
436
        my $dbi = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
437
        $insert_tmpl = 'insert into table1 {insert key1 key2}';
438
        $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
439
        die "Fatal Error";
440
        $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
441
    })
442
};
443
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message");
444
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value");
445
$result = $dbi->execute($SELECT_TMPL->{0});
446
$rows   = $result->fetch_hash_all;
447
is_deeply($rows, [], "$test : rollback");
448

            
449

            
450
test 'Error case';
451
$dbi = DBIx::Custom->new;
452
eval{$dbi->run_transaction};
453
like($@, qr/Not yet connect to database/, "$test : Yet Connected");
454

            
455
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit');
456
eval{$dbi->connect;};
457
ok($@, "$test : connect error");
458

            
459
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
460
$dbi->connect;
461
$dbi->dbh->{AutoCommit} = 0;
462
eval{$dbi->run_transaction()};
463
like($@, qr/AutoCommit must be true before transaction start/,
464
         "$test : run_transaction auto commit is false");
465

            
466
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
467
$sql = 'laksjdf';
468
eval{$dbi->prepare($sql)};
469
like($@, qr/$sql/, "$test : prepare fail");
470

            
471
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
472
$sql = 'laksjdf';
473
eval{$dbi->do($sql, qw/1 2 3/)};
474
like($@, qr/$sql/, "$test : do fail");
475

            
476
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
477
eval{$dbi->create_query("{p }")};
478
ok($@, "$test : create_query invalid SQL template");
479

            
480
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
481
$dbi->do($CREATE_TABLE->{0});
482
$query = $dbi->create_query("select * from table1 where {= key1}");
483
eval{$dbi->execute($query, {key2 => 1})};
484
like($@, qr/Corresponding key is not found in your parameters/, 
485
        "$test : execute corresponding key not found");
486

            
487

            
488
test 'insert';
489
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
490
$dbi->do($CREATE_TABLE->{0});
491
$dbi->insert('table1', {key1 => 1, key2 => 2});
492
$dbi->insert('table1', {key1 => 3, key2 => 4});
493
$result = $dbi->execute($SELECT_TMPL->{0});
494
$rows   = $result->fetch_hash_all;
495
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
496

            
497
$dbi->do('delete from table1');
498
$dbi->insert('table1', {key1 => 1, key2 => 2}, sub {
499
    my $query = shift;
500
    $query->bind_filter(sub {
501
        my ($value, $key) = @_;
502
        if ($key eq 'key1') {
503
            return $value * 3;
504
        }
505
        return $value;
506
    });
507
});
508
$result = $dbi->execute($SELECT_TMPL->{0});
509
$rows   = $result->fetch_hash_all;
510
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
511

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

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

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

            
525

            
526
test 'update';
527
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
528
$dbi->do($CREATE_TABLE->{1});
529
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
530
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
531
$dbi->update('table1', {key2 => 11}, {key1 => 1});
532
$result = $dbi->execute($SELECT_TMPL->{0});
533
$rows   = $result->fetch_hash_all;
534
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
535
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
536
                  "$test : basic");
537
                  
538
$dbi->do("delete from table1");
539
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
540
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
541
$dbi->update('table1', {key2 => 12}, {key2 => 2, key3 => 3});
542
$result = $dbi->execute($SELECT_TMPL->{0});
543
$rows   = $result->fetch_hash_all;
544
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
545
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
546
                  "$test : update key same as search key");
547

            
548
$dbi->do("delete from table1");
549
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
550
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
551
$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub {
552
    my $query = shift;
553
    $query->bind_filter(sub {
554
        my ($value, $key) = @_;
555
        if ($key eq 'key2') {
556
            return $value * 2;
557
        }
558
        return $value;
559
    });
560
});
561
$result = $dbi->execute($SELECT_TMPL->{0});
562
$rows   = $result->fetch_hash_all;
563
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
564
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
565
                  "$test : query edit callback");
566

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
567
$dbi->update('table1', {key2 => 11}, {key1 => 1}, '   ', sub {
568
    my $query = shift;
569
    is($query->sql, 'update table1 set key2 = ? where key1 = ?    ;',
570
       "$test: append statement");
571
});
572

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

            
574
test 'update error';
575
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
576
$dbi->do($CREATE_TABLE->{1});
577
eval{$dbi->update('table1')};
578
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/,
579
         "$test : update key-value pairs not specified");
580

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

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

            
589

            
590
test 'update_all';
591
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
592
$dbi->do($CREATE_TABLE->{1});
593
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
594
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
595
$dbi->update_all('table1', {key2 => 10}, sub {
596
    my $query = shift;
597
    $query->bind_filter(sub {
598
        my ($value, $key) = @_;
599
        return $value * 2;
600
    })
601
});
602
$result = $dbi->execute($SELECT_TMPL->{0});
603
$rows   = $result->fetch_hash_all;
604
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
605
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
606
                  "$test : query edit callback");
607

            
608

            
609
test 'delete';
610
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
611
$dbi->do($CREATE_TABLE->{0});
612
$dbi->insert('table1', {key1 => 1, key2 => 2});
613
$dbi->insert('table1', {key1 => 3, key2 => 4});
614
$dbi->delete('table1', {key1 => 1});
615
$result = $dbi->execute($SELECT_TMPL->{0});
616
$rows   = $result->fetch_hash_all;
617
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
618

            
619
$dbi->do("delete from table1;");
620
$dbi->insert('table1', {key1 => 1, key2 => 2});
621
$dbi->insert('table1', {key1 => 3, key2 => 4});
622
$dbi->delete('table1', {key2 => 1}, sub {
623
    my $query = shift;
624
    $query->bind_filter(sub {
625
        my ($value, $key) = @_;
626
        return $value * 2;
627
    });
628
});
629
$result = $dbi->execute($SELECT_TMPL->{0});
630
$rows   = $result->fetch_hash_all;
631
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
632

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
633
$dbi->delete('table1', {key1 => 1}, '   ', sub {
634
    my $query = shift;
635
    is($query->sql, 'delete from table1 where key1 = ?    ;',
636
       "$test: append statement");
637
});
638

            
639

            
packaging one directory
yuki-kimoto authored on 2009-11-16
640
$dbi->delete_all('table1');
641
$dbi->insert('table1', {key1 => 1, key2 => 2});
642
$dbi->insert('table1', {key1 => 3, key2 => 4});
643
$dbi->delete('table1', {key1 => 1, key2 => 2});
644
$rows = $dbi->select('table1')->fetch_hash_all;
645
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
646

            
647

            
648
test 'delete error';
649
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
650
$dbi->do($CREATE_TABLE->{0});
651
eval{$dbi->delete('table1')};
652
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
653
         "$test : where key-value pairs not specified");
654

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

            
659

            
660
test 'delete_all';
661
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
662
$dbi->do($CREATE_TABLE->{0});
663
$dbi->insert('table1', {key1 => 1, key2 => 2});
664
$dbi->insert('table1', {key1 => 3, key2 => 4});
665
$dbi->delete_all('table1');
666
$result = $dbi->execute($SELECT_TMPL->{0});
667
$rows   = $result->fetch_hash_all;
668
is_deeply($rows, [], "$test : basic");
669

            
670

            
671
test 'select';
672
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
673
$dbi->do($CREATE_TABLE->{0});
674
$dbi->insert('table1', {key1 => 1, key2 => 2});
675
$dbi->insert('table1', {key1 => 3, key2 => 4});
676
$rows = $dbi->select('table1')->fetch_hash_all;
677
is_deeply($rows, [{key1 => 1, key2 => 2},
678
                  {key1 => 3, key2 => 4}], "$test : table");
679

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

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

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

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

            
692
$rows = $dbi->select('table1', {key1 => 2}, sub {
693
    my $query = shift;
694
    $query->bind_filter(sub {
695
        my ($value, $key) = @_;
696
        if ($key eq 'key1') {
697
            return $value - 1;
698
        }
699
        return $value;
700
    });
701
})->fetch_hash_all;
702
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
703

            
704
$dbi->do($CREATE_TABLE->{2});
705
$dbi->insert('table2', {key1 => 1, key3 => 5});
706
$rows = $dbi->select([qw/table1 table2/],
707
                     ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
708
                     {'table1.key2' => 2},
709
                     "where table1.key1 = table2.key1")->fetch_hash_all;
710
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
711

            
712
test 'Cache';
713
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
714
DBIx::Custom->query_cache_max(2);
715
$dbi->do($CREATE_TABLE->{0});
716
DBIx::Custom->delete_class_attr('_query_caches');
717
DBIx::Custom->delete_class_attr('_query_cache_keys');
718
$tmpls[0] = "insert into table1 {insert key1 key2}";
719
$queries[0] = $dbi->create_query($tmpls[0]);
720
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
721
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
722
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
723

            
724
$tmpls[1] = "select * from table1";
725
$queries[1] = $dbi->create_query($tmpls[1]);
726
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
727
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
728
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
729
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos second");
730
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
731

            
732
$tmpls[2] = "select key1, key2 from table1";
733
$queries[2] = $dbi->create_query($tmpls[2]);
734
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
735
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
736
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
737
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
738
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
739
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
740

            
741
$queries[1] = $dbi->create_query($tmpls[1]);
742
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
743
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
744
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
745
isnt(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
746
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
747
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
748
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
749

            
fix timeformat tests
yuki-kimoto authored on 2009-11-23
750
$query = $dbi->create_query($tmpls[0]);
751
$query->bind_filter('aaa');
752
$query = $dbi->create_query($tmpls[0]);
753
ok(!$query->bind_filter, "$test : only cached sql and key_infos");
754
$query->bind_filter('bbb');
755
$query = $dbi->create_query($tmpls[0]);
756
ok(!$query->bind_filter, "$test : only cached sql and key_infos");
757

            
758