line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package NewBie::Gift::DB; |
2
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
105
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use base 'Object'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
75
|
|
5
|
1
|
|
|
1
|
|
5
|
use Hashtable; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
1262
|
use SQL::Abstract; |
|
1
|
|
|
|
|
34044
|
|
|
1
|
|
|
|
|
61
|
|
7
|
1
|
|
|
1
|
|
10015
|
use DBI; |
|
1
|
|
|
|
|
21819
|
|
|
1
|
|
|
|
|
401
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
|
|
my $pkg = shift; |
11
|
0
|
|
|
|
|
|
my $options = {@_}; |
12
|
0
|
|
0
|
|
|
|
return bless { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
13
|
|
|
|
|
|
|
dsn => $options->{dsn} || 'DBI:mysql:database=test;host=dbhost', |
14
|
|
|
|
|
|
|
user => $options->{username} || 'root', |
15
|
|
|
|
|
|
|
pass => $options->{password} || '' |
16
|
|
|
|
|
|
|
}, $pkg; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub dbh { |
20
|
0
|
|
|
0
|
|
|
my $self = shift; |
21
|
0
|
0
|
|
|
|
|
return ref($self->dbh) eq 'DBI' ? $self->dbh : DBI->connect($self->{dsn}, $self->{user}, $self->{pass}); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub disconnect { |
25
|
0
|
|
|
0
|
|
|
shift->dbh->disconnect; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head2 select |
29
|
|
|
|
|
|
|
Just use SQL::Abstract to generate SQL, so watch SQL::Abstract for options. |
30
|
|
|
|
|
|
|
For example: |
31
|
|
|
|
|
|
|
my fields => ['col1', 'col2'], |
32
|
|
|
|
|
|
|
my $where => { |
33
|
|
|
|
|
|
|
user => 'nwiger', |
34
|
|
|
|
|
|
|
priority => [ |
35
|
|
|
|
|
|
|
{ '=', 2 }, |
36
|
|
|
|
|
|
|
{ '>', 5 }, |
37
|
|
|
|
|
|
|
], |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
my $order => ['col1', {-desc => 'col2'}] |
40
|
|
|
|
|
|
|
select $table, $fields, $where, $order, sub { |
41
|
|
|
|
|
|
|
my ($row) = @_; |
42
|
|
|
|
|
|
|
$row->each(sub{ |
43
|
|
|
|
|
|
|
my ($colname, $colvalue) = @_; |
44
|
|
|
|
|
|
|
}) |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
sub select { |
48
|
0
|
|
|
0
|
|
|
my $self = shift; |
49
|
0
|
|
|
|
|
|
$self->query('select', @_); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub insert { |
53
|
0
|
|
|
0
|
|
|
my $self = shift; |
54
|
0
|
|
|
|
|
|
$self->query('insert', @_); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub update { |
58
|
0
|
|
|
0
|
|
|
my $self = shift; |
59
|
0
|
|
|
|
|
|
$self->query('update', @_); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub delete { |
63
|
0
|
|
|
0
|
|
|
my $self = shift; |
64
|
0
|
|
|
|
|
|
$self->query('delete', @_); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub query { |
68
|
0
|
|
|
0
|
|
|
my ($self, $operator, $table, @options) = @_; |
69
|
0
|
|
|
|
|
|
my $cb; |
70
|
0
|
0
|
|
|
|
|
if (lc($operator) eq 'select') { |
71
|
0
|
|
|
|
|
|
$cb = pop @options; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my $dbh = $self->dbh; |
75
|
0
|
|
|
|
|
|
my $sql = SQL::Abstract->new; |
76
|
0
|
|
|
|
|
|
my ( $stmt, @bind ) = eval $sql->$operator( $table, @options ); |
77
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($stmt); |
78
|
0
|
|
|
|
|
|
my $rv = $sth->execute(@bind); |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
if (lc($operator) eq 'select') { |
81
|
0
|
|
|
|
|
|
while (my $ref = $sth->fetchrow_hashref()) { |
82
|
0
|
|
|
|
|
|
$cb->( Hashtable->new(%$ref) ); |
83
|
|
|
|
|
|
|
}; |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
$sth->finish(); |
87
|
0
|
|
|
|
|
|
return $rv; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |