DBIx-Custom / t / 02-sqlite.t /
Newer Older
466 lines | 18.443kb
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

            
cleanup#
yuki-kimoto authored on 2009-10-30
23
# Varialbes for test
add prepare
yuki-kimoto authored on 2009-10-31
24
our $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-10-31
29
our $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

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

            
cleanup
yuki-kimoto authored on 2009-10-29
37
my $dbi;
38
my $sth;
39
my $tmpl;
cleanup#
yuki-kimoto authored on 2009-10-30
40
my $select_tmpl;
41
my $insert_tmpl;
add tests
yuki-kimoto authored on 2009-10-31
42
my $update_tmpl;
cleanup
yuki-kimoto authored on 2009-10-29
43
my $params;
44
my $sql;
45
my $result;
46
my @rows;
47
my $rows;
add tests
yuki-kimoto authored on 2009-10-29
48
my $query;
cleanup#
yuki-kimoto authored on 2009-10-30
49
my $select_query;
50
my $insert_query;
add tests
yuki-kimoto authored on 2009-10-31
51
my $update_query;
add prepare
yuki-kimoto authored on 2009-10-31
52
my $ret_val;
cleanup#
yuki-kimoto authored on 2009-10-30
53

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

            
cleanup
yuki-kimoto authored on 2009-10-31
55
test 'disconnect';
add tests
yuki-kimoto authored on 2009-10-31
56
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
57
$dbi->connect;
58
$dbi->disconnect;
59
ok(!$dbi->dbh, $test);
add tests
yuki-kimoto authored on 2009-10-31
60

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

            
cleanup
yuki-kimoto authored on 2009-10-31
62
test 'connected';
63
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
64
ok(!$dbi->connected, "$test : not connected");
65
$dbi->connect;
66
ok($dbi->connected, "$test : connected");
67

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

            
69
test 'preapare';
70
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
71
$sth = $dbi->prepare($CREATE_TABLE->{0});
72
ok($sth, "$test : auto connect");
73
$sth->execute;
74
$sth = $dbi->prepare($DROP_TABLE->{0});
75
ok($sth, "$test : basic");
76

            
77

            
78
test 'do';
79
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
80
$ret_val = $dbi->do($CREATE_TABLE->{0});
81
ok(defined $ret_val, "$test : auto connect");
82
$ret_val = $dbi->do($DROP_TABLE->{0});
83
ok(defined $ret_val, "$test : basic");
84

            
85

            
cleanup
yuki-kimoto authored on 2009-10-29
86
# Prepare table
87
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
88
$dbi->connect;
add prepare
yuki-kimoto authored on 2009-10-31
89
$dbi->do($CREATE_TABLE->{0});
90
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
cleanup
yuki-kimoto authored on 2009-10-29
91
$sth->execute(1, 2);
92
$sth->execute(3, 4);
add test
yuki-kimoto authored on 2009-10-18
93

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

            
add tests
yuki-kimoto authored on 2009-10-29
95
test 'DBI::Custom::Result test';
96
$tmpl = "select key1, key2 from table1";
97
$query = $dbi->create_query($tmpl);
98
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
99

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

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

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

            
add tests
yuki-kimoto authored on 2009-10-29
120
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
121
@rows = ();
122
while (my %row = $result->fetch_hash) {
123
    push @rows, {%row};
add test module
yuki-kimoto authored on 2009-10-19
124
}
add tests
yuki-kimoto authored on 2009-10-29
125
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
126

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

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

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

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

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

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

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

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

            
171

            
add tests
yuki-kimoto authored on 2009-10-31
172
test 'Filter basic';
cleanup
yuki-kimoto authored on 2009-10-31
173
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
174
$dbi->do($CREATE_TABLE->{0});
cleanup#
yuki-kimoto authored on 2009-10-30
175

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

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

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

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

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

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

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

            
add tests
yuki-kimoto authored on 2009-10-31
254
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
255
$query = $dbi->create_query($tmpl);
256
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
257
$rows = $result->fetch_all_hash;
258
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table");
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, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5});
263
$rows = $result->fetch_all_hash;
264
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot");
265

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

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

            
284

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

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

            
298
$tmpl = "select * from table1 where {in table1.key1 2};";
299
$query = $dbi->create_query($tmpl);
300
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}});
301
$rows = $result->fetch_all_hash;
302
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
303

            
add tests
yuki-kimoto authored on 2009-10-31
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_all_hash;
308
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
309

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

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

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

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

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

            
add tests
yuki-kimoto authored on 2009-10-31
333
$dbi->do("delete from table1");
334
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
335
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5});
336
$result = $dbi->execute($SELECT_TMPL->{0});
337
$rows = $result->fetch_all_hash;
338
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
339

            
340
$dbi->do("delete from table1");
341
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}});
342
$result = $dbi->execute($SELECT_TMPL->{0});
343
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
344
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
345

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

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

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

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

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

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

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

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

            
add tests
yuki-kimoto authored on 2009-10-31
387
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}});
388
$result = $dbi->execute($SELECT_TMPL->{0});
389
$rows = $result->fetch_all_hash;
390
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
391
                  {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
392

            
add tests
yuki-kimoto authored on 2009-10-31
393
$dbi->execute($update_tmpl, {'#update' => {'table1.key1' => 6, 'table1.key2' => 6, 'table1.key3' => 6, 'table1.key4' => 6}, 'table1.key5' => 5});
394
$result = $dbi->execute($SELECT_TMPL->{0});
395
$rows = $result->fetch_all_hash;
396
is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
397
                  {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
398

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

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

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

            
429

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

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

            
439
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
440
$dbi->connect;
441
$dbi->dbh->{AutoCommit} = 0;
442
eval{$dbi->run_tranzaction()};
443
like($@, qr/AutoCommit must be true before tranzaction start/,
444
         "$test : run_tranzaction auto commit is false");
445

            
446
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
447
$sql = 'laksjdf';
448
eval{$dbi->prepare($sql)};
449
like($@, qr/$sql/, "$test : prepare fail");
450

            
451
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
452
$sql = 'laksjdf';
453
eval{$dbi->do($sql, qw/1 2 3/)};
454
like($@, qr/$sql/, "$test : do fail");
455

            
456
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
457
eval{$dbi->create_query("{p }")};
458
ok($@, "$test : create_query invalid SQL template");
459

            
460
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
461
$dbi->do($CREATE_TABLE->{0});
462
$query = $dbi->create_query("select * from table1 where {= key1}");
463
eval{$dbi->execute($query, {key2 => 1})};
464
like($@, qr/Corresponding key is not found in your parameters/, 
465
        "$test : execute corresponding key not found");
466