1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package Bucky::DB;
use base 'Bucky';
use Data::Dumper;
use DBI;
my $DB_LOOKUP =
{
bucky => '',
user => 'users',
keyword => 'keywords',
thread => 'threads',
file => 'files',
comment => 'comments',
family => 'family',
search_log => 'search_log',
poetaster_log => 'poetaster_log',
svn => 'svn',
};
sub insert
{
my ($self, $type, $record) = @_;
$type = $DB_LOOKUP->{$type};
return unless $type && ref($record) eq "HASH" && scalar keys %$record;
my $keys = [];
my $values = [];
foreach my $key (keys %$record)
{
push @$keys, $key;
push @$values, $self->quote($record->{$key});
}
my $key_string = join ",", @$keys;
my $value_string = join ",", @$values;
return unless length $key_string && length $value_string;
my $sql = "INSERT INTO $type ($key_string) VALUES($value_string)";
$self->execute($sql);
return $self->lastinsertid($sql);
}
sub update
{
my ($self, $type, $opt) = @_;
my $criteria = $opt->{'criteria'};
my $record = $opt->{'record'};
$type = $DB_LOOKUP->{$type};
return unless $type && ref($record) eq "HASH" && scalar keys %$record;
my $key_values = [];
foreach my $key (keys %$record)
{
push @$key_values, $key . "=" . $self->quote($record->{$key});
}
my $key_value_string = join ",", @$key_values;
return unless length $key_value_string;
my $criteria_string = $self->criteria($criteria);
return unless length $criteria_string;
my $sql = "UPDATE $type SET $key_value_string WHERE $criteria_string";
$self->execute($sql);
}
sub select
{
my ($self, $type, $criteria) = @_;
$type = $DB_LOOKUP->{$type};
return unless $type;
my $criteria_string = $self->criteria($criteria);
my $rows = [];
my $sql = "SELECT * FROM $type";
$sql .= " " . $criteria_string if $criteria_string;
my $sth = $self->execute($sql);
while (my $row = $sth->fetchrow_hashref)
{
push @$rows, $row;
}
return $rows;
}
sub select_by_id
{
my ($self, $type, $id_array) = @_;
$type = $DB_LOOKUP->{$type};
return unless $type and ref($id_array) eq "ARRAY" and scalar @$id_array;
my $rows = {};
my $ids = join ",", @$id_array;
my $sql = "SELECT * FROM $type";
$sql .= " WHERE id IN ($ids)";
my $sth = $self->execute($sql);
while (my $row = $sth->fetchrow_hashref)
{
$rows->{ $row->{'id'} } = $row;
}
return $rows;
}
sub criteria
{
my ($self, $criteria) = @_;
my $criteria_list = [];
if ($self->is_string($criteria))
{
push @$criteria_list, $criteria;
}
elsif (ref $criteria eq "HASH")
{
foreach my $key (keys %$criteria)
{
my $criterion = $key;
if ($criteria->{$key})
{ $criterion .= "=" . $self->quote($criteria->{$key}); }
push @$criteria_list, $criterion;
}
}
return undef unless scalar @$criteria_list;
my $criteria_string = join(" AND ", @$criteria_list);
$criteria_string = "WHERE " . $criteria_string if $criteria_string =~ /[=<>]|( (IS|IN) )/;
return $criteria_string;
}
sub execute
{
my ($self, $sql) = @_;
my $sth = $self->dbh->prepare($sql);
$sth->execute;
return $sth;
}
sub quote
{
my ($self, $string) = @_;
return $self->dbh->quote($string);
}
sub lastinsertid
{
my ($self) = @_;
return $self->dbh->last_insert_id(0, undef, undef, undef);
}
sub dbh
{
my ($self, $parent) = @_;
if ($parent && ref($parent) =~ /Bucky/)
{
$self->{_dbh} ||= $parent->dbh;
}
if (! $self->{_dbh})
{
$self->{_dbh} ||= DBI->connect($self->dsn);
}
return $self->{_dbh};
}
sub dsn
{
my ($self) = @_;
$self->{_dsn} ||=
"DBI:mysql:database=" . $self->db_name .
":" . $self->db_host .
";mysql_read_default_file=" . $self->my_cnf;
return $self->{_dsn};
}
sub db_name
{ 'bucky' }
sub db_host
{ 'localhost' }
sub my_cnf
{ '/var/www/vhosts/carbonpictures.com/.my.cnf' }
1;
|