DBIx-Custom / t / 02-sqlite.t /
Newer Older
210 lines | 5.562kb
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
cleanup
yuki-kimoto authored on 2009-10-29
23
my $dbi;
24
my $sth;
25
my $tmpl;
cleanup#
yuki-kimoto authored on 2009-10-30
26
my $select_tmpl;
27
my $insert_tmpl;
cleanup
yuki-kimoto authored on 2009-10-29
28
my $params;
29
my $sql;
30
my $result;
31
my @rows;
32
my $rows;
add tests
yuki-kimoto authored on 2009-10-29
33
my $query;
cleanup#
yuki-kimoto authored on 2009-10-30
34
my $select_query;
35
my $insert_query;
36

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

            
38

            
39
# Prepare table
40
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:');
41
$dbi->connect;
42
$dbi->dbh->do("create table table1 (key1 char(255), key2 char(255))");
43
$sth = $dbi->dbh->prepare("insert into table1 (key1, key2) values (?, ?);");
44
$sth->execute(1, 2);
45
$sth->execute(3, 4);
add test
yuki-kimoto authored on 2009-10-18
46

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

            
add tests
yuki-kimoto authored on 2009-10-29
48
test 'DBI::Custom::Result test';
49
$tmpl = "select key1, key2 from table1";
50
$query = $dbi->create_query($tmpl);
51
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
52

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

            
59

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

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

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

            
75

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

            
83

            
add tests
yuki-kimoto authored on 2009-10-29
84
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
85
$rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
86
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
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 list 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_hash;
add tests
yuki-kimoto authored on 2009-10-29
96
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar 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;
add tests
yuki-kimoto authored on 2009-10-29
101
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context");
cleanup
yuki-kimoto authored on 2009-10-29
102

            
add tests
yuki-kimoto authored on 2009-10-29
103
__END__
cleanup
yuki-kimoto authored on 2009-10-29
104

            
cleanup#
yuki-kimoto authored on 2009-10-30
105
test 'Filter';
106
$dbi->reconnect;
107
$dbi->dbh->do("create table table1 (key1 char(255), key2 char(255));");
108

            
109
$tmpl = "insert into {insert_values key1 key2};";
110
$query = $dbi->create_query($tmpl);
111
$query->bind_filter(sub {
112
    my ($key, $value) = @_;
113
    if ($key eq 'k1') {
114
        return "$value-$key-$column";
115
    }
116
    return $value;
117
});
118

            
119

            
120
$query->fetch_filter(sub {
121
    my ($key, $value)
122
});
123

            
cleanup
yuki-kimoto authored on 2009-10-29
124
$dbi->fetch_filter(sub {
125
    my ($key, $value, $type, $sth, $i) = @_;
126
    if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) {
127
        return $value * 3;
cleanup
yuki-kimoto authored on 2009-10-19
128
    }
cleanup
yuki-kimoto authored on 2009-10-29
129
    return $value;
130
});
131

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

            
134
$rows = $result->fetch_all;
135

            
136
is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array');
137

            
138

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

            
141
$rows = $result->fetch_all_hash;
142

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

            
145

            
146

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

            
152
$dbi->filters(filter => sub {
153
    my ($key, $value) = @_;
154
    if ($key eq 'key1' && $value eq 'a') {
155
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
156
    }
cleanup
yuki-kimoto authored on 2009-10-29
157
    return $value;
158
});
159

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

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

            
165
# Expand place holer upper case
166
my $dbi = DBI::Custom->new;
167
$dbi->sql_template->upper_case(1);
168
my $tmpl   = "select * from table where {like k7}";
169
my $params = {k7 => 'g'};
170

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

            
175
# Insert values
176
$dbi = DBI::Custom->new;
177
$tmpl   = "insert into table {insert_values}";
178
$params = {insert_values => {key1 => 'a', key2 => 'b'}};
179

            
180
$dbi->filters(filter => sub {
181
    my ($key, $value) = @_;
182
    if ($key eq 'key1' && $value eq 'a') {
183
        return uc $value;
cleanup
yuki-kimoto authored on 2009-10-19
184
    }
cleanup
yuki-kimoto authored on 2009-10-29
185
    return $value;
186
});
cleanup
yuki-kimoto authored on 2009-10-19
187
    
cleanup
yuki-kimoto authored on 2009-10-29
188
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
189
is($sql, "insert into table (key1, key2) values (?, ?);");
190
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
191

            
192
# Update set
193
$dbi = DBI::Custom->new;
194
$tmpl   = "update table {update_set}";
195
$params = {update_set => {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;
add tests
yuki-kimoto authored on 2009-10-19
201
    }
cleanup
yuki-kimoto authored on 2009-10-29
202
    return $value;
203
});
add tests
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, "update table set key1 = ?, key2 = ?;");
207
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
add tests
yuki-kimoto authored on 2009-10-19
208

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