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

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

            
cleanup
yuki-kimoto authored on 2009-10-29
67
# Prepare table
68
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
69
$dbi->connect;
add prepare
yuki-kimoto authored on 2009-10-31
70
$dbi->do($CREATE_TABLE->{0});
71
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
cleanup
yuki-kimoto authored on 2009-10-29
72
$sth->execute(1, 2);
73
$sth->execute(3, 4);
add test
yuki-kimoto authored on 2009-10-18
74

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

            
add tests
yuki-kimoto authored on 2009-10-29
76
test 'DBI::Custom::Result test';
77
$tmpl = "select key1, key2 from table1";
78
$query = $dbi->create_query($tmpl);
79
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
80

            
81
@rows = ();
82
while (my $row = $result->fetch) {
83
    push @rows, [@$row];
add test module
yuki-kimoto authored on 2009-10-19
84
}
add tests
yuki-kimoto authored on 2009-10-29
85
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
86

            
add tests
yuki-kimoto authored on 2009-10-29
87
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
88
@rows = ();
89
while (my @row = $result->fetch) {
90
    push @rows, [@row];
add test module
yuki-kimoto authored on 2009-10-19
91
}
add tests
yuki-kimoto authored on 2009-10-29
92
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
add test module
yuki-kimoto authored on 2009-10-19
93

            
add tests
yuki-kimoto authored on 2009-10-29
94
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
95
@rows = ();
96
while (my $row = $result->fetch_hash) {
97
    push @rows, {%$row};
add test module
yuki-kimoto authored on 2009-10-19
98
}
add tests
yuki-kimoto authored on 2009-10-29
99
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
100

            
add tests
yuki-kimoto authored on 2009-10-29
101
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
102
@rows = ();
103
while (my %row = $result->fetch_hash) {
104
    push @rows, {%row};
add test module
yuki-kimoto authored on 2009-10-19
105
}
add tests
yuki-kimoto authored on 2009-10-29
106
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
107

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

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

            
add tests
yuki-kimoto authored on 2009-10-29
116
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
117
@rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-29
118
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_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 = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
122
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context");
cleanup
yuki-kimoto authored on 2009-10-29
123

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

            
add tests
yuki-kimoto authored on 2009-10-31
125
test 'Insert query return value';
cleanup
yuki-kimoto authored on 2009-10-31
126
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
127
$dbi->do($CREATE_TABLE->{0});
128
$tmpl = "insert into table1 {insert key1 key2}";
129
$query = $dbi->create_query($tmpl);
130
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2});
131
ok($ret_val, $test);
132

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

            
134
test 'Direct execute';
cleanup
yuki-kimoto authored on 2009-10-31
135
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
136
$dbi->do($CREATE_TABLE->{0});
137
$insert_tmpl = "insert into table1 {insert key1 key2}";
138
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub {
139
    my $query = shift;
140
    $query->bind_filter(sub {
141
        my ($key, $value) = @_;
142
        if ($key eq 'key2') {
143
            return $value + 1;
144
        }
145
        return $value;
146
    });
147
});
148
$result = $dbi->execute($SELECT_TMPL->{0});
149
$rows = $result->fetch_all_hash;
150
is_deeply($rows, [{key1 => 1, key2 => 3}], $test);
151

            
152

            
add tests
yuki-kimoto authored on 2009-10-31
153
test 'Filter basic';
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});
cleanup#
yuki-kimoto authored on 2009-10-30
156

            
add tests
yuki-kimoto authored on 2009-10-31
157
$insert_tmpl  = "insert into table1 {insert key1 key2};";
add prepare
yuki-kimoto authored on 2009-10-31
158
$insert_query = $dbi->create_query($insert_tmpl);
159
$insert_query->bind_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
160
    my ($key, $value, $table, $column) = @_;
161
    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
add prepare
yuki-kimoto authored on 2009-10-31
162
        return $value * 2;
cleanup#
yuki-kimoto authored on 2009-10-30
163
    }
164
    return $value;
165
});
add tests
yuki-kimoto authored on 2009-10-31
166
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
167
$select_query = $dbi->create_query($SELECT_TMPL->{0});
add prepare
yuki-kimoto authored on 2009-10-31
168
$select_query->fetch_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
169
    my ($key, $value, $type, $sth, $i) = @_;
170
    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
add prepare
yuki-kimoto authored on 2009-10-31
171
        return $value * 3;
172
    }
173
    return $value;
174
});
175
$result = $dbi->execute($select_query);
176
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
177
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
cleanup#
yuki-kimoto authored on 2009-10-30
178

            
add tests
yuki-kimoto authored on 2009-10-31
179
$dbi->do("delete from table1;");
180
$insert_query->no_bind_filters('key1');
181
$select_query->no_fetch_filters('key2');
182
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
183
$result = $dbi->execute($select_query);
184
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
185
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters");
add tests
yuki-kimoto authored on 2009-10-31
186

            
cleanup
yuki-kimoto authored on 2009-10-31
187
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
188
$dbi->do($CREATE_TABLE->{0});
189
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
190
$insert_query = $dbi->create_query($insert_tmpl);
191
$insert_query->bind_filter(sub {
192
    my ($key, $value, $table, $column) = @_;
193
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
194
        return $value * 3;
195
    }
196
    return $value;
197
});
198
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}});
199
$select_query = $dbi->create_query($SELECT_TMPL->{0});
200
$result       = $dbi->execute($select_query);
201
$rows = $result->fetch_all_hash;
202
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
203

            
add tests
yuki-kimoto authored on 2009-10-31
204
test 'Filter in';
205
$insert_tmpl  = "insert into table1 {insert key1 key2};";
206
$insert_query = $dbi->create_query($insert_tmpl);
207
$dbi->execute($insert_query, {key1 => 2, key2 => 4});
208
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
209
$select_query = $dbi->create_query($select_tmpl);
210
$select_query->bind_filter(sub {
211
    my ($key, $value, $table, $column) = @_;
212
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') {
213
        return $value * 2;
214
    }
215
    return $value;
216
});
217
$result = $dbi->execute($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}});
218
$rows = $result->fetch_all_hash;
219
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter");
220

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

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

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

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

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

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

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

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

            
265

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

            
273
$tmpl = "select * from table1 where {in key1 2};";
274
$query = $dbi->create_query($tmpl);
275
$result = $dbi->execute($query, {key1 => [9, 1]});
276
$rows = $result->fetch_all_hash;
277
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
278

            
279
$tmpl = "select * from table1 where {in table1.key1 2};";
280
$query = $dbi->create_query($tmpl);
281
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}});
282
$rows = $result->fetch_all_hash;
283
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
284

            
add tests
yuki-kimoto authored on 2009-10-31
285
$tmpl = "select * from table1 where {in table1.key1 2};";
286
$query = $dbi->create_query($tmpl);
287
$result = $dbi->execute($query, {'table1.key1' => [9, 1]});
288
$rows = $result->fetch_all_hash;
289
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
290

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

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

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

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

            
307
$dbi->do("delete from table1");
308
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
309
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
310
$result = $dbi->execute($SELECT_TMPL->{0});
311
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
312
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
313

            
add tests
yuki-kimoto authored on 2009-10-31
314
$dbi->do("delete from table1");
315
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
316
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5});
317
$result = $dbi->execute($SELECT_TMPL->{0});
318
$rows = $result->fetch_all_hash;
319
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
320

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

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

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

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

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

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

            
add tests
yuki-kimoto authored on 2009-10-31
348
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5});
add tests
yuki-kimoto authored on 2009-10-31
349
$result = $dbi->execute($SELECT_TMPL->{0});
350
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
351
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5},
352
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update");
add tests
yuki-kimoto authored on 2009-10-31
353

            
add tests
yuki-kimoto authored on 2009-10-31
354
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
355
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}});
add tests
yuki-kimoto authored on 2009-10-31
356
$result = $dbi->execute($SELECT_TMPL->{0});
357
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
358
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5},
359
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name");
add tests
yuki-kimoto authored on 2009-10-31
360

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

            
add tests
yuki-kimoto authored on 2009-10-31
368
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}});
369
$result = $dbi->execute($SELECT_TMPL->{0});
370
$rows = $result->fetch_all_hash;
371
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
372
                  {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
373

            
add tests
yuki-kimoto authored on 2009-10-31
374
$dbi->execute($update_tmpl, {'#update' => {'table1.key1' => 6, 'table1.key2' => 6, 'table1.key3' => 6, 'table1.key4' => 6}, 'table1.key5' => 5});
375
$result = $dbi->execute($SELECT_TMPL->{0});
376
$rows = $result->fetch_all_hash;
377
is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
378
                  {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
379

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

            
381
test 'run_tansaction';
382
$dbi->do($DROP_TABLE->{0});
383
$dbi->do($CREATE_TABLE->{0});
384
$dbi->run_tranzaction(sub {
385
    $insert_tmpl = 'insert into table1 {insert key1 key2}';
386
    $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
387
    $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
388
});
389
$result = $dbi->execute($SELECT_TMPL->{0});
390
$rows   = $result->fetch_all_hash;
391
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
392

            
393
$dbi->do($DROP_TABLE->{0});
394
$dbi->do($CREATE_TABLE->{0});
395
$dbi->dbh->{RaiseError} = 0;
396
eval{
397
    $dbi->run_tranzaction(sub {
398
        $insert_tmpl = 'insert into table1 {insert key1 key2}';
399
        $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
400
        die "Fatal Error";
401
        $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
402
    })
403
};
404
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message");
405
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value");
406
$result = $dbi->execute($SELECT_TMPL->{0});
407
$rows   = $result->fetch_all_hash;
408
is_deeply($rows, [], "$test : rollback");
409

            
410