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 |
# Varialbe for test |
23 |
my $dbi; |
|
24 |
my $sth; |
|
25 |
my $tmpl; |
|
26 |
my $params; |
|
27 |
my $sql; |
|
28 |
my $result; |
|
29 |
my @rows; |
|
30 |
my $rows; |
|
31 |
|
|
32 |
|
|
33 |
# Prepare table |
|
34 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
35 |
$dbi->connect; |
|
36 |
$dbi->dbh->do("create table table1 (key1 char(255), key2 char(255))"); |
|
37 |
$sth = $dbi->dbh->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
38 |
$sth->execute(1, 2); |
|
39 |
$sth->execute(3, 4); |
|
add test
|
40 |
|
add test module
|
41 |
|
cleanup
|
42 |
__END__ |
43 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
44 |
|
|
45 |
@rows = (); |
|
46 |
while (my $row = $result->fetch) { |
|
47 |
push @rows, [@$row]; |
|
add test module
|
48 |
} |
cleanup
|
49 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch'); |
50 |
|
|
51 |
|
|
52 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
add test module
|
53 |
|
cleanup
|
54 |
@rows = (); |
55 |
while (my @row = $result->fetch) { |
|
56 |
push @rows, [@row]; |
|
add test module
|
57 |
} |
cleanup
|
58 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch list context'); |
add test module
|
59 |
|
cleanup
|
60 |
|
61 |
$result = $dbi->execute("select key1, key2 from table1;"); |
|
62 |
|
|
63 |
@rows = (); |
|
64 |
while (my $row = $result->fetch_hash) { |
|
65 |
push @rows, {%$row}; |
|
add test module
|
66 |
} |
cleanup
|
67 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch_hash'); |
add test module
|
68 |
|
cleanup
|
69 |
|
70 |
$result = $dbi->execute("select key1, key2 from table1;"); |
|
71 |
|
|
72 |
@rows = (); |
|
73 |
while (my %row = $result->fetch_hash) { |
|
74 |
push @rows, {%row}; |
|
add test module
|
75 |
} |
cleanup
|
76 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch hash list context'); |
add test module
|
77 |
|
78 |
|
|
cleanup
|
79 |
$result = $dbi->execute("select key1, key2 from table1"); |
add test module
|
80 |
|
cleanup
|
81 |
$rows = $result->fetch_all; |
82 |
is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all'); |
|
83 |
|
|
84 |
|
|
85 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
86 |
|
|
87 |
@rows = $result->fetch_all; |
|
88 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all list context'); |
|
89 |
|
|
90 |
|
|
91 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
92 |
|
|
93 |
@rows = $result->fetch_all_hash; |
|
94 |
is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all_hash'); |
|
95 |
|
|
96 |
|
|
97 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
98 |
|
|
99 |
@rows = $result->fetch_all; |
|
100 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all_hash list context'); |
|
101 |
|
|
102 |
|
|
103 |
$dbi->fetch_filter(sub { |
|
104 |
my ($key, $value, $type, $sth, $i) = @_; |
|
105 |
if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) { |
|
106 |
return $value * 3; |
|
cleanup
|
107 |
} |
cleanup
|
108 |
return $value; |
109 |
}); |
|
110 |
|
|
111 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
112 |
|
|
113 |
$rows = $result->fetch_all; |
|
114 |
|
|
115 |
is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array'); |
|
116 |
|
|
117 |
|
|
118 |
$result = $dbi->execute("select key1, key2 from table1"); |
|
119 |
|
|
120 |
$rows = $result->fetch_all_hash; |
|
121 |
|
|
122 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch_filter hash'); |
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
# Expand place holer |
|
127 |
my $dbi = DBI::Custom->new; |
|
128 |
my $tmpl = "select * from table where {= key1} && {<> key2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}"; |
|
129 |
my $params = {key1 => 'a', key2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'}; |
|
130 |
|
|
131 |
$dbi->filters(filter => sub { |
|
132 |
my ($key, $value) = @_; |
|
133 |
if ($key eq 'key1' && $value eq 'a') { |
|
134 |
return uc $value; |
|
add tests
|
135 |
} |
cleanup
|
136 |
return $value; |
137 |
}); |
|
138 |
|
|
139 |
my ($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
|
140 |
|
|
141 |
is($sql, "select * from table where key1 = ? && key2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
|
142 |
is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
|
143 |
|
|
144 |
# Expand place holer upper case |
|
145 |
my $dbi = DBI::Custom->new; |
|
146 |
$dbi->sql_template->upper_case(1); |
|
147 |
my $tmpl = "select * from table where {like k7}"; |
|
148 |
my $params = {k7 => 'g'}; |
|
149 |
|
|
150 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params); |
|
151 |
is($sql, "select * from table where k7 LIKE ?;", 'sql template2'); |
|
152 |
is_deeply(\@bind, ['g'], 'sql template bind2' ); |
|
153 |
|
|
154 |
# Insert values |
|
155 |
$dbi = DBI::Custom->new; |
|
156 |
$tmpl = "insert into table {insert_values}"; |
|
157 |
$params = {insert_values => {key1 => 'a', key2 => 'b'}}; |
|
158 |
|
|
159 |
$dbi->filters(filter => sub { |
|
160 |
my ($key, $value) = @_; |
|
161 |
if ($key eq 'key1' && $value eq 'a') { |
|
162 |
return uc $value; |
|
cleanup
|
163 |
} |
cleanup
|
164 |
return $value; |
165 |
}); |
|
cleanup
|
166 |
|
cleanup
|
167 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
168 |
is($sql, "insert into table (key1, key2) values (?, ?);"); |
|
169 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
170 |
|
|
171 |
# Update set |
|
172 |
$dbi = DBI::Custom->new; |
|
173 |
$tmpl = "update table {update_set}"; |
|
174 |
$params = {update_set => {key1 => 'a', key2 => 'b'}}; |
|
175 |
|
|
176 |
$dbi->filters(filter => sub { |
|
177 |
my ($key, $value) = @_; |
|
178 |
if ($key eq 'key1' && $value eq 'a') { |
|
179 |
return uc $value; |
|
add tests
|
180 |
} |
cleanup
|
181 |
return $value; |
182 |
}); |
|
add tests
|
183 |
|
cleanup
|
184 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
185 |
is($sql, "update table set key1 = ?, key2 = ?;"); |
|
186 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
add tests
|
187 |
|
add test module
|
188 |
|