DBIx-Custom / t / 02-sqlite.t /
Newer Older
185 lines | 5.054kb
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-29
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;
add tests
yuki-kimoto authored on 2009-10-29
31
my $query;
cleanup
yuki-kimoto authored on 2009-10-29
32

            
33

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

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

            
add tests
yuki-kimoto authored on 2009-10-29
43
test 'DBI::Custom::Result test';
44
$tmpl = "select key1, key2 from table1";
45
$query = $dbi->create_query($tmpl);
46
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
47

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

            
54

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

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

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

            
70

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

            
78

            
add tests
yuki-kimoto authored on 2009-10-29
79
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
80
$rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
81
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
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 list 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_hash;
add tests
yuki-kimoto authored on 2009-10-29
91
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash 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_hash list context");
cleanup
yuki-kimoto authored on 2009-10-29
97

            
add tests
yuki-kimoto authored on 2009-10-29
98
__END__
cleanup
yuki-kimoto authored on 2009-10-29
99

            
100
$dbi->fetch_filter(sub {
101
    my ($key, $value, $type, $sth, $i) = @_;
102
    if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) {
103
        return $value * 3;
cleanup
yuki-kimoto authored on 2009-10-19
104
    }
cleanup
yuki-kimoto authored on 2009-10-29
105
    return $value;
106
});
107

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

            
110
$rows = $result->fetch_all;
111

            
112
is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array');
113

            
114

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

            
117
$rows = $result->fetch_all_hash;
118

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

            
121

            
122

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

            
128
$dbi->filters(filter => sub {
129
    my ($key, $value) = @_;
130
    if ($key eq 'key1' && $value eq 'a') {
131
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
132
    }
cleanup
yuki-kimoto authored on 2009-10-29
133
    return $value;
134
});
135

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

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

            
141
# Expand place holer upper case
142
my $dbi = DBI::Custom->new;
143
$dbi->sql_template->upper_case(1);
144
my $tmpl   = "select * from table where {like k7}";
145
my $params = {k7 => 'g'};
146

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

            
151
# Insert values
152
$dbi = DBI::Custom->new;
153
$tmpl   = "insert into table {insert_values}";
154
$params = {insert_values => {key1 => 'a', key2 => 'b'}};
155

            
156
$dbi->filters(filter => sub {
157
    my ($key, $value) = @_;
158
    if ($key eq 'key1' && $value eq 'a') {
159
        return uc $value;
cleanup
yuki-kimoto authored on 2009-10-19
160
    }
cleanup
yuki-kimoto authored on 2009-10-29
161
    return $value;
162
});
cleanup
yuki-kimoto authored on 2009-10-19
163
    
cleanup
yuki-kimoto authored on 2009-10-29
164
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
165
is($sql, "insert into table (key1, key2) values (?, ?);");
166
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
167

            
168
# Update set
169
$dbi = DBI::Custom->new;
170
$tmpl   = "update table {update_set}";
171
$params = {update_set => {key1 => 'a', key2 => 'b'}};
172

            
173
$dbi->filters(filter => sub {
174
    my ($key, $value) = @_;
175
    if ($key eq 'key1' && $value eq 'a') {
176
        return uc $value;
add tests
yuki-kimoto authored on 2009-10-19
177
    }
cleanup
yuki-kimoto authored on 2009-10-29
178
    return $value;
179
});
add tests
yuki-kimoto authored on 2009-10-19
180
    
cleanup
yuki-kimoto authored on 2009-10-29
181
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter});
182
is($sql, "update table set key1 = ?, key2 = ?;");
183
is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
add tests
yuki-kimoto authored on 2009-10-19
184

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