DBIx-Custom / t / 02-sqlite.t /
Newer Older
629 lines | 24.333kb
add test
yuki-kimoto authored on 2009-10-18
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 }
9
        or plan skip_all => 'DBD::SQLite >= 1.00 required';
10

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

            
cleanup
yuki-kimoto authored on 2009-10-29
15
# Function for test name
16
my $test;
17
sub test {
18
    $test = shift;
19
}
add test
yuki-kimoto authored on 2009-10-18
20

            
cleanup
yuki-kimoto authored on 2009-10-31
21

            
22

            
add tests
yuki-kimoto authored on 2009-11-02
23
# Constant varialbes for test
24
my $CREATE_TABLE = {
add tests
yuki-kimoto authored on 2009-10-31
25
    0 => 'create table table1 (key1 char(255), key2 char(255));',
26
    1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));'
add prepare
yuki-kimoto authored on 2009-10-31
27
};
28

            
add tests
yuki-kimoto authored on 2009-11-02
29
my $SELECT_TMPL = {
add tests
yuki-kimoto authored on 2009-10-31
30
    0 => 'select * from table1;'
add tests
yuki-kimoto authored on 2009-10-31
31
};
32

            
add tests
yuki-kimoto authored on 2009-11-02
33
my $DROP_TABLE = {
cleanup
yuki-kimoto authored on 2009-10-31
34
    0 => 'drop table table1'
35
};
36

            
add tests
yuki-kimoto authored on 2009-11-02
37
my $NEW_ARGS = {
38
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
39
};
40

            
41
# Variables for test
cleanup
yuki-kimoto authored on 2009-10-29
42
my $dbi;
43
my $sth;
44
my $tmpl;
cleanup#
yuki-kimoto authored on 2009-10-30
45
my $select_tmpl;
46
my $insert_tmpl;
add tests
yuki-kimoto authored on 2009-10-31
47
my $update_tmpl;
cleanup
yuki-kimoto authored on 2009-10-29
48
my $params;
49
my $sql;
50
my $result;
51
my @rows;
52
my $rows;
add tests
yuki-kimoto authored on 2009-10-29
53
my $query;
cleanup#
yuki-kimoto authored on 2009-10-30
54
my $select_query;
55
my $insert_query;
add tests
yuki-kimoto authored on 2009-10-31
56
my $update_query;
add prepare
yuki-kimoto authored on 2009-10-31
57
my $ret_val;
cleanup#
yuki-kimoto authored on 2009-10-30
58

            
cleanup
yuki-kimoto authored on 2009-10-29
59

            
cleanup
yuki-kimoto authored on 2009-10-31
60
test 'disconnect';
add tests
yuki-kimoto authored on 2009-11-02
61
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
62
$dbi->connect;
63
$dbi->disconnect;
64
ok(!$dbi->dbh, $test);
add tests
yuki-kimoto authored on 2009-10-31
65

            
add tests
yuki-kimoto authored on 2009-10-31
66

            
cleanup
yuki-kimoto authored on 2009-10-31
67
test 'connected';
add tests
yuki-kimoto authored on 2009-11-02
68
$dbi = DBI::Custom->new($NEW_ARGS->{0});
cleanup
yuki-kimoto authored on 2009-10-31
69
ok(!$dbi->connected, "$test : not connected");
70
$dbi->connect;
71
ok($dbi->connected, "$test : connected");
72

            
add tests
yuki-kimoto authored on 2009-10-31
73

            
74
test 'preapare';
add tests
yuki-kimoto authored on 2009-11-02
75
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
76
$sth = $dbi->prepare($CREATE_TABLE->{0});
77
ok($sth, "$test : auto connect");
78
$sth->execute;
79
$sth = $dbi->prepare($DROP_TABLE->{0});
80
ok($sth, "$test : basic");
81

            
82

            
83
test 'do';
add tests
yuki-kimoto authored on 2009-11-02
84
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
85
$ret_val = $dbi->do($CREATE_TABLE->{0});
86
ok(defined $ret_val, "$test : auto connect");
87
$ret_val = $dbi->do($DROP_TABLE->{0});
88
ok(defined $ret_val, "$test : basic");
89

            
90

            
cleanup
yuki-kimoto authored on 2009-10-29
91
# Prepare table
add tests
yuki-kimoto authored on 2009-11-02
92
$dbi = DBI::Custom->new($NEW_ARGS->{0});
cleanup
yuki-kimoto authored on 2009-10-29
93
$dbi->connect;
add prepare
yuki-kimoto authored on 2009-10-31
94
$dbi->do($CREATE_TABLE->{0});
95
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
cleanup
yuki-kimoto authored on 2009-10-29
96
$sth->execute(1, 2);
97
$sth->execute(3, 4);
add test
yuki-kimoto authored on 2009-10-18
98

            
add test module
yuki-kimoto authored on 2009-10-19
99

            
add tests
yuki-kimoto authored on 2009-10-29
100
test 'DBI::Custom::Result test';
101
$tmpl = "select key1, key2 from table1";
102
$query = $dbi->create_query($tmpl);
103
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
104

            
105
@rows = ();
106
while (my $row = $result->fetch) {
107
    push @rows, [@$row];
add test module
yuki-kimoto authored on 2009-10-19
108
}
add tests
yuki-kimoto authored on 2009-10-29
109
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
110

            
add tests
yuki-kimoto authored on 2009-10-29
111
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
112
@rows = ();
113
while (my @row = $result->fetch) {
114
    push @rows, [@row];
add test module
yuki-kimoto authored on 2009-10-19
115
}
add tests
yuki-kimoto authored on 2009-10-29
116
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
add test module
yuki-kimoto authored on 2009-10-19
117

            
add tests
yuki-kimoto authored on 2009-10-29
118
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
119
@rows = ();
120
while (my $row = $result->fetch_hash) {
121
    push @rows, {%$row};
add test module
yuki-kimoto authored on 2009-10-19
122
}
add tests
yuki-kimoto authored on 2009-10-29
123
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
124

            
add tests
yuki-kimoto authored on 2009-10-29
125
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
126
@rows = ();
127
while (my %row = $result->fetch_hash) {
128
    push @rows, {%row};
add test module
yuki-kimoto authored on 2009-10-19
129
}
add tests
yuki-kimoto authored on 2009-10-29
130
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
add test module
yuki-kimoto authored on 2009-10-19
131

            
add tests
yuki-kimoto authored on 2009-10-29
132
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
133
$rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
134
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
135

            
add tests
yuki-kimoto authored on 2009-10-29
136
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
137
@rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
138
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context");
cleanup
yuki-kimoto authored on 2009-10-29
139

            
add tests
yuki-kimoto authored on 2009-10-29
140
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
141
@rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-29
142
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
143

            
add tests
yuki-kimoto authored on 2009-10-29
144
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
145
@rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
146
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context");
cleanup
yuki-kimoto authored on 2009-10-29
147

            
add tests
yuki-kimoto authored on 2009-10-31
148

            
add tests
yuki-kimoto authored on 2009-10-31
149
test 'Insert query return value';
cleanup
yuki-kimoto authored on 2009-10-31
150
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
151
$dbi->do($CREATE_TABLE->{0});
152
$tmpl = "insert into table1 {insert key1 key2}";
153
$query = $dbi->create_query($tmpl);
154
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2});
155
ok($ret_val, $test);
156

            
add tests
yuki-kimoto authored on 2009-10-31
157

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

            
176

            
add tests
yuki-kimoto authored on 2009-10-31
177
test 'Filter basic';
cleanup
yuki-kimoto authored on 2009-10-31
178
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
179
$dbi->do($CREATE_TABLE->{0});
cleanup#
yuki-kimoto authored on 2009-10-30
180

            
add tests
yuki-kimoto authored on 2009-10-31
181
$insert_tmpl  = "insert into table1 {insert key1 key2};";
add prepare
yuki-kimoto authored on 2009-10-31
182
$insert_query = $dbi->create_query($insert_tmpl);
183
$insert_query->bind_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
184
    my ($key, $value, $table, $column) = @_;
185
    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
add prepare
yuki-kimoto authored on 2009-10-31
186
        return $value * 2;
cleanup#
yuki-kimoto authored on 2009-10-30
187
    }
188
    return $value;
189
});
add tests
yuki-kimoto authored on 2009-10-31
190
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
191
$select_query = $dbi->create_query($SELECT_TMPL->{0});
add prepare
yuki-kimoto authored on 2009-10-31
192
$select_query->fetch_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
193
    my ($key, $value, $type, $sth, $i) = @_;
194
    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
add prepare
yuki-kimoto authored on 2009-10-31
195
        return $value * 3;
196
    }
197
    return $value;
198
});
199
$result = $dbi->execute($select_query);
200
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
201
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
cleanup#
yuki-kimoto authored on 2009-10-30
202

            
add tests
yuki-kimoto authored on 2009-10-31
203
$dbi->do("delete from table1;");
204
$insert_query->no_bind_filters('key1');
205
$select_query->no_fetch_filters('key2');
206
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
207
$result = $dbi->execute($select_query);
208
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
209
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters");
add tests
yuki-kimoto authored on 2009-10-31
210

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

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

            
add tests
yuki-kimoto authored on 2009-10-31
245

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

            
add tests
yuki-kimoto authored on 2009-10-31
253
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
254
$query = $dbi->create_query($tmpl);
255
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
cleanup
yuki-kimoto authored on 2009-10-29
256
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
257
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
cleanup
yuki-kimoto authored on 2009-10-29
258

            
add tests
yuki-kimoto authored on 2009-10-31
259
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
260
$query = $dbi->create_query($tmpl);
261
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
262
$rows = $result->fetch_all_hash;
263
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table");
264

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

            
add tests
yuki-kimoto authored on 2009-10-31
271
$tmpl = "select * from table1 where {<= key1} and {like key2};";
272
$query = $dbi->create_query($tmpl);
273
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'});
274
$rows = $result->fetch_all_hash;
275
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
cleanup
yuki-kimoto authored on 2009-10-29
276

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

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

            
289

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

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

            
303
$tmpl = "select * from table1 where {in table1.key1 2};";
304
$query = $dbi->create_query($tmpl);
305
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}});
306
$rows = $result->fetch_all_hash;
307
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table");
add tests
yuki-kimoto authored on 2009-10-31
308

            
add tests
yuki-kimoto authored on 2009-10-31
309
$tmpl = "select * from table1 where {in table1.key1 2};";
310
$query = $dbi->create_query($tmpl);
311
$result = $dbi->execute($query, {'table1.key1' => [9, 1]});
312
$rows = $result->fetch_all_hash;
313
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot");
add tests
yuki-kimoto authored on 2009-10-31
314

            
cleanup
yuki-kimoto authored on 2009-10-29
315

            
add tests
yuki-kimoto authored on 2009-10-31
316
test 'DBI::Custom::SQL::Template insert tag';
add tests
yuki-kimoto authored on 2009-10-31
317
$dbi->do("delete from table1");
318
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
319
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
cleanup
yuki-kimoto authored on 2009-10-29
320

            
add tests
yuki-kimoto authored on 2009-10-31
321
$result = $dbi->execute($SELECT_TMPL->{0});
add tests
yuki-kimoto authored on 2009-10-31
322
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
323
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
cleanup
yuki-kimoto authored on 2009-10-29
324

            
add tests
yuki-kimoto authored on 2009-10-31
325
$dbi->do("delete from table1");
326
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
327
$result = $dbi->execute($SELECT_TMPL->{0});
328
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
329
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert");
add tests
yuki-kimoto authored on 2009-10-31
330

            
331
$dbi->do("delete from table1");
332
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
333
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
334
$result = $dbi->execute($SELECT_TMPL->{0});
335
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
336
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
337

            
add tests
yuki-kimoto authored on 2009-10-31
338
$dbi->do("delete from table1");
339
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
340
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5});
341
$result = $dbi->execute($SELECT_TMPL->{0});
342
$rows = $result->fetch_all_hash;
343
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot");
add tests
yuki-kimoto authored on 2009-10-31
344

            
345
$dbi->do("delete from table1");
346
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}});
347
$result = $dbi->execute($SELECT_TMPL->{0});
348
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
349
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name");
add tests
yuki-kimoto authored on 2009-10-31
350

            
add tests
yuki-kimoto authored on 2009-10-31
351
$dbi->do("delete from table1");
352
$dbi->execute($insert_tmpl, {'#insert' => {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}});
353
$result = $dbi->execute($SELECT_TMPL->{0});
354
$rows = $result->fetch_all_hash;
355
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name dot");
356

            
add tests
yuki-kimoto authored on 2009-10-31
357

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

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

            
367
$result = $dbi->execute($SELECT_TMPL->{0});
368
$rows = $result->fetch_all_hash;
369
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
370
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
add tests
yuki-kimoto authored on 2009-10-31
371

            
add tests
yuki-kimoto authored on 2009-10-31
372
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5});
add tests
yuki-kimoto authored on 2009-10-31
373
$result = $dbi->execute($SELECT_TMPL->{0});
374
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
375
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5},
376
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update");
add tests
yuki-kimoto authored on 2009-10-31
377

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

            
add tests
yuki-kimoto authored on 2009-10-31
385
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
386
$dbi->execute($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5});
add tests
yuki-kimoto authored on 2009-10-31
387
$result = $dbi->execute($SELECT_TMPL->{0});
388
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
389
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
390
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot");
cleanup
yuki-kimoto authored on 2009-10-29
391

            
add tests
yuki-kimoto authored on 2009-10-31
392
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}});
393
$result = $dbi->execute($SELECT_TMPL->{0});
394
$rows = $result->fetch_all_hash;
395
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
396
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name");
cleanup
yuki-kimoto authored on 2009-10-29
397

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

            
cleanup
yuki-kimoto authored on 2009-10-31
404

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

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

            
434

            
add tests
yuki-kimoto authored on 2009-10-31
435
test 'Error case';
436
$dbi = DBI::Custom->new;
437
eval{$dbi->run_tranzaction};
438
like($@, qr/Not yet connect to database/, "$test : Yet Connected");
439

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

            
add tests
yuki-kimoto authored on 2009-11-02
444
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
445
$dbi->connect;
446
$dbi->dbh->{AutoCommit} = 0;
447
eval{$dbi->run_tranzaction()};
448
like($@, qr/AutoCommit must be true before tranzaction start/,
449
         "$test : run_tranzaction auto commit is false");
450

            
add tests
yuki-kimoto authored on 2009-11-02
451
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
452
$sql = 'laksjdf';
453
eval{$dbi->prepare($sql)};
454
like($@, qr/$sql/, "$test : prepare fail");
455

            
add tests
yuki-kimoto authored on 2009-11-02
456
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
457
$sql = 'laksjdf';
458
eval{$dbi->do($sql, qw/1 2 3/)};
459
like($@, qr/$sql/, "$test : do fail");
460

            
add tests
yuki-kimoto authored on 2009-11-02
461
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
462
eval{$dbi->create_query("{p }")};
463
ok($@, "$test : create_query invalid SQL template");
464

            
add tests
yuki-kimoto authored on 2009-11-02
465
$dbi = DBI::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
466
$dbi->do($CREATE_TABLE->{0});
467
$query = $dbi->create_query("select * from table1 where {= key1}");
468
eval{$dbi->execute($query, {key2 => 1})};
469
like($@, qr/Corresponding key is not found in your parameters/, 
470
        "$test : execute corresponding key not found");
471

            
add tests
yuki-kimoto authored on 2009-11-02
472

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

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

            
add tests
yuki-kimoto authored on 2009-11-03
497

            
498
test 'insert error';
add tests
yuki-kimoto authored on 2009-11-02
499
eval{$dbi->insert('table1')};
add tests
yuki-kimoto authored on 2009-11-03
500
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed");
add tests
yuki-kimoto authored on 2009-11-02
501

            
502
eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, 'aaa')};
503
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref");
add tests
yuki-kimoto authored on 2009-11-03
504

            
505

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

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

            
547

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

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

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

            
563

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

            
582

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

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

            
607
test 'delete error';
608
$dbi = DBI::Custom->new($NEW_ARGS->{0});
609
$dbi->do($CREATE_TABLE->{0});
610
eval{$dbi->delete('table1')};
611
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
612
         "$test : where key-value pairs not specified");
613

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

            
618

            
619
test 'delete_all';
620
$dbi = DBI::Custom->new($NEW_ARGS->{0});
621
$dbi->do($CREATE_TABLE->{0});
622
$dbi->insert('table1', {key1 => 1, key2 => 2});
623
$dbi->insert('table1', {key1 => 3, key2 => 4});
624
$dbi->delete_all('table1');
625
$result = $dbi->execute($SELECT_TMPL->{0});
626
$rows   = $result->fetch_all_hash;
627
is_deeply($rows, [], "$test : basic");
628

            
629