DBIx-Custom / t / 02-sqlite.t /
Newer Older
335 lines | 10.641kb
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-30
21
# Varialbes for test
add prepare
yuki-kimoto authored on 2009-10-31
22
our $CREATE_TABLE = {
add tests
yuki-kimoto authored on 2009-10-31
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));'
add prepare
yuki-kimoto authored on 2009-10-31
25
};
26

            
add tests
yuki-kimoto authored on 2009-10-31
27
our $SELECT_TMPL = {
add tests
yuki-kimoto authored on 2009-10-31
28
    0 => 'select * from table1;'
add tests
yuki-kimoto authored on 2009-10-31
29
};
30

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

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

            
49

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

            
cleanup
yuki-kimoto authored on 2009-10-29
51
# Prepare table
52
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
53
$dbi->connect;
add prepare
yuki-kimoto authored on 2009-10-31
54
$dbi->do($CREATE_TABLE->{0});
55
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
cleanup
yuki-kimoto authored on 2009-10-29
56
$sth->execute(1, 2);
57
$sth->execute(3, 4);
add test
yuki-kimoto authored on 2009-10-18
58

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

            
add tests
yuki-kimoto authored on 2009-10-29
60
test 'DBI::Custom::Result test';
61
$tmpl = "select key1, key2 from table1";
62
$query = $dbi->create_query($tmpl);
63
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
64

            
65
@rows = ();
66
while (my $row = $result->fetch) {
67
    push @rows, [@$row];
add test module
yuki-kimoto authored on 2009-10-19
68
}
add tests
yuki-kimoto authored on 2009-10-29
69
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
70

            
71

            
add tests
yuki-kimoto authored on 2009-10-29
72
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
73
@rows = ();
74
while (my @row = $result->fetch) {
75
    push @rows, [@row];
add test module
yuki-kimoto authored on 2009-10-19
76
}
add tests
yuki-kimoto authored on 2009-10-29
77
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
add test module
yuki-kimoto authored on 2009-10-19
78

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

            
add tests
yuki-kimoto authored on 2009-10-29
80
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
81
@rows = ();
82
while (my $row = $result->fetch_hash) {
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, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
86

            
87

            
add tests
yuki-kimoto authored on 2009-10-29
88
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
89
@rows = ();
90
while (my %row = $result->fetch_hash) {
91
    push @rows, {%row};
add test module
yuki-kimoto authored on 2009-10-19
92
}
add tests
yuki-kimoto authored on 2009-10-29
93
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
94

            
95

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

            
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 = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
103
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context");
cleanup
yuki-kimoto authored on 2009-10-29
104

            
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 = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-29
108
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
109

            
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 = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
113
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context");
cleanup
yuki-kimoto authored on 2009-10-29
114

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

            
add tests
yuki-kimoto authored on 2009-10-31
116
test 'Insert query return value';
117
$dbi->reconnect;
118
$dbi->do($CREATE_TABLE->{0});
119
$tmpl = "insert into table1 {insert key1 key2}";
120
$query = $dbi->create_query($tmpl);
121
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2});
122
ok($ret_val, $test);
123

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

            
125
test 'Direct execute';
126
$dbi->reconnect;
127
$dbi->do($CREATE_TABLE->{0});
128
$insert_tmpl = "insert into table1 {insert key1 key2}";
129
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub {
130
    my $query = shift;
131
    $query->bind_filter(sub {
132
        my ($key, $value) = @_;
133
        if ($key eq 'key2') {
134
            return $value + 1;
135
        }
136
        return $value;
137
    });
138
});
139

            
140
$result = $dbi->execute($SELECT_TMPL->{0});
141

            
142
$rows = $result->fetch_all_hash;
143
is_deeply($rows, [{key1 => 1, key2 => 3}], $test);
144

            
145

            
cleanup#
yuki-kimoto authored on 2009-10-30
146
test 'Filter';
147
$dbi->reconnect;
add tests
yuki-kimoto authored on 2009-10-31
148
$dbi->do($CREATE_TABLE->{0});
cleanup#
yuki-kimoto authored on 2009-10-30
149

            
add tests
yuki-kimoto authored on 2009-10-31
150
$insert_tmpl  = "insert into table1 {insert key1 key2};";
add prepare
yuki-kimoto authored on 2009-10-31
151
$insert_query = $dbi->create_query($insert_tmpl);
152
$insert_query->bind_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
153
    my ($key, $value, $table, $column) = @_;
154
    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
add prepare
yuki-kimoto authored on 2009-10-31
155
        return $value * 2;
cleanup#
yuki-kimoto authored on 2009-10-30
156
    }
157
    return $value;
158
});
add tests
yuki-kimoto authored on 2009-10-31
159

            
add tests
yuki-kimoto authored on 2009-10-31
160
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
add prepare
yuki-kimoto authored on 2009-10-31
161

            
add tests
yuki-kimoto authored on 2009-10-31
162
$select_query = $dbi->create_query($SELECT_TMPL->{0});
add prepare
yuki-kimoto authored on 2009-10-31
163
$select_query->fetch_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
164
    my ($key, $value, $type, $sth, $i) = @_;
165
    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
add prepare
yuki-kimoto authored on 2009-10-31
166
        return $value * 3;
167
    }
168
    return $value;
169
});
170
$result = $dbi->execute($select_query);
cleanup#
yuki-kimoto authored on 2009-10-30
171

            
add prepare
yuki-kimoto authored on 2009-10-31
172
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
173
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
cleanup#
yuki-kimoto authored on 2009-10-30
174

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

            
176
$dbi->do("delete from table1;");
177
$insert_query->no_bind_filters('key1');
178
$select_query->no_fetch_filters('key2');
179

            
180
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
181
$result = $dbi->execute($select_query);
182
$rows = $result->fetch_all_hash;
183
is_deeply($rows, [{key1 => 1, key2 => 2}], 'no_fetch_filters no_bind_filters');
184

            
185

            
add tests
yuki-kimoto authored on 2009-10-31
186
$dbi->reconnect;
187
$dbi->do($CREATE_TABLE->{0});
188
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
189

            
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

            
199
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}});
200

            
201
$select_query = $dbi->create_query($SELECT_TMPL->{0});
202
$result       = $dbi->execute($select_query);
203
$rows = $result->fetch_all_hash;
204
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
205

            
add tests
yuki-kimoto authored on 2009-10-31
206
test 'DBI::Custom::SQL::Template';
207
$dbi->reconnect;
208
$dbi->do($CREATE_TABLE->{1});
209
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
210
$sth->execute(1, 2, 3, 4, 5);
211
$sth->execute(6, 7, 8, 9, 10);
add tests
yuki-kimoto authored on 2009-10-31
212

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

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

            
225

            
add tests
yuki-kimoto authored on 2009-10-31
226
$dbi->do("delete from table1");
227
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
228
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
cleanup
yuki-kimoto authored on 2009-10-29
229

            
add tests
yuki-kimoto authored on 2009-10-31
230
$result = $dbi->execute($SELECT_TMPL->{0});
add tests
yuki-kimoto authored on 2009-10-31
231
$rows = $result->fetch_all_hash;
232
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test insert tag");
cleanup
yuki-kimoto authored on 2009-10-29
233

            
add tests
yuki-kimoto authored on 2009-10-31
234
$dbi->do("delete from table1");
235
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
236
$result = $dbi->execute($SELECT_TMPL->{0});
237
$rows = $result->fetch_all_hash;
238
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test insert tag #insert");
239

            
240

            
241
$dbi->do("delete from table1");
242
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
243
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
244

            
245
$result = $dbi->execute($SELECT_TMPL->{0});
246
$rows = $result->fetch_all_hash;
247
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test insert tag with table name");
248

            
249
$dbi->do("delete from table1");
250
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}});
251
$result = $dbi->execute($SELECT_TMPL->{0});
252
$rows = $result->fetch_all_hash;
253
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test insert tag #insert with table name");
254

            
255

            
256

            
257

            
258
$dbi->do("delete from table1");
259
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
260
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
261
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
262

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

            
266
$result = $dbi->execute($SELECT_TMPL->{0});
267
$rows = $result->fetch_all_hash;
268
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
269
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test update tag");
270

            
271
__END__
272

            
273
$dbi->do("delete from table1");
274
$dbi->execute($update_tmpl, {'#update' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
275
$result = $dbi->execute($SELECT_TMPL->{0});
276
$rows = $result->fetch_all_hash;
277
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test update tag #update");
278

            
279

            
280
$dbi->do("delete from table1");
281
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
282
$dbi->execute($update_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
283

            
284
$result = $dbi->execute($SELECT_TMPL->{0});
285
$rows = $result->fetch_all_hash;
286
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test update tag with table name");
287

            
288
$dbi->do("delete from table1");
289
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}});
290
$result = $dbi->execute($SELECT_TMPL->{0});
291
$rows = $result->fetch_all_hash;
292
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test update tag #update with table name");
293

            
294

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

            
add tests
yuki-kimoto authored on 2009-10-31
296
__END__
cleanup
yuki-kimoto authored on 2009-10-29
297

            
298
# Insert values
299
$dbi = DBI::Custom->new;
add tests
yuki-kimoto authored on 2009-10-31
300
$tmpl   = "insert into table {insert}";
301
$params = {insert => {key1 => 'a', key2 => 'b'}};
cleanup
yuki-kimoto authored on 2009-10-29
302

            
303
$dbi->filters(filter => sub {
304
    my ($key, $value) = @_;
305
    if ($key eq 'key1' && $value eq 'a') {
306
        return uc $value;
cleanup
yuki-kimoto authored on 2009-10-19
307
    }
cleanup
yuki-kimoto authored on 2009-10-29
308
    return $value;
309
});
cleanup
yuki-kimoto authored on 2009-10-19
310
    
cleanup
yuki-kimoto authored on 2009-10-29
311
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
312
is($sql, "insert into table (key1, key2) values (?, ?);");
313
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
314

            
315
# Update set
316
$dbi = DBI::Custom->new;
add tests
yuki-kimoto authored on 2009-10-31
317
$tmpl   = "update table {update}";
318
$params = {update => {key1 => 'a', key2 => 'b'}};
cleanup
yuki-kimoto authored on 2009-10-29
319

            
320
$dbi->filters(filter => sub {
321
    my ($key, $value) = @_;
322
    if ($key eq 'key1' && $value eq 'a') {
323
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
324
    }
cleanup
yuki-kimoto authored on 2009-10-29
325
    return $value;
326
});
add tests
yuki-kimoto authored on 2009-10-19
327
    
cleanup
yuki-kimoto authored on 2009-10-29
328
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
329
is($sql, "update table set key1 = ?, key2 = ?;");
330
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
add tests
yuki-kimoto authored on 2009-10-19
331

            
cleanup#
yuki-kimoto authored on 2009-10-30
332
$dbi->disconnnect;
add test module
yuki-kimoto authored on 2009-10-19
333

            
add tests
yuki-kimoto authored on 2009-10-31
334
# Tag 'in' is easy to wrong
335