DBIx-Custom / t / 02-sqlite.t /
Newer Older
229 lines | 6.094kb
add test
yuki-kimoto authored on 2009-10-18
1
use Test::More;
2
use strict;
3
use warnings;
add tests
yuki-kimoto authored on 2009-10-19
4
use DBI qw/:sql_types/;
add test
yuki-kimoto authored on 2009-10-18
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
yuki-kimoto authored on 2009-10-29
16
# Function for test name
17
my $test;
18
sub test {
19
    $test = shift;
20
}
add test
yuki-kimoto authored on 2009-10-18
21

            
cleanup#
yuki-kimoto authored on 2009-10-30
22
# Varialbes for test
add prepare
yuki-kimoto authored on 2009-10-31
23
our $CREATE_TABLE = {
24
    0 => 'create table table1 (key1 char(255), key2 char(255));'
25
};
26

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

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

            
43

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

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

            
add tests
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-29
57

            
58
@rows = ();
59
while (my $row = $result->fetch) {
60
    push @rows, [@$row];
add test module
yuki-kimoto authored on 2009-10-19
61
}
add tests
yuki-kimoto authored on 2009-10-29
62
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
63

            
64

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

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

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

            
80

            
add tests
yuki-kimoto authored on 2009-10-29
81
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
82
@rows = ();
83
while (my %row = $result->fetch_hash) {
84
    push @rows, {%row};
add test module
yuki-kimoto authored on 2009-10-19
85
}
add tests
yuki-kimoto authored on 2009-10-29
86
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
87

            
88

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

            
93

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

            
98

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

            
103

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

            
add tests
yuki-kimoto authored on 2009-10-29
108
__END__
cleanup
yuki-kimoto authored on 2009-10-29
109

            
cleanup#
yuki-kimoto authored on 2009-10-30
110
test 'Filter';
111
$dbi->reconnect;
add prepare
yuki-kimoto authored on 2009-10-31
112
$dbi->dbh->do($CREATE_TABLE->{0});
cleanup#
yuki-kimoto authored on 2009-10-30
113

            
add prepare
yuki-kimoto authored on 2009-10-31
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#
yuki-kimoto authored on 2009-10-30
117
    my ($key, $value) = @_;
add prepare
yuki-kimoto authored on 2009-10-31
118
    if ($key eq 'key1') {
119
        return $value * 2;
cleanup#
yuki-kimoto authored on 2009-10-30
120
    }
121
    return $value;
122
});
add prepare
yuki-kimoto authored on 2009-10-31
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#
yuki-kimoto authored on 2009-10-30
137

            
add prepare
yuki-kimoto authored on 2009-10-31
138
$rows = $result->fetch_all_hash;
139
is_deeply($rows, {k1 => 2, k2 => 6}, "$test : bind_filter fetch_filter");
cleanup#
yuki-kimoto authored on 2009-10-30
140

            
add prepare
yuki-kimoto authored on 2009-10-31
141
__END__
cleanup#
yuki-kimoto authored on 2009-10-30
142

            
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
147
    }
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
175
    }
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
203
    }
cleanup
yuki-kimoto authored on 2009-10-29
204
    return $value;
205
});
cleanup
yuki-kimoto authored on 2009-10-19
206
    
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
220
    }
cleanup
yuki-kimoto authored on 2009-10-29
221
    return $value;
222
});
add tests
yuki-kimoto authored on 2009-10-19
223
    
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
227

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