add test
|
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
|
15 |
# Function for test name |
16 |
my $test; |
|
17 |
sub test { |
|
18 |
$test = shift; |
|
19 |
} |
|
add test
|
20 |
|
cleanup#
|
21 |
# Varialbes for test |
add prepare
|
22 |
our $CREATE_TABLE = { |
23 |
0 => 'create table table1 (key1 char(255), key2 char(255));' |
|
24 |
}; |
|
25 |
|
|
add tests
|
26 |
our $SELECT_TMPL = { |
27 |
0 => 'select key1, key2 from table1;' |
|
28 |
}; |
|
29 |
|
|
cleanup
|
30 |
my $dbi; |
31 |
my $sth; |
|
32 |
my $tmpl; |
|
cleanup#
|
33 |
my $select_tmpl; |
34 |
my $insert_tmpl; |
|
add tests
|
35 |
my $update_tmpl; |
cleanup
|
36 |
my $params; |
37 |
my $sql; |
|
38 |
my $result; |
|
39 |
my @rows; |
|
40 |
my $rows; |
|
add tests
|
41 |
my $query; |
cleanup#
|
42 |
my $select_query; |
43 |
my $insert_query; |
|
add tests
|
44 |
my $update_query; |
add prepare
|
45 |
my $ret_val; |
cleanup#
|
46 |
|
cleanup
|
47 |
|
48 |
|
|
add tests
|
49 |
|
cleanup
|
50 |
# Prepare table |
51 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
52 |
$dbi->connect; |
|
add prepare
|
53 |
$dbi->do($CREATE_TABLE->{0}); |
54 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
cleanup
|
55 |
$sth->execute(1, 2); |
56 |
$sth->execute(3, 4); |
|
add test
|
57 |
|
add test module
|
58 |
|
add tests
|
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
|
63 |
|
64 |
@rows = (); |
|
65 |
while (my $row = $result->fetch) { |
|
66 |
push @rows, [@$row]; |
|
add test module
|
67 |
} |
add tests
|
68 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
cleanup
|
69 |
|
70 |
|
|
add tests
|
71 |
$result = $dbi->execute($query); |
cleanup
|
72 |
@rows = (); |
73 |
while (my @row = $result->fetch) { |
|
74 |
push @rows, [@row]; |
|
add test module
|
75 |
} |
add tests
|
76 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
add test module
|
77 |
|
cleanup
|
78 |
|
add tests
|
79 |
$result = $dbi->execute($query); |
cleanup
|
80 |
@rows = (); |
81 |
while (my $row = $result->fetch_hash) { |
|
82 |
push @rows, {%$row}; |
|
add test module
|
83 |
} |
add tests
|
84 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
cleanup
|
85 |
|
86 |
|
|
add tests
|
87 |
$result = $dbi->execute($query); |
cleanup
|
88 |
@rows = (); |
89 |
while (my %row = $result->fetch_hash) { |
|
90 |
push @rows, {%row}; |
|
add test module
|
91 |
} |
add tests
|
92 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
add test module
|
93 |
|
94 |
|
|
add tests
|
95 |
$result = $dbi->execute($query); |
cleanup
|
96 |
$rows = $result->fetch_all; |
add tests
|
97 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
cleanup
|
98 |
|
99 |
|
|
add tests
|
100 |
$result = $dbi->execute($query); |
cleanup
|
101 |
@rows = $result->fetch_all; |
add tests
|
102 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
cleanup
|
103 |
|
104 |
|
|
add tests
|
105 |
$result = $dbi->execute($query); |
cleanup
|
106 |
@rows = $result->fetch_all_hash; |
add tests
|
107 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context"); |
cleanup
|
108 |
|
109 |
|
|
add tests
|
110 |
$result = $dbi->execute($query); |
cleanup
|
111 |
@rows = $result->fetch_all; |
add tests
|
112 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context"); |
cleanup
|
113 |
|
add tests
|
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#
|
122 |
test 'Filter'; |
123 |
$dbi->reconnect; |
|
add tests
|
124 |
$dbi->do($CREATE_TABLE->{0}); |
cleanup#
|
125 |
|
add tests
|
126 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
add prepare
|
127 |
$insert_query = $dbi->create_query($insert_tmpl); |
128 |
$insert_query->bind_filter(sub { |
|
add tests
|
129 |
my ($key, $value, $table, $column) = @_; |
130 |
if ($key eq 'key1' && $table eq '' && $column eq 'key1') { |
|
add prepare
|
131 |
return $value * 2; |
cleanup#
|
132 |
} |
133 |
return $value; |
|
134 |
}); |
|
add tests
|
135 |
|
add tests
|
136 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
add prepare
|
137 |
|
add tests
|
138 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
add prepare
|
139 |
$select_query->fetch_filter(sub { |
add tests
|
140 |
my ($key, $value, $type, $sth, $i) = @_; |
141 |
if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) { |
|
add prepare
|
142 |
return $value * 3; |
143 |
} |
|
144 |
return $value; |
|
145 |
}); |
|
146 |
$result = $dbi->execute($select_query); |
|
cleanup#
|
147 |
|
add prepare
|
148 |
$rows = $result->fetch_all_hash; |
add tests
|
149 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter"); |
cleanup#
|
150 |
|
add tests
|
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
|
172 |
__END__ |
cleanup#
|
173 |
|
cleanup
|
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
|
178 |
} |
cleanup
|
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
|
206 |
} |
cleanup
|
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
|
227 |
$tmpl = "insert into table {insert}"; |
228 |
$params = {insert => {key1 => 'a', key2 => 'b'}}; |
|
cleanup
|
229 |
|
230 |
$dbi->filters(filter => sub { |
|
231 |
my ($key, $value) = @_; |
|
232 |
if ($key eq 'key1' && $value eq 'a') { |
|
233 |
return uc $value; |
|
cleanup
|
234 |
} |
cleanup
|
235 |
return $value; |
236 |
}); |
|
cleanup
|
237 |
|
cleanup
|
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
|
244 |
$tmpl = "update table {update}"; |
245 |
$params = {update => {key1 => 'a', key2 => 'b'}}; |
|
cleanup
|
246 |
|
247 |
$dbi->filters(filter => sub { |
|
248 |
my ($key, $value) = @_; |
|
249 |
if ($key eq 'key1' && $value eq 'a') { |
|
250 |
return uc $value; |
|
add tests
|
251 |
} |
cleanup
|
252 |
return $value; |
253 |
}); |
|
add tests
|
254 |
|
cleanup
|
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
|
258 |
|
cleanup#
|
259 |
$dbi->disconnnect; |
add test module
|
260 |
|