add test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
add tests
|
4 |
use DBI qw/:sql_types/; |
add test
|
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 |
|
|
add test module
|
16 |
package Test::DBI::Custom; |
17 |
use Object::Simple; |
|
add test
|
18 |
|
add test module
|
19 |
sub dbi : Attr {} |
add test
|
20 |
|
add test module
|
21 |
sub new { |
22 |
my $self = shift->SUPER::new; |
|
update document
|
23 |
my $dbi = DBI::Custom->new->data_source('dbi:SQLite:dbname=:memory:'); |
add test module
|
24 |
|
25 |
$dbi->connect; |
|
26 |
$self->dbi($dbi); |
|
27 |
return $self; |
|
28 |
} |
|
29 |
|
|
30 |
sub create_table { |
|
31 |
my ($self, $create_table) = @_; |
|
32 |
$self->dbi->query_raw_sql($create_table); |
|
33 |
return $self; |
|
34 |
} |
|
35 |
|
|
36 |
sub create_table1 { |
|
37 |
my $self = shift; |
|
38 |
$self->create_table("create table t1 (k1 char(255), k2 char(255), k3 char(255), k4 char(255), k5 char(255));"); |
|
39 |
return $self; |
|
40 |
} |
|
41 |
|
|
42 |
sub insert { |
|
43 |
my ($self, @values_list) = @_; |
|
add various thins
|
44 |
my $table = ref $params_list[0] ? '' : shift; |
add test module
|
45 |
$table ||= 't1'; |
46 |
|
|
add various thins
|
47 |
foreach my $params (@values_list) { |
48 |
my $sql = $self->dbi->execute( |
|
49 |
"insert into $table {insert_values}", {insert_values => $params} |
|
add test module
|
50 |
); |
51 |
} |
|
52 |
return $self; |
|
53 |
} |
|
54 |
|
|
55 |
sub test { |
|
56 |
my ($self, $code) = @_; |
|
57 |
$code->($self->dbi); |
|
58 |
} |
|
59 |
|
|
60 |
Object::Simple->build_class; |
|
61 |
|
|
62 |
package main; |
|
63 |
my $t = Test::DBI::Custom->new; |
|
64 |
|
|
65 |
$t->new->create_table1->insert({k1 => 1, k2 => 2}, {k1 => 3, k2 => 4})->test(sub { |
|
66 |
my $dbi = shift; |
|
add test
|
67 |
|
cleanup
|
68 |
my $r; # resultset |
69 |
my @rows; |
|
add tests
|
70 |
my $rows; |
cleanup
|
71 |
|
add various thins
|
72 |
$r = $dbi->execute("select k1, k2 from t1"); |
cleanup
|
73 |
|
74 |
@rows = (); |
|
75 |
while (my $row = $r->fetch) { |
|
76 |
push @rows, [@$row]; |
|
77 |
} |
|
add tests
|
78 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch'); |
79 |
|
|
80 |
|
|
add various thins
|
81 |
$r = $dbi->execute("select k1, k2 from t1"); |
add tests
|
82 |
|
83 |
@rows = (); |
|
84 |
while (my @row = $r->fetch) { |
|
85 |
push @rows, [@row]; |
|
86 |
} |
|
add tests
|
87 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch list context'); |
cleanup
|
88 |
|
add tests
|
89 |
|
add various thins
|
90 |
$r = $dbi->execute("select k1, k2 from t1;"); |
cleanup
|
91 |
|
92 |
@rows = (); |
|
93 |
while (my $row = $r->fetch_hash) { |
|
94 |
push @rows, {%$row}; |
|
95 |
} |
|
add tests
|
96 |
is_deeply(\@rows, [{k1 => 1, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch_hash'); |
add tests
|
97 |
|
add tests
|
98 |
|
add various thins
|
99 |
$r = $dbi->execute("select k1, k2 from t1;"); |
cleanup
|
100 |
|
add tests
|
101 |
@rows = (); |
102 |
while (my %row = $r->fetch_hash) { |
|
103 |
push @rows, {%row}; |
|
104 |
} |
|
add tests
|
105 |
is_deeply(\@rows, [{k1 => 1, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch hash list context'); |
cleanup
|
106 |
|
107 |
|
|
add various thins
|
108 |
$r = $dbi->execute("select k1, k2 from t1"); |
add tests
|
109 |
|
110 |
$rows = $r->fetch_all; |
|
add tests
|
111 |
is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all'); |
add tests
|
112 |
|
113 |
|
|
add various thins
|
114 |
$r = $dbi->execute("select k1, k2 from t1"); |
add tests
|
115 |
|
116 |
@rows = $r->fetch_all; |
|
117 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all list context'); |
|
118 |
|
|
119 |
|
|
add various thins
|
120 |
$r = $dbi->execute("select k1, k2 from t1"); |
add tests
|
121 |
|
122 |
@rows = $r->fetch_all_hash; |
|
123 |
is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all_hash'); |
|
cleanup
|
124 |
|
125 |
|
|
add various thins
|
126 |
$r = $dbi->execute("select k1, k2 from t1"); |
add tests
|
127 |
|
128 |
@rows = $r->fetch_all; |
|
129 |
is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all_hash list context'); |
|
130 |
|
|
131 |
|
|
add test
|
132 |
$dbi->fetch_filter(sub { |
add tests
|
133 |
my ($key, $value, $type, $sth, $i) = @_; |
134 |
if ($key eq 'k1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) { |
|
add test
|
135 |
return $value * 3; |
136 |
} |
|
137 |
return $value; |
|
138 |
}); |
|
139 |
|
|
add various thins
|
140 |
$r = $dbi->execute("select k1, k2 from t1"); |
add test
|
141 |
|
add tests
|
142 |
$rows = $r->fetch_all; |
143 |
|
|
144 |
is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array'); |
|
add test
|
145 |
|
add tests
|
146 |
|
add various thins
|
147 |
$r = $dbi->execute("select k1, k2 from t1"); |
add tests
|
148 |
|
149 |
$rows = $r->fetch_all_hash; |
|
150 |
|
|
151 |
is_deeply($rows, [{k1 => 3, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch_filter hash'); |
|
152 |
|
|
add test module
|
153 |
}); |
154 |
|