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

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

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

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

            
511

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

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

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
553
$dbi->update('table1', {key2 => 11}, {key1 => 1}, '   ', sub {
554
    my $query = shift;
555
    is($query->sql, 'update table1 set key2 = ? where key1 = ?    ;',
556
       "$test: append statement");
557
});
558

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

            
560
test 'update error';
561
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
562
$dbi->do($CREATE_TABLE->{1});
563
eval{$dbi->update('table1')};
564
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/,
565
         "$test : update key-value pairs not specified");
566

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

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

            
575

            
576
test 'update_all';
577
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
578
$dbi->do($CREATE_TABLE->{1});
579
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
580
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
581
$dbi->update_all('table1', {key2 => 10}, sub {
582
    my $query = shift;
583
    $query->bind_filter(sub {
584
        my ($value, $key) = @_;
585
        return $value * 2;
586
    })
587
});
588
$result = $dbi->execute($SELECT_TMPL->{0});
589
$rows   = $result->fetch_hash_all;
590
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
591
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
592
                  "$test : query edit callback");
593

            
594

            
595
test 'delete';
596
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
597
$dbi->do($CREATE_TABLE->{0});
598
$dbi->insert('table1', {key1 => 1, key2 => 2});
599
$dbi->insert('table1', {key1 => 3, key2 => 4});
600
$dbi->delete('table1', {key1 => 1});
601
$result = $dbi->execute($SELECT_TMPL->{0});
602
$rows   = $result->fetch_hash_all;
603
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
604

            
605
$dbi->do("delete from table1;");
606
$dbi->insert('table1', {key1 => 1, key2 => 2});
607
$dbi->insert('table1', {key1 => 3, key2 => 4});
608
$dbi->delete('table1', {key2 => 1}, sub {
609
    my $query = shift;
610
    $query->bind_filter(sub {
611
        my ($value, $key) = @_;
612
        return $value * 2;
613
    });
614
});
615
$result = $dbi->execute($SELECT_TMPL->{0});
616
$rows   = $result->fetch_hash_all;
617
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
618

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
619
$dbi->delete('table1', {key1 => 1}, '   ', sub {
620
    my $query = shift;
621
    is($query->sql, 'delete from table1 where key1 = ?    ;',
622
       "$test: append statement");
623
});
624

            
625

            
packaging one directory
yuki-kimoto authored on 2009-11-16
626
$dbi->delete_all('table1');
627
$dbi->insert('table1', {key1 => 1, key2 => 2});
628
$dbi->insert('table1', {key1 => 3, key2 => 4});
629
$dbi->delete('table1', {key1 => 1, key2 => 2});
630
$rows = $dbi->select('table1')->fetch_hash_all;
631
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
632

            
633

            
634
test 'delete error';
635
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
636
$dbi->do($CREATE_TABLE->{0});
637
eval{$dbi->delete('table1')};
638
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
639
         "$test : where key-value pairs not specified");
640

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

            
645

            
646
test 'delete_all';
647
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
648
$dbi->do($CREATE_TABLE->{0});
649
$dbi->insert('table1', {key1 => 1, key2 => 2});
650
$dbi->insert('table1', {key1 => 3, key2 => 4});
651
$dbi->delete_all('table1');
652
$result = $dbi->execute($SELECT_TMPL->{0});
653
$rows   = $result->fetch_hash_all;
654
is_deeply($rows, [], "$test : basic");
655

            
656

            
657
test 'select';
658
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
659
$dbi->do($CREATE_TABLE->{0});
660
$dbi->insert('table1', {key1 => 1, key2 => 2});
661
$dbi->insert('table1', {key1 => 3, key2 => 4});
662
$rows = $dbi->select('table1')->fetch_hash_all;
663
is_deeply($rows, [{key1 => 1, key2 => 2},
664
                  {key1 => 3, key2 => 4}], "$test : table");
665

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

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

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

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

            
678
$rows = $dbi->select('table1', {key1 => 2}, sub {
679
    my $query = shift;
680
    $query->bind_filter(sub {
681
        my ($value, $key) = @_;
682
        if ($key eq 'key1') {
683
            return $value - 1;
684
        }
685
        return $value;
686
    });
687
})->fetch_hash_all;
688
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
689

            
690
$dbi->do($CREATE_TABLE->{2});
691
$dbi->insert('table2', {key1 => 1, key3 => 5});
692
$rows = $dbi->select([qw/table1 table2/],
693
                     ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
694
                     {'table1.key2' => 2},
695
                     "where table1.key1 = table2.key1")->fetch_hash_all;
696
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
697

            
698
test 'Cache';
699
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
700
DBIx::Custom->query_cache_max(2);
701
$dbi->do($CREATE_TABLE->{0});
702
DBIx::Custom->delete_class_attr('_query_caches');
703
DBIx::Custom->delete_class_attr('_query_cache_keys');
704
$tmpls[0] = "insert into table1 {insert key1 key2}";
705
$queries[0] = $dbi->create_query($tmpls[0]);
706
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
707
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
708
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
709

            
710
$tmpls[1] = "select * from table1";
711
$queries[1] = $dbi->create_query($tmpls[1]);
712
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
713
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
714
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
715
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos second");
716
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
717

            
718
$tmpls[2] = "select key1, key2 from table1";
719
$queries[2] = $dbi->create_query($tmpls[2]);
720
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
721
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
722
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
723
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
724
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
725
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
726

            
727
$queries[1] = $dbi->create_query($tmpls[1]);
728
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
729
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
730
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
731
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
732
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
733
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
734