DBIx-Custom / t / 02-sqlite.t /
Newer Older
188 lines | 5.021kb
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;
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
yuki-kimoto authored on 2009-10-18
40

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

            
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
48
}
cleanup
yuki-kimoto authored on 2009-10-29
49
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch');
50

            
51

            
52
$result = $dbi->execute("select key1, key2 from table1");
add test module
yuki-kimoto authored on 2009-10-19
53

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

            
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
66
}
cleanup
yuki-kimoto authored on 2009-10-29
67
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch_hash');
add test module
yuki-kimoto authored on 2009-10-19
68

            
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
75
}
cleanup
yuki-kimoto authored on 2009-10-29
76
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'fetch hash list context');
add test module
yuki-kimoto authored on 2009-10-19
77

            
78

            
cleanup
yuki-kimoto authored on 2009-10-29
79
$result = $dbi->execute("select key1, key2 from table1");
add test module
yuki-kimoto authored on 2009-10-19
80

            
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
107
    }
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
135
    }
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
163
    }
cleanup
yuki-kimoto authored on 2009-10-29
164
    return $value;
165
});
cleanup
yuki-kimoto authored on 2009-10-19
166
    
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
180
    }
cleanup
yuki-kimoto authored on 2009-10-29
181
    return $value;
182
});
add tests
yuki-kimoto authored on 2009-10-19
183
    
cleanup
yuki-kimoto authored on 2009-10-29
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
yuki-kimoto authored on 2009-10-19
187

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