add test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
add tests
|
4 |
use DBI qw/:sql_types/; |
add test
|
5 |
|
6 |
BEGIN { |
|
7 |
eval { require DBD::SQLite; 1 } |
|
8 |
or plan skip_all => 'DBD::SQLite required'; |
|
9 |
eval { DBD::SQLite->VERSION >= 1 } |
|
10 |
or plan skip_all => 'DBD::SQLite >= 1.00 required'; |
|
11 |
|
|
12 |
plan 'no_plan'; |
|
13 |
use_ok('DBI::Custom'); |
|
14 |
} |
|
15 |
|
|
cleanup
|
16 |
# Function for test name |
17 |
my $test; |
|
18 |
sub test { |
|
19 |
$test = shift; |
|
20 |
} |
|
add test
|
21 |
|
cleanup#
|
22 |
# Varialbes for test |
add prepare
|
23 |
our $CREATE_TABLE = { |
24 |
0 => 'create table table1 (key1 char(255), key2 char(255));' |
|
25 |
}; |
|
26 |
|
|
cleanup
|
27 |
my $dbi; |
28 |
my $sth; |
|
29 |
my $tmpl; |
|
cleanup#
|
30 |
my $select_tmpl; |
31 |
my $insert_tmpl; |
|
cleanup
|
32 |
my $params; |
33 |
my $sql; |
|
34 |
my $result; |
|
35 |
my @rows; |
|
36 |
my $rows; |
|
add tests
|
37 |
my $query; |
cleanup#
|
38 |
my $select_query; |
39 |
my $insert_query; |
|
add prepare
|
40 |
my $ret_val; |
cleanup#
|
41 |
|
cleanup
|
42 |
|
43 |
|
|
44 |
# Prepare table |
|
45 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
46 |
$dbi->connect; |
|
add prepare
|
47 |
$dbi->do($CREATE_TABLE->{0}); |
48 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
cleanup
|
49 |
$sth->execute(1, 2); |
50 |
$sth->execute(3, 4); |
|
add test
|
51 |
|
add test module
|
52 |
|
add tests
|
53 |
test 'DBI::Custom::Result test'; |
54 |
$tmpl = "select key1, key2 from table1"; |
|
55 |
$query = $dbi->create_query($tmpl); |
|
56 |
$result = $dbi->execute($query); |
|
cleanup
|
57 |
|
58 |
@rows = (); |
|
59 |
while (my $row = $result->fetch) { |
|
60 |
push @rows, [@$row]; |
|
add test module
|
61 |
} |
add tests
|
62 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
cleanup
|
63 |
|
64 |
|
|
add tests
|
65 |
$result = $dbi->execute($query); |
cleanup
|
66 |
@rows = (); |
67 |
while (my @row = $result->fetch) { |
|
68 |
push @rows, [@row]; |
|
add test module
|
69 |
} |
add tests
|
70 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
add test module
|
71 |
|
cleanup
|
72 |
|
add tests
|
73 |
$result = $dbi->execute($query); |
cleanup
|
74 |
@rows = (); |
75 |
while (my $row = $result->fetch_hash) { |
|
76 |
push @rows, {%$row}; |
|
add test module
|
77 |
} |
add tests
|
78 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
cleanup
|
79 |
|
80 |
|
|
add tests
|
81 |
$result = $dbi->execute($query); |
cleanup
|
82 |
@rows = (); |
83 |
while (my %row = $result->fetch_hash) { |
|
84 |
push @rows, {%row}; |
|
add test module
|
85 |
} |
add tests
|
86 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
add test module
|
87 |
|
88 |
|
|
add tests
|
89 |
$result = $dbi->execute($query); |
cleanup
|
90 |
$rows = $result->fetch_all; |
add tests
|
91 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
cleanup
|
92 |
|
93 |
|
|
add tests
|
94 |
$result = $dbi->execute($query); |
cleanup
|
95 |
@rows = $result->fetch_all; |
add tests
|
96 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
cleanup
|
97 |
|
98 |
|
|
add tests
|
99 |
$result = $dbi->execute($query); |
cleanup
|
100 |
@rows = $result->fetch_all_hash; |
add tests
|
101 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context"); |
cleanup
|
102 |
|
103 |
|
|
add tests
|
104 |
$result = $dbi->execute($query); |
cleanup
|
105 |
@rows = $result->fetch_all; |
add tests
|
106 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context"); |
cleanup
|
107 |
|
add tests
|
108 |
__END__ |
cleanup
|
109 |
|
cleanup#
|
110 |
test 'Filter'; |
111 |
$dbi->reconnect; |
|
add prepare
|
112 |
$dbi->dbh->do($CREATE_TABLE->{0}); |
cleanup#
|
113 |
|
add prepare
|
114 |
$insert_tmpl = "insert into table1 {insert_values key1 key2};"; |
115 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
116 |
$insert_query->bind_filter(sub { |
|
cleanup#
|
117 |
my ($key, $value) = @_; |
add prepare
|
118 |
if ($key eq 'key1') { |
119 |
return $value * 2; |
|
cleanup#
|
120 |
} |
121 |
return $value; |
|
122 |
}); |
|
add prepare
|
123 |
$DB::single = 1; |
124 |
$ret_val = $dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
|
125 |
ok($ret_val, "Insert success return value"); |
|
126 |
|
|
127 |
$select_tmpl = "select k1, k2 from table1"; |
|
128 |
$select_query = $dbi->create_query($select_query); |
|
129 |
$select_query->fetch_filter(sub { |
|
130 |
my ($key, $value); |
|
131 |
if ($key eq 'key2') { |
|
132 |
return $value * 3; |
|
133 |
} |
|
134 |
return $value; |
|
135 |
}); |
|
136 |
$result = $dbi->execute($select_query); |
|
cleanup#
|
137 |
|
add prepare
|
138 |
$rows = $result->fetch_all_hash; |
139 |
is_deeply($rows, {k1 => 2, k2 => 6}, "$test : bind_filter fetch_filter"); |
|
cleanup#
|
140 |
|
add prepare
|
141 |
__END__ |
cleanup#
|
142 |
|
cleanup
|
143 |
$dbi->fetch_filter(sub { |
144 |
my ($key, $value, $type, $sth, $i) = @_; |
|
145 |
if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) { |
|
146 |
return $value * 3; |
|
cleanup
|
147 |
} |
cleanup
|
148 |
return $value; |
149 |
}); |
|
150 |
|
|
151 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
152 |
|
|
153 |
$rows = $result->fetch_all; |
|
154 |
|
|
155 |
is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array'); |
|
156 |
|
|
157 |
|
|
158 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
159 |
|
|
160 |
$rows = $result->fetch_all_hash; |
|
161 |
|
|
162 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch_filter hash'); |
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
# Expand place holer |
|
167 |
my $dbi = DBI::Custom->new; |
|
168 |
my $tmpl = "select * from table where {= key1} && {<> key2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}"; |
|
169 |
my $params = {key1 => 'a', key2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'}; |
|
170 |
|
|
171 |
$dbi->filters(filter => sub { |
|
172 |
my ($key, $value) = @_; |
|
173 |
if ($key eq 'key1' && $value eq 'a') { |
|
174 |
return uc $value; |
|
add tests
|
175 |
} |
cleanup
|
176 |
return $value; |
177 |
}); |
|
178 |
|
|
179 |
my ($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
|
180 |
|
|
181 |
is($sql, "select * from table where key1 = ? && key2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
|
182 |
is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
|
183 |
|
|
184 |
# Expand place holer upper case |
|
185 |
my $dbi = DBI::Custom->new; |
|
186 |
$dbi->sql_template->upper_case(1); |
|
187 |
my $tmpl = "select * from table where {like k7}"; |
|
188 |
my $params = {k7 => 'g'}; |
|
189 |
|
|
190 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params); |
|
191 |
is($sql, "select * from table where k7 LIKE ?;", 'sql template2'); |
|
192 |
is_deeply(\@bind, ['g'], 'sql template bind2' ); |
|
193 |
|
|
194 |
# Insert values |
|
195 |
$dbi = DBI::Custom->new; |
|
196 |
$tmpl = "insert into table {insert_values}"; |
|
197 |
$params = {insert_values => {key1 => 'a', key2 => 'b'}}; |
|
198 |
|
|
199 |
$dbi->filters(filter => sub { |
|
200 |
my ($key, $value) = @_; |
|
201 |
if ($key eq 'key1' && $value eq 'a') { |
|
202 |
return uc $value; |
|
cleanup
|
203 |
} |
cleanup
|
204 |
return $value; |
205 |
}); |
|
cleanup
|
206 |
|
cleanup
|
207 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
208 |
is($sql, "insert into table (key1, key2) values (?, ?);"); |
|
209 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
210 |
|
|
211 |
# Update set |
|
212 |
$dbi = DBI::Custom->new; |
|
213 |
$tmpl = "update table {update_set}"; |
|
214 |
$params = {update_set => {key1 => 'a', key2 => 'b'}}; |
|
215 |
|
|
216 |
$dbi->filters(filter => sub { |
|
217 |
my ($key, $value) = @_; |
|
218 |
if ($key eq 'key1' && $value eq 'a') { |
|
219 |
return uc $value; |
|
add tests
|
220 |
} |
cleanup
|
221 |
return $value; |
222 |
}); |
|
add tests
|
223 |
|
cleanup
|
224 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
225 |
is($sql, "update table set key1 = ?, key2 = ?;"); |
|
226 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
add tests
|
227 |
|
cleanup#
|
228 |
$dbi->disconnnect; |
add test module
|
229 |
|