DBIx-Custom / t / 02-sqlite.t /
Newer Older
227 lines | 6.201kb
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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
112
$insert_tmpl  = "insert into table1 {insert_values key1 key2};";
113
$insert_query = $dbi->create_query($insert_tmpl);
114
$insert_query->bind_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
115
    my ($key, $value, $table, $column) = @_;
116
    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
add prepare
yuki-kimoto authored on 2009-10-31
117
        return $value * 2;
cleanup#
yuki-kimoto authored on 2009-10-30
118
    }
119
    return $value;
120
});
add tests
yuki-kimoto authored on 2009-10-31
121

            
add prepare
yuki-kimoto authored on 2009-10-31
122
$ret_val = $dbi->execute($insert_query, {key1 => 1, key2 => 2});
123
ok($ret_val, "Insert success return value");
124

            
add tests
yuki-kimoto authored on 2009-10-31
125
$select_tmpl  = "select key1, key2 from table1";
126
$select_query = $dbi->create_query($select_tmpl);
add prepare
yuki-kimoto authored on 2009-10-31
127
$select_query->fetch_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
128
    my ($key, $value, $type, $sth, $i) = @_;
129
    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
add prepare
yuki-kimoto authored on 2009-10-31
130
        return $value * 3;
131
    }
132
    return $value;
133
});
134
$result = $dbi->execute($select_query);
cleanup#
yuki-kimoto authored on 2009-10-30
135

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

            
add prepare
yuki-kimoto authored on 2009-10-31
139
__END__
cleanup#
yuki-kimoto authored on 2009-10-30
140

            
cleanup
yuki-kimoto authored on 2009-10-29
141
$dbi->fetch_filter(sub {
142
    my ($key, $value, $type, $sth, $i) = @_;
143
    if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) {
144
        return $value * 3;
cleanup
yuki-kimoto authored on 2009-10-19
145
    }
cleanup
yuki-kimoto authored on 2009-10-29
146
    return $value;
147
});
148

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

            
151
$rows = $result->fetch_all;
152

            
153
is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array');
154

            
155

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

            
158
$rows = $result->fetch_all_hash;
159

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

            
162

            
163

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

            
169
$dbi->filters(filter => sub {
170
    my ($key, $value) = @_;
171
    if ($key eq 'key1' && $value eq 'a') {
172
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
173
    }
cleanup
yuki-kimoto authored on 2009-10-29
174
    return $value;
175
});
176

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

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

            
182
# Expand place holer upper case
183
my $dbi = DBI::Custom->new;
184
$dbi->sql_template->upper_case(1);
185
my $tmpl   = "select * from table where {like k7}";
186
my $params = {k7 => 'g'};
187

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

            
192
# Insert values
193
$dbi = DBI::Custom->new;
194
$tmpl   = "insert into table {insert_values}";
195
$params = {insert_values => {key1 => 'a', key2 => 'b'}};
196

            
197
$dbi->filters(filter => sub {
198
    my ($key, $value) = @_;
199
    if ($key eq 'key1' && $value eq 'a') {
200
        return uc $value;
cleanup
yuki-kimoto authored on 2009-10-19
201
    }
cleanup
yuki-kimoto authored on 2009-10-29
202
    return $value;
203
});
cleanup
yuki-kimoto authored on 2009-10-19
204
    
cleanup
yuki-kimoto authored on 2009-10-29
205
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
206
is($sql, "insert into table (key1, key2) values (?, ?);");
207
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
208

            
209
# Update set
210
$dbi = DBI::Custom->new;
211
$tmpl   = "update table {update_set}";
212
$params = {update_set => {key1 => 'a', key2 => 'b'}};
213

            
214
$dbi->filters(filter => sub {
215
    my ($key, $value) = @_;
216
    if ($key eq 'key1' && $value eq 'a') {
217
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
218
    }
cleanup
yuki-kimoto authored on 2009-10-29
219
    return $value;
220
});
add tests
yuki-kimoto authored on 2009-10-19
221
    
cleanup
yuki-kimoto authored on 2009-10-29
222
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
223
is($sql, "update table set key1 = ?, key2 = ?;");
224
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
add tests
yuki-kimoto authored on 2009-10-19
225

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