line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::orMapper; |
2
|
1
|
|
|
1
|
|
37744
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
2351
|
use DBI; |
|
1
|
|
|
|
|
20848
|
|
|
1
|
|
|
|
|
1856
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
orMapper - DBI base easy O/R Mapper. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Class::orMapper; |
15
|
|
|
|
|
|
|
my $read_database = { |
16
|
|
|
|
|
|
|
dsn => 'dbi:xxxx:dbname=xxxx;host=localhost;port=xxxx', |
17
|
|
|
|
|
|
|
uid => 'xxxx', |
18
|
|
|
|
|
|
|
pwd => 'xxxx', |
19
|
|
|
|
|
|
|
opt => {AutoCommit => 0}, |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
my $write_database = { |
22
|
|
|
|
|
|
|
dsn => 'dbi:xxxx:dbname=xxxx;host=localhost;port=xxxx', |
23
|
|
|
|
|
|
|
uid => 'xxxx', |
24
|
|
|
|
|
|
|
pwd => 'xxxx', |
25
|
|
|
|
|
|
|
opt => {AutoCommit => 0}, |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
my $db = new Class::orMapper($read_database, $write_database); |
28
|
|
|
|
|
|
|
my $data = $db->select_arrayref({ |
29
|
|
|
|
|
|
|
table => 'xxxx', |
30
|
|
|
|
|
|
|
columns => [qw/aaa bbb ccc/], |
31
|
|
|
|
|
|
|
where => [ |
32
|
|
|
|
|
|
|
{aaa => {'=' => 'dddd'}}, |
33
|
|
|
|
|
|
|
], |
34
|
|
|
|
|
|
|
order => {'bbb' => 'desc'}, |
35
|
|
|
|
|
|
|
}); |
36
|
|
|
|
|
|
|
use Data::Dumper; |
37
|
|
|
|
|
|
|
warn Dumper($data); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 DESCRIPTION |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
This Module is easy database operation module. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 Usage |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $data = $db->select_n_arrayref($sql,$value); # $data is Array Reference. |
46
|
|
|
|
|
|
|
my $data = $db->select_n_hashref($sql,$value); # $data is Hash Reference. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$sql : SQL(Strings) |
49
|
|
|
|
|
|
|
$value: Bind variable with Array Reference. |
50
|
|
|
|
|
|
|
ex.) my $sql = "select * from test where hoge=?"; |
51
|
|
|
|
|
|
|
my $value = [qw/abc/]; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $data = $db->select_arrayref($param); |
54
|
|
|
|
|
|
|
my $data = $db->select_hashref($param); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
parameter format: |
57
|
|
|
|
|
|
|
$param = { |
58
|
|
|
|
|
|
|
table => 'table_name', |
59
|
|
|
|
|
|
|
columns => [aaa,bbb,ccc], |
60
|
|
|
|
|
|
|
where => [ |
61
|
|
|
|
|
|
|
{xxx => {'=' => 'value1', '>' => 'value2'}}, |
62
|
|
|
|
|
|
|
{xxx => [qw/abc def cfg/], |
63
|
|
|
|
|
|
|
], |
64
|
|
|
|
|
|
|
order => {'yyy' => 'desc', 'zzz' => 'asc'}, |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$db->insert($param); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
parameter format: |
70
|
|
|
|
|
|
|
$param = { |
71
|
|
|
|
|
|
|
table => 'table_name', |
72
|
|
|
|
|
|
|
columns => { |
73
|
|
|
|
|
|
|
aaa => 'bbb', |
74
|
|
|
|
|
|
|
ccc => 'ddd', |
75
|
|
|
|
|
|
|
eee => 'fff', |
76
|
|
|
|
|
|
|
}, |
77
|
|
|
|
|
|
|
}; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$db->update($param); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
parameter format: |
82
|
|
|
|
|
|
|
$param = { |
83
|
|
|
|
|
|
|
table => 'table_name', |
84
|
|
|
|
|
|
|
columns => { |
85
|
|
|
|
|
|
|
aaa => 'bbb', |
86
|
|
|
|
|
|
|
ccc => 'ddd', |
87
|
|
|
|
|
|
|
eee => 'fff', |
88
|
|
|
|
|
|
|
}, |
89
|
|
|
|
|
|
|
where => [ |
90
|
|
|
|
|
|
|
{xxx => {'=' => 'value1', '>' => 'value2'}}, |
91
|
|
|
|
|
|
|
{xxx => [qw/abc def cfg/], |
92
|
|
|
|
|
|
|
], |
93
|
|
|
|
|
|
|
}; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
$db->delete($param); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
parameter format: |
98
|
|
|
|
|
|
|
$param = { |
99
|
|
|
|
|
|
|
table => 'table_name', |
100
|
|
|
|
|
|
|
where => [ |
101
|
|
|
|
|
|
|
{xxx => {'=' => 'value1', '>' => 'value2'}}, |
102
|
|
|
|
|
|
|
{xxx => [qw/abc def cfg/], |
103
|
|
|
|
|
|
|
], |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$db->truncate($param); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
parameter format: |
109
|
|
|
|
|
|
|
$param = { |
110
|
|
|
|
|
|
|
table => 'table_name', |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 Copyright |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Kazunori Minoda (c)2012 |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub new{ |
120
|
0
|
|
|
0
|
0
|
|
my ($this,$db_r,$db_w) = @_; |
121
|
0
|
|
0
|
|
|
|
my $dbh_r = DBI->connect($db_r->{dsn},$db_r->{uid},$db_r->{pwd},$db_r->{opt}) |
122
|
|
|
|
|
|
|
||die $DBI::errstr; |
123
|
0
|
|
0
|
|
|
|
my $dbh_w = DBI->connect($db_w->{dsn},$db_w->{uid},$db_w->{pwd},$db_w->{opt}) |
124
|
|
|
|
|
|
|
||die $DBI::errstr; |
125
|
0
|
|
|
|
|
|
my $self = { |
126
|
|
|
|
|
|
|
dbh_r => $dbh_r, |
127
|
|
|
|
|
|
|
dbh_w => $dbh_w, |
128
|
|
|
|
|
|
|
}; |
129
|
0
|
|
|
|
|
|
return bless($self,$this); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub DESTROY{ |
133
|
0
|
|
|
0
|
|
|
my $self = shift; |
134
|
0
|
0
|
|
|
|
|
$self->{dbh_r}->disconnect if($self->{dbh_r}); |
135
|
0
|
0
|
|
|
|
|
$self->{dbh_w}->disconnect if($self->{dbh_w}); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# select |
139
|
|
|
|
|
|
|
sub select_n_arrayref{ |
140
|
0
|
|
|
0
|
0
|
|
my ($self,$s,$v) = @_; |
141
|
0
|
|
|
|
|
|
my $sth = $self->{dbh_r}->prepare($s); |
142
|
0
|
|
|
|
|
|
$sth->execute(@{$v}); |
|
0
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
my @o; |
144
|
0
|
|
|
|
|
|
while(my $r = $sth->fetchrow_arrayref){ |
145
|
0
|
0
|
|
|
|
|
my @tmp = map{$_?$_:''} @{$r}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
push(@o, \@tmp); |
147
|
|
|
|
|
|
|
} |
148
|
0
|
|
|
|
|
|
$sth->finish; |
149
|
0
|
|
|
|
|
|
return \@o; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub select_n_hashref{ |
153
|
0
|
|
|
0
|
0
|
|
my ($self,$s,$v) = @_; |
154
|
0
|
|
|
|
|
|
my $sth = $self->{dbh_r}->prepare($s); |
155
|
0
|
|
|
|
|
|
$sth->execute(@{$v}); |
|
0
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
my @o; |
157
|
0
|
|
|
|
|
|
while(my $r = $sth->fetchrow_hashref){ |
158
|
0
|
|
|
|
|
|
push(@o, $r); |
159
|
|
|
|
|
|
|
} |
160
|
0
|
|
|
|
|
|
$sth->finish; |
161
|
0
|
|
|
|
|
|
return \@o; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub select_arrayref{ |
165
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
166
|
0
|
|
|
|
|
|
my ($s,@v) = $self->select_base($p); |
167
|
0
|
|
|
|
|
|
my $sth = $self->{dbh_r}->prepare($s); |
168
|
0
|
|
|
|
|
|
$sth->execute(@v); |
169
|
0
|
|
|
|
|
|
my @o; |
170
|
0
|
|
|
|
|
|
while(my $r = $sth->fetchrow_arrayref){ |
171
|
0
|
0
|
|
|
|
|
my @tmp = map{$_?$_:''} @{$r}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
push(@o, \@tmp); |
173
|
|
|
|
|
|
|
} |
174
|
0
|
|
|
|
|
|
$sth->finish; |
175
|
0
|
|
|
|
|
|
return \@o; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub select_hashref{ |
179
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
180
|
0
|
|
|
|
|
|
my ($s,@v) = $self->select_base($p); |
181
|
0
|
|
|
|
|
|
my $sth = $self->{dbh_r}->prepare($s); |
182
|
0
|
|
|
|
|
|
$sth->execute(@v); |
183
|
0
|
|
|
|
|
|
my @o; |
184
|
0
|
|
|
|
|
|
while(my $r = $sth->fetchrow_hashref){ |
185
|
0
|
|
|
|
|
|
push(@o, $r); |
186
|
|
|
|
|
|
|
} |
187
|
0
|
|
|
|
|
|
$sth->finish; |
188
|
0
|
|
|
|
|
|
return \@o; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# insert |
192
|
|
|
|
|
|
|
sub insert{ |
193
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
194
|
0
|
|
|
|
|
|
my ($s,@v); |
195
|
0
|
|
|
|
|
|
$s = "insert into " . $p->{table} . "(" . join(",",map{push(@v,$p->{columns}->{$_});$_} keys %{$p->{columns}}) . ") values(" . join(',',map{$_ = '?';$_} values %{$p->{columns}}) . ")"; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
my $sth = $self->{dbh_w}->prepare($s); |
197
|
0
|
|
|
|
|
|
$sth->execute(@v); |
198
|
0
|
|
|
|
|
|
$sth->finish; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# update |
202
|
|
|
|
|
|
|
sub update{ |
203
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
204
|
0
|
|
|
|
|
|
my ($s,@v); |
205
|
0
|
|
|
|
|
|
$s = "update " . $p->{table} . " set " . join(',', map{push(@v,$p->{columns}->{$_});$_ = $_ . '=?'} keys %{$p->{columns}}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
my ($w,@vv) = where($p); |
207
|
0
|
0
|
|
|
|
|
if($w){ |
208
|
0
|
|
|
|
|
|
$w =~ s/ and //; |
209
|
0
|
|
|
|
|
|
$s .= ' where ' . $w; |
210
|
|
|
|
|
|
|
} |
211
|
0
|
|
|
|
|
|
push(@v,$_) for (@vv); |
212
|
0
|
|
|
|
|
|
my $sth = $self->{dbh_w}->prepare($s); |
213
|
0
|
|
|
|
|
|
$sth->execute(@v); |
214
|
0
|
|
|
|
|
|
$sth->finish; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# delete |
218
|
|
|
|
|
|
|
sub delete{ |
219
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
220
|
0
|
|
|
|
|
|
my $s = "delete from " . $p->{table}; |
221
|
0
|
|
|
|
|
|
my ($w,@v) = where($p); |
222
|
0
|
0
|
|
|
|
|
if($w){ |
223
|
0
|
|
|
|
|
|
$w =~ s/ and //; |
224
|
0
|
|
|
|
|
|
$s .= ' where ' . $w; |
225
|
|
|
|
|
|
|
} |
226
|
0
|
|
|
|
|
|
my $sth = $self->{dbh_w}->prepare($s); |
227
|
0
|
|
|
|
|
|
$sth->execute(@v); |
228
|
0
|
|
|
|
|
|
$sth->finish; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# truncate |
232
|
|
|
|
|
|
|
sub truncate{ |
233
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
234
|
0
|
|
|
|
|
|
my $s = "truncate table " . $p->{table}; |
235
|
0
|
|
|
|
|
|
$self->{dbh_w}->do($s); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
# internal use function |
239
|
|
|
|
|
|
|
sub select_base{ |
240
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
241
|
0
|
|
|
|
|
|
my $s = "select " . join(',',@{$p->{columns}}) . " from " . $p->{table}; |
|
0
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
my ($w,@v) = where($p); |
243
|
0
|
0
|
|
|
|
|
if($w){ |
244
|
0
|
|
|
|
|
|
$w =~ s/ and //; |
245
|
0
|
|
|
|
|
|
$s .= ' where ' . $w; |
246
|
|
|
|
|
|
|
} |
247
|
0
|
|
|
|
|
|
my $o; |
248
|
0
|
0
|
|
|
|
|
if($p->{order}){ |
249
|
0
|
|
|
|
|
|
$o .= ',' . $_ . ' ' . $p->{order}->{$_} for (keys %{$p->{order}}); |
|
0
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
} |
251
|
0
|
0
|
|
|
|
|
if($o){ |
252
|
0
|
|
|
|
|
|
$o =~ s/^,//; |
253
|
0
|
|
|
|
|
|
$s .= ' order by ' . $o; |
254
|
|
|
|
|
|
|
} |
255
|
0
|
|
|
|
|
|
return ($s,@v); |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub where{ |
259
|
0
|
|
|
0
|
0
|
|
my $p = shift; |
260
|
0
|
|
|
|
|
|
my ($w,@v); |
261
|
0
|
|
|
|
|
|
for my $ww (@{$p->{where}}){ |
|
0
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
for my $www (keys %{$ww}){ |
|
0
|
|
|
|
|
|
|
263
|
0
|
0
|
|
|
|
|
if(ref($ww->{$www}) eq 'ARRAY'){ |
|
|
0
|
|
|
|
|
|
264
|
0
|
|
|
|
|
|
$w .= ' and ' . $www . ' in (' . join(",",map{push(@v,$_); $_ = '?'; $_ } @{$ww->{$www}}) . ')'; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
elsif(ref($ww->{$www}) eq 'HASH'){ |
267
|
0
|
|
|
|
|
|
for (keys %{$ww->{$www}}){ |
|
0
|
|
|
|
|
|
|
268
|
0
|
|
|
|
|
|
$w .= ' and ' . $www . $_ . '?'; |
269
|
0
|
|
|
|
|
|
push(@v,$ww->{$www}->{$_}); |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
} |
274
|
0
|
|
|
|
|
|
return ($w,@v); |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
1; |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|