DBIx-Custom / t / dbi-custom-core-sqlite.t /
Newer Older
716 lines | 29.156kb
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 {
185
    my ($value, $key, $table, $column) = @_;
186
    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
187
        return $value * 2;
188
    }
189
    return $value;
190
});
191
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
192
$select_query = $dbi->create_query($SELECT_TMPL->{0});
193
$select_query->fetch_filter(sub {
194
    my ($value, $key, $type, $sth, $i) = @_;
195
    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
196
        return $value * 3;
197
    }
198
    return $value;
199
});
200
$result = $dbi->execute($select_query);
201
$rows = $result->fetch_hash_all;
202
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
203

            
204
$dbi->do("delete from table1;");
205
$insert_query->no_bind_filters('key1');
206
$select_query->no_fetch_filters('key2');
207
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
208
$result = $dbi->execute($select_query);
209
$rows = $result->fetch_hash_all;
210
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters");
211

            
212
$dbi->do($DROP_TABLE->{0});
213
$dbi->do($CREATE_TABLE->{0});
214
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
215
$insert_query = $dbi->create_query($insert_tmpl);
216
$insert_query->bind_filter(sub {
217
    my ($value, $key, $table, $column) = @_;
218
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
219
        return $value * 3;
220
    }
221
    return $value;
222
});
223
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}});
224
$select_query = $dbi->create_query($SELECT_TMPL->{0});
225
$result       = $dbi->execute($select_query);
226
$rows = $result->fetch_hash_all;
227
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
228

            
229
test 'Filter in';
230
$insert_tmpl  = "insert into table1 {insert key1 key2};";
231
$insert_query = $dbi->create_query($insert_tmpl);
232
$dbi->execute($insert_query, {key1 => 2, key2 => 4});
233
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
234
$select_query = $dbi->create_query($select_tmpl);
235
$select_query->bind_filter(sub {
236
    my ($value, $key, $table, $column) = @_;
237
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') {
238
        return $value * 2;
239
    }
240
    return $value;
241
});
242
$result = $dbi->execute($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}});
243
$rows = $result->fetch_hash_all;
244
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter");
245

            
246

            
247
test 'DBIx::Custom::SQL::Template basic tag';
248
$dbi->do($DROP_TABLE->{0});
249
$dbi->do($CREATE_TABLE->{1});
250
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
251
$sth->execute(1, 2, 3, 4, 5);
252
$sth->execute(6, 7, 8, 9, 10);
253

            
254
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
255
$query = $dbi->create_query($tmpl);
256
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
257
$rows = $result->fetch_hash_all;
258
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
259

            
260
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
261
$query = $dbi->create_query($tmpl);
262
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
263
$rows = $result->fetch_hash_all;
264
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table");
265

            
266
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
267
$query = $dbi->create_query($tmpl);
268
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.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 with table dot");
271

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

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

            
284
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
285
$query = $dbi->create_query($tmpl);
286
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.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 with table dot");
289

            
290

            
291
test 'DIB::Custom::SQL::Template in tag';
292
$dbi->do($DROP_TABLE->{0});
293
$dbi->do($CREATE_TABLE->{1});
294
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
295
$sth->execute(1, 2, 3, 4, 5);
296
$sth->execute(6, 7, 8, 9, 10);
297

            
298
$tmpl = "select * from table1 where {in key1 2};";
299
$query = $dbi->create_query($tmpl);
300
$result = $dbi->execute($query, {key1 => [9, 1]});
301
$rows = $result->fetch_hash_all;
302
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
303

            
304
$tmpl = "select * from table1 where {in table1.key1 2};";
305
$query = $dbi->create_query($tmpl);
306
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}});
307
$rows = $result->fetch_hash_all;
308
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table");
309

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

            
316

            
317
test 'DBIx::Custom::SQL::Template insert tag';
318
$dbi->do("delete from table1");
319
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
320
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
321

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

            
326
$dbi->do("delete from table1");
327
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
328
$result = $dbi->execute($SELECT_TMPL->{0});
329
$rows = $result->fetch_hash_all;
330
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert");
331

            
332
$dbi->do("delete from table1");
333
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
334
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
335
$result = $dbi->execute($SELECT_TMPL->{0});
336
$rows = $result->fetch_hash_all;
337
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
338

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

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

            
352
$dbi->do("delete from table1");
353
$dbi->execute($insert_tmpl, {'#insert' => {'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 : #insert with table name dot");
357

            
358

            
359
test 'DBIx::Custom::SQL::Template update tag';
360
$dbi->do("delete from table1");
361
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
362
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
363
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
364

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

            
368
$result = $dbi->execute($SELECT_TMPL->{0});
369
$rows = $result->fetch_hash_all;
370
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
371
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
372

            
373
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5});
374
$result = $dbi->execute($SELECT_TMPL->{0});
375
$rows = $result->fetch_hash_all;
376
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5},
377
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update");
378

            
379
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
380
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}});
381
$result = $dbi->execute($SELECT_TMPL->{0});
382
$rows = $result->fetch_hash_all;
383
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5},
384
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name");
385

            
386
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
387
$dbi->execute($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5});
388
$result = $dbi->execute($SELECT_TMPL->{0});
389
$rows = $result->fetch_hash_all;
390
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5},
391
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot");
392

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

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

            
405

            
406
test 'run_tansaction';
407
$dbi->do($DROP_TABLE->{0});
408
$dbi->do($CREATE_TABLE->{0});
409
$dbi->run_transaction(sub {
410
    $insert_tmpl = 'insert into table1 {insert key1 key2}';
411
    $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
412
    $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
413
});
414
$result = $dbi->execute($SELECT_TMPL->{0});
415
$rows   = $result->fetch_hash_all;
416
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
417

            
418
$dbi->do($DROP_TABLE->{0});
419
$dbi->do($CREATE_TABLE->{0});
420
$dbi->dbh->{RaiseError} = 0;
421
eval{
422
    $dbi->run_transaction(sub {
423
        $insert_tmpl = 'insert into table1 {insert key1 key2}';
424
        $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
425
        die "Fatal Error";
426
        $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
427
    })
428
};
429
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message");
430
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value");
431
$result = $dbi->execute($SELECT_TMPL->{0});
432
$rows   = $result->fetch_hash_all;
433
is_deeply($rows, [], "$test : rollback");
434

            
435

            
436
test 'Error case';
437
$dbi = DBIx::Custom->new;
438
eval{$dbi->run_transaction};
439
like($@, qr/Not yet connect to database/, "$test : Yet Connected");
440

            
441
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit');
442
eval{$dbi->connect;};
443
ok($@, "$test : connect error");
444

            
445
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
446
$dbi->connect;
447
$dbi->dbh->{AutoCommit} = 0;
448
eval{$dbi->run_transaction()};
449
like($@, qr/AutoCommit must be true before transaction start/,
450
         "$test : run_transaction auto commit is false");
451

            
452
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
453
$sql = 'laksjdf';
454
eval{$dbi->prepare($sql)};
455
like($@, qr/$sql/, "$test : prepare fail");
456

            
457
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
458
$sql = 'laksjdf';
459
eval{$dbi->do($sql, qw/1 2 3/)};
460
like($@, qr/$sql/, "$test : do fail");
461

            
462
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
463
eval{$dbi->create_query("{p }")};
464
ok($@, "$test : create_query invalid SQL template");
465

            
466
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
467
$dbi->do($CREATE_TABLE->{0});
468
$query = $dbi->create_query("select * from table1 where {= key1}");
469
eval{$dbi->execute($query, {key2 => 1})};
470
like($@, qr/Corresponding key is not found in your parameters/, 
471
        "$test : execute corresponding key not found");
472

            
473

            
474
test 'insert';
475
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
476
$dbi->do($CREATE_TABLE->{0});
477
$dbi->insert('table1', {key1 => 1, key2 => 2});
478
$dbi->insert('table1', {key1 => 3, key2 => 4});
479
$result = $dbi->execute($SELECT_TMPL->{0});
480
$rows   = $result->fetch_hash_all;
481
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
482

            
483
$dbi->do('delete from table1');
484
$dbi->insert('table1', {key1 => 1, key2 => 2}, sub {
485
    my $query = shift;
486
    $query->bind_filter(sub {
487
        my ($value, $key) = @_;
488
        if ($key eq 'key1') {
489
            return $value * 3;
490
        }
491
        return $value;
492
    });
493
});
494
$result = $dbi->execute($SELECT_TMPL->{0});
495
$rows   = $result->fetch_hash_all;
496
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
497

            
498

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

            
503
eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, 'aaa')};
504
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref");
505

            
506

            
507
test 'update';
508
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
509
$dbi->do($CREATE_TABLE->{1});
510
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
511
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
512
$dbi->update('table1', {key2 => 11}, {key1 => 1});
513
$result = $dbi->execute($SELECT_TMPL->{0});
514
$rows   = $result->fetch_hash_all;
515
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
516
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
517
                  "$test : basic");
518
                  
519
$dbi->do("delete from table1");
520
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
521
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
522
$dbi->update('table1', {key2 => 12}, {key2 => 2, key3 => 3});
523
$result = $dbi->execute($SELECT_TMPL->{0});
524
$rows   = $result->fetch_hash_all;
525
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
526
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
527
                  "$test : update key same as search key");
528

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

            
548

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

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

            
560
eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, 'aaa')};
561
like($@, qr/Query edit callback must be code reference/, 
562
         "$test : query edit callback not code reference");
563

            
564

            
565
test 'update_all';
566
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
567
$dbi->do($CREATE_TABLE->{1});
568
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
569
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
570
$dbi->update_all('table1', {key2 => 10}, sub {
571
    my $query = shift;
572
    $query->bind_filter(sub {
573
        my ($value, $key) = @_;
574
        return $value * 2;
575
    })
576
});
577
$result = $dbi->execute($SELECT_TMPL->{0});
578
$rows   = $result->fetch_hash_all;
579
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
580
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
581
                  "$test : query edit callback");
582

            
583

            
584
test 'delete';
585
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
586
$dbi->do($CREATE_TABLE->{0});
587
$dbi->insert('table1', {key1 => 1, key2 => 2});
588
$dbi->insert('table1', {key1 => 3, key2 => 4});
589
$dbi->delete('table1', {key1 => 1});
590
$result = $dbi->execute($SELECT_TMPL->{0});
591
$rows   = $result->fetch_hash_all;
592
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
593

            
594
$dbi->do("delete from table1;");
595
$dbi->insert('table1', {key1 => 1, key2 => 2});
596
$dbi->insert('table1', {key1 => 3, key2 => 4});
597
$dbi->delete('table1', {key2 => 1}, sub {
598
    my $query = shift;
599
    $query->bind_filter(sub {
600
        my ($value, $key) = @_;
601
        return $value * 2;
602
    });
603
});
604
$result = $dbi->execute($SELECT_TMPL->{0});
605
$rows   = $result->fetch_hash_all;
606
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
607

            
608
$dbi->delete_all('table1');
609
$dbi->insert('table1', {key1 => 1, key2 => 2});
610
$dbi->insert('table1', {key1 => 3, key2 => 4});
611
$dbi->delete('table1', {key1 => 1, key2 => 2});
612
$rows = $dbi->select('table1')->fetch_hash_all;
613
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
614

            
615

            
616
test 'delete error';
617
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
618
$dbi->do($CREATE_TABLE->{0});
619
eval{$dbi->delete('table1')};
620
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
621
         "$test : where key-value pairs not specified");
622

            
623
eval{$dbi->delete('table1', {key1 => 1}, 'aaa')};
624
like($@, qr/Query edit callback must be code reference/, 
625
         "$test : query edit callback not code ref");
626

            
627

            
628
test 'delete_all';
629
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
630
$dbi->do($CREATE_TABLE->{0});
631
$dbi->insert('table1', {key1 => 1, key2 => 2});
632
$dbi->insert('table1', {key1 => 3, key2 => 4});
633
$dbi->delete_all('table1');
634
$result = $dbi->execute($SELECT_TMPL->{0});
635
$rows   = $result->fetch_hash_all;
636
is_deeply($rows, [], "$test : basic");
637

            
638

            
639
test 'select';
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
$rows = $dbi->select('table1')->fetch_hash_all;
645
is_deeply($rows, [{key1 => 1, key2 => 2},
646
                  {key1 => 3, key2 => 4}], "$test : table");
647

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

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

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

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

            
660
$rows = $dbi->select('table1', {key1 => 2}, sub {
661
    my $query = shift;
662
    $query->bind_filter(sub {
663
        my ($value, $key) = @_;
664
        if ($key eq 'key1') {
665
            return $value - 1;
666
        }
667
        return $value;
668
    });
669
})->fetch_hash_all;
670
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
671

            
672
$dbi->do($CREATE_TABLE->{2});
673
$dbi->insert('table2', {key1 => 1, key3 => 5});
674
$rows = $dbi->select([qw/table1 table2/],
675
                     ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
676
                     {'table1.key2' => 2},
677
                     "where table1.key1 = table2.key1")->fetch_hash_all;
678
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
679

            
680
test 'Cache';
681
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
682
DBIx::Custom->query_cache_max(2);
683
$dbi->do($CREATE_TABLE->{0});
684
DBIx::Custom->delete_class_attr('_query_caches');
685
DBIx::Custom->delete_class_attr('_query_cache_keys');
686
$tmpls[0] = "insert into table1 {insert key1 key2}";
687
$queries[0] = $dbi->create_query($tmpls[0]);
688
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
689
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
690
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
691

            
692
$tmpls[1] = "select * from table1";
693
$queries[1] = $dbi->create_query($tmpls[1]);
694
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
695
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
696
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
697
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos second");
698
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
699

            
700
$tmpls[2] = "select key1, key2 from table1";
701
$queries[2] = $dbi->create_query($tmpls[2]);
702
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
703
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
704
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
705
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
706
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
707
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
708

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