| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MySQL::mycrud; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20367
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
33
|
|
|
4
|
1
|
|
|
1
|
|
3697
|
use DBI; |
|
|
1
|
|
|
|
|
17901
|
|
|
|
1
|
|
|
|
|
68
|
|
|
5
|
1
|
|
|
1
|
|
10
|
use Carp qw/croak/; |
|
|
1
|
|
|
|
|
8
|
|
|
|
1
|
|
|
|
|
61
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
9
|
use vars qw/$VERSION/; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
537
|
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.03'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
|
12
|
|
|
|
|
|
|
|
|
13
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
14
|
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
my $db = shift; |
|
16
|
0
|
|
|
|
|
|
my $host = shift; |
|
17
|
0
|
|
|
|
|
|
my $port = shift; |
|
18
|
0
|
|
|
|
|
|
my $user = shift; |
|
19
|
0
|
|
|
|
|
|
my $passwd = shift; |
|
20
|
|
|
|
|
|
|
|
|
21
|
0
|
0
|
|
|
|
|
my $dbh = DBI->connect("dbi:mysql:database=$db;host=$host;port=$port", $user, $passwd, |
|
22
|
|
|
|
|
|
|
{ |
|
23
|
|
|
|
|
|
|
PrintError => 0, |
|
24
|
|
|
|
|
|
|
RaiseError => 1, |
|
25
|
|
|
|
|
|
|
}) or croak $DBI::errstr; |
|
26
|
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
bless { 'dbh'=>$dbh }, $class; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub get_rows { |
|
32
|
|
|
|
|
|
|
|
|
33
|
0
|
|
|
0
|
1
|
|
my ($self,$str,$ref) = @_; |
|
34
|
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
my @values; |
|
36
|
0
|
0
|
|
|
|
|
@values = @$ref if defined $ref; |
|
37
|
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}; |
|
39
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($str); |
|
40
|
|
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
|
$sth->execute(@values) or croak $dbh->errstr; |
|
42
|
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my @records; |
|
44
|
0
|
|
|
|
|
|
while ( my $ref = $sth->fetchrow_hashref ) { |
|
45
|
0
|
|
|
|
|
|
push @records, $ref; |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
$sth->finish; |
|
49
|
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return \@records; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub get_row { |
|
55
|
|
|
|
|
|
|
|
|
56
|
0
|
|
|
0
|
1
|
|
my ($self,$str,$ref) = @_; |
|
57
|
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my @values; |
|
59
|
0
|
0
|
|
|
|
|
@values = @$ref if defined $ref; |
|
60
|
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}; |
|
62
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($str); |
|
63
|
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
$sth->execute(@values) or croak $dbh->errstr; |
|
65
|
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
my @records = $sth->fetchrow_array; |
|
67
|
0
|
|
|
|
|
|
$sth->finish; |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
return @records; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub do_sql { |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
|
|
0
|
1
|
|
my ($self,$str,$ref) = @_; |
|
76
|
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my @values; |
|
78
|
0
|
0
|
|
|
|
|
@values = @$ref if defined $ref; |
|
79
|
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}; |
|
81
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($str); |
|
82
|
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
$sth->execute(@values) or croak $dbh->errstr; |
|
84
|
0
|
|
|
|
|
|
$sth->finish; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub disconnect { |
|
89
|
|
|
|
|
|
|
|
|
90
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
91
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}; |
|
92
|
0
|
|
|
|
|
|
$dbh->disconnect; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# |
|
97
|
|
|
|
|
|
|
#self destroy |
|
98
|
|
|
|
|
|
|
# |
|
99
|
|
|
|
|
|
|
sub DESTROY { |
|
100
|
|
|
|
|
|
|
|
|
101
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
102
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}; |
|
103
|
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
if ($dbh) { |
|
105
|
0
|
|
|
0
|
|
|
local $SIG{'__WARN__'} = sub {}; |
|
|
0
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
$dbh->disconnect(); |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 NAME |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
MySQL::mycrud - nothing but the mysql methods for myself |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 VERSION |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Version 0.03 |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
use MySQL::mycrud; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# connect to the database |
|
126
|
|
|
|
|
|
|
my $db = MySQL::mycrud->new('database_name','host','port','user','password'); |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# get one row |
|
129
|
|
|
|
|
|
|
my ($name,$age) = $db->get_row("select name,age from table where id=123"); # or |
|
130
|
|
|
|
|
|
|
my ($name,$age) = $db->get_row("select name,age from table where id=?",[123]); |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# get many rows |
|
133
|
|
|
|
|
|
|
my $rr = $db->get_rows("select * from table where id between 123 and 456"); # or |
|
134
|
|
|
|
|
|
|
my $rr = $db->get_rows("select * from table where id between ? and ?",[123,456]); |
|
135
|
|
|
|
|
|
|
for my $r (@$rr) { # each element is a hash ref |
|
136
|
|
|
|
|
|
|
print $r->{name},$r->{age}; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# do updates |
|
140
|
|
|
|
|
|
|
$db->do_sql("insert into table(name,age) values(?,?)",['John Doe',30]); |
|
141
|
|
|
|
|
|
|
$db->do_sql("update table set age=32 where id=123"); |
|
142
|
|
|
|
|
|
|
$db->do_sql("delete from table where id=123"); |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# disconnect it |
|
145
|
|
|
|
|
|
|
$db->disconnect; |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 METHODS |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 new(db_name,host,port,user,passwd) |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
my $db = MySQL::mycrud->new('database_name','host','port','user','password'); |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
create the object and connect to the database. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 get_row(sql) |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my ($name,$age) = $db->get_row("select name,age from table where id=123"); |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
get one row, the result returned is a list. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 get_rows(sql) |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my $rr = $db->get_rows("select * from table where id between 123 and 456"); |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
get rows, the result returned is an array reference, each element in the array is a hash reference. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 do_sql(sql) |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$db->do_sql("insert into table(name,age) values(?,?)",['John Doe',30]); |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
run any sql for updates, including insert,replace,update,delete,drop etc. |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 disconnect() |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
$db->disconnect; |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
disconnect from the database. anyway if $db is gone out of the scope, the database will be disconnected automatically. |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
DBI DBD::mysql |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Ken Peng |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 BUGS/LIMITATIONS |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
If you have found bugs, please send email to |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SUPPORT |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
perldoc MySQL::mycrud |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Copyright 2012 Ken Peng, all rights reserved. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
|
208
|
|
|
|
|
|
|
it under the same terms as Perl itself. |