DBIx-Custom / t / 02-sqlite.t /
Newer Older
260 lines | 7.091kb
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 = {
23
    0 => 'create table table1 (key1 char(255), key2 char(255));'
24
};
25

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

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

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

            
48

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

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

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

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

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

            
70

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

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

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

            
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_hash) {
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, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
add test module
yuki-kimoto authored on 2009-10-19
93

            
94

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

            
99

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

            
104

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

            
109

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

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

            
cleanup#
yuki-kimoto authored on 2009-10-30
122
test 'Filter';
123
$dbi->reconnect;
add tests
yuki-kimoto authored on 2009-10-31
124
$dbi->do($CREATE_TABLE->{0});
cleanup#
yuki-kimoto authored on 2009-10-30
125

            
add tests
yuki-kimoto authored on 2009-10-31
126
$insert_tmpl  = "insert into table1 {insert key1 key2};";
add prepare
yuki-kimoto authored on 2009-10-31
127
$insert_query = $dbi->create_query($insert_tmpl);
128
$insert_query->bind_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
129
    my ($key, $value, $table, $column) = @_;
130
    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
add prepare
yuki-kimoto authored on 2009-10-31
131
        return $value * 2;
cleanup#
yuki-kimoto authored on 2009-10-30
132
    }
133
    return $value;
134
});
add tests
yuki-kimoto authored on 2009-10-31
135

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

            
add tests
yuki-kimoto authored on 2009-10-31
138
$select_query = $dbi->create_query($SELECT_TMPL->{0});
add prepare
yuki-kimoto authored on 2009-10-31
139
$select_query->fetch_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
140
    my ($key, $value, $type, $sth, $i) = @_;
141
    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
add prepare
yuki-kimoto authored on 2009-10-31
142
        return $value * 3;
143
    }
144
    return $value;
145
});
146
$result = $dbi->execute($select_query);
cleanup#
yuki-kimoto authored on 2009-10-30
147

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

            
add tests
yuki-kimoto authored on 2009-10-31
151
$dbi->reconnect;
152
$dbi->do($CREATE_TABLE->{0});
153
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
154

            
155
$insert_query = $dbi->create_query($insert_tmpl);
156
$insert_query->bind_filter(sub {
157
    my ($key, $value, $table, $column) = @_;
158
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
159
        return $value * 3;
160
    }
161
    return $value;
162
});
163

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

            
166
$select_query = $dbi->create_query($SELECT_TMPL->{0});
167
$result       = $dbi->execute($select_query);
168
$rows = $result->fetch_all_hash;
169
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
170

            
171

            
add prepare
yuki-kimoto authored on 2009-10-31
172
__END__
cleanup#
yuki-kimoto authored on 2009-10-30
173

            
cleanup
yuki-kimoto authored on 2009-10-29
174
$dbi->fetch_filter(sub {
175
    my ($key, $value, $type, $sth, $i) = @_;
176
    if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) {
177
        return $value * 3;
cleanup
yuki-kimoto authored on 2009-10-19
178
    }
cleanup
yuki-kimoto authored on 2009-10-29
179
    return $value;
180
});
181

            
182
$result = $dbi->execute("select key1, key2 from table1");
183

            
184
$rows = $result->fetch_all;
185

            
186
is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array');
187

            
188

            
189
$result = $dbi->execute("select key1, key2 from table1");
190

            
191
$rows = $result->fetch_all_hash;
192

            
193
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch_filter hash');
194

            
195

            
196

            
197
# Expand place holer
198
my $dbi = DBI::Custom->new;
199
my $tmpl   = "select * from table where {= key1} && {<> key2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}";
200
my $params = {key1 => 'a', key2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'};
201

            
202
$dbi->filters(filter => sub {
203
    my ($key, $value) = @_;
204
    if ($key eq 'key1' && $value eq 'a') {
205
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
206
    }
cleanup
yuki-kimoto authored on 2009-10-29
207
    return $value;
208
});
209

            
210
my ($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
211

            
212
is($sql, "select * from table where key1 = ? && key2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2');
213
is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' );
214

            
215
# Expand place holer upper case
216
my $dbi = DBI::Custom->new;
217
$dbi->sql_template->upper_case(1);
218
my $tmpl   = "select * from table where {like k7}";
219
my $params = {k7 => 'g'};
220

            
221
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params);
222
is($sql, "select * from table where k7 LIKE ?;", 'sql template2');
223
is_deeply(\@bind, ['g'], 'sql template bind2' );
224

            
225
# Insert values
226
$dbi = DBI::Custom->new;
add tests
yuki-kimoto authored on 2009-10-31
227
$tmpl   = "insert into table {insert}";
228
$params = {insert => {key1 => 'a', key2 => 'b'}};
cleanup
yuki-kimoto authored on 2009-10-29
229

            
230
$dbi->filters(filter => sub {
231
    my ($key, $value) = @_;
232
    if ($key eq 'key1' && $value eq 'a') {
233
        return uc $value;
cleanup
yuki-kimoto authored on 2009-10-19
234
    }
cleanup
yuki-kimoto authored on 2009-10-29
235
    return $value;
236
});
cleanup
yuki-kimoto authored on 2009-10-19
237
    
cleanup
yuki-kimoto authored on 2009-10-29
238
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
239
is($sql, "insert into table (key1, key2) values (?, ?);");
240
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
241

            
242
# Update set
243
$dbi = DBI::Custom->new;
add tests
yuki-kimoto authored on 2009-10-31
244
$tmpl   = "update table {update}";
245
$params = {update => {key1 => 'a', key2 => 'b'}};
cleanup
yuki-kimoto authored on 2009-10-29
246

            
247
$dbi->filters(filter => sub {
248
    my ($key, $value) = @_;
249
    if ($key eq 'key1' && $value eq 'a') {
250
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
251
    }
cleanup
yuki-kimoto authored on 2009-10-29
252
    return $value;
253
});
add tests
yuki-kimoto authored on 2009-10-19
254
    
cleanup
yuki-kimoto authored on 2009-10-29
255
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
256
is($sql, "update table set key1 = ?, key2 = ?;");
257
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
add tests
yuki-kimoto authored on 2009-10-19
258

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