line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBomb::Base; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
DBomb::Base - Provides inheritable methods mapped to database operations. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut |
10
|
|
|
|
|
|
|
|
11
|
8
|
|
|
8
|
|
476674
|
use strict; |
|
8
|
|
|
|
|
19
|
|
|
8
|
|
|
|
|
305
|
|
12
|
8
|
|
|
8
|
|
44
|
use warnings; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
337
|
|
13
|
|
|
|
|
|
|
our $VERSION = '$Revision: 1.27 $'; |
14
|
|
|
|
|
|
|
|
15
|
8
|
|
|
8
|
|
49
|
use DBomb::Query; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
171
|
|
16
|
8
|
|
|
8
|
|
4891
|
use DBomb::GluedQuery; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use DBomb::GluedUpdate; |
18
|
|
|
|
|
|
|
use DBomb::Value::Column; |
19
|
|
|
|
|
|
|
use DBomb::Meta::Key; |
20
|
|
|
|
|
|
|
use DBomb::Tie::PrimaryKeyList; |
21
|
|
|
|
|
|
|
use Carp::Assert; |
22
|
|
|
|
|
|
|
use Carp qw(croak); |
23
|
|
|
|
|
|
|
use base qw(DBomb::Base::Private); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
## new() |
26
|
|
|
|
|
|
|
## new($PrimaryKeyValue) |
27
|
|
|
|
|
|
|
## new($pk_column) |
28
|
|
|
|
|
|
|
## new($dbh) |
29
|
|
|
|
|
|
|
sub new |
30
|
|
|
|
|
|
|
{ |
31
|
|
|
|
|
|
|
my $class = ref($_[0]) ? ref(shift) : shift; |
32
|
|
|
|
|
|
|
my $self = bless +{}, $class; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$self->_dbo_values(+{}); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
## Create values objects |
37
|
|
|
|
|
|
|
for (values %{$self->_dbo_table_info->columns}){ |
38
|
|
|
|
|
|
|
$self->_dbo_values->{$_->name} = new DBomb::Value::Column($_); |
39
|
|
|
|
|
|
|
#TODO: default value |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my ($got_dbh, $got_key) = (0,0); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
for (@_) { |
45
|
|
|
|
|
|
|
last if $got_key && $got_dbh; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
if (UNIVERSAL::isa($_,'DBI::db') && not $got_dbh){ |
48
|
|
|
|
|
|
|
$self->dbh($_); |
49
|
|
|
|
|
|
|
$got_dbh = 1; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
elsif (UNIVERSAL::isa($_,'DBomb::Value::Key') && not $got_key){ |
52
|
|
|
|
|
|
|
$self->_dbo_set_primary_key($_); |
53
|
|
|
|
|
|
|
$got_key = 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
elsif (defined($_) && !ref($_) && !$got_key){ |
56
|
|
|
|
|
|
|
$self->_dbo_set_primary_key($_); |
57
|
|
|
|
|
|
|
$got_key = 1; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
elsif (UNIVERSAL::isa($_,'ARRAY') && not $got_key){ |
60
|
|
|
|
|
|
|
$self->_dbo_set_primary_key($_); |
61
|
|
|
|
|
|
|
$got_key = 1; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
elsif (ref($_) eq 'HASH'){ |
64
|
|
|
|
|
|
|
#TODO: accept +{key=>value} as passed to new DBomb::Value::PrimaryKey |
65
|
|
|
|
|
|
|
die("not implemented yet"); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$self->init; |
70
|
|
|
|
|
|
|
return $self; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
## Meant to be overridden by subclasses. |
74
|
|
|
|
|
|
|
sub init |
75
|
|
|
|
|
|
|
{ |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
## returns a query object |
79
|
|
|
|
|
|
|
## $class->select(@column_aliases_or_names) |
80
|
|
|
|
|
|
|
sub select |
81
|
|
|
|
|
|
|
{ |
82
|
|
|
|
|
|
|
my $class = shift; |
83
|
|
|
|
|
|
|
my @columns = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
push @columns, values %{$class->_dbo_table_info->primary_key->columns} unless @_; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
## promote string names to column_info objects |
88
|
|
|
|
|
|
|
COLUMN: for(@columns){ |
89
|
|
|
|
|
|
|
next if UNIVERSAL::isa($_,'DBomb::Meta::ColumnInfo'); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
if (exists $class->_dbo_table_info->columns->{$_}){ |
92
|
|
|
|
|
|
|
$_ = $class->_dbo_table_info->columns->{$_}; |
93
|
|
|
|
|
|
|
next COLUMN; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
for my $c ( values %{$class->_dbo_table_info->columns}){ |
97
|
|
|
|
|
|
|
if ($_ eq $c->accessor){ |
98
|
|
|
|
|
|
|
$_ = $c; |
99
|
|
|
|
|
|
|
next COLUMN; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
croak "Column '$_' not found in object $class."; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
return new DBomb::GluedQuery($class->_dbo_dbh,$class->_dbo_expand_select_groups([@columns]))->from($class->_dbo_table_info); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# $class->select_count() |
110
|
|
|
|
|
|
|
sub select_count |
111
|
|
|
|
|
|
|
{ |
112
|
|
|
|
|
|
|
my $class = ref($_[0]) ? ref(shift) : shift; |
113
|
|
|
|
|
|
|
return new DBomb::Query($class->_dbo_dbh,["COUNT(*)"])->from($class->_dbo_table_info->name); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
## $class->selectall_arrayref() |
118
|
|
|
|
|
|
|
## $class->selectall_arrayref(@bind_values) |
119
|
|
|
|
|
|
|
## $class->selectall_arrayref($dbh, @bind_values) |
120
|
|
|
|
|
|
|
sub selectall_arrayref |
121
|
|
|
|
|
|
|
{ |
122
|
|
|
|
|
|
|
my ($class, @bind_values) = @_; |
123
|
|
|
|
|
|
|
my $dbh; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$dbh = shift(@bind_values) if UNIVERSAL::isa($bind_values[0],'DBI::db'); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Let $dbh override default dbh |
128
|
|
|
|
|
|
|
$dbh = $class->_dbo_dbh unless defined $dbh; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
$class = ref($class) if ref($class); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
## We don't need a glued query here since we are just selecting the primary key columns. |
133
|
|
|
|
|
|
|
## The tied list will create the objects as needed. |
134
|
|
|
|
|
|
|
my $query = new DBomb::Query($dbh,$class->_dbo_table_info->primary_key->column_names) |
135
|
|
|
|
|
|
|
->from($class->_dbo_table_info->name); |
136
|
|
|
|
|
|
|
my $keys_list = $query->selectall_arrayref; |
137
|
|
|
|
|
|
|
my @arr; |
138
|
|
|
|
|
|
|
tie @arr, 'DBomb::Tie::PrimaryKeyList', $class, $keys_list; |
139
|
|
|
|
|
|
|
return \@arr; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
## delete() |
143
|
|
|
|
|
|
|
## $class->delete() |
144
|
|
|
|
|
|
|
sub delete |
145
|
|
|
|
|
|
|
{ |
146
|
|
|
|
|
|
|
my $self = shift; |
147
|
|
|
|
|
|
|
assert(@_==0, 'delete takes no arguments'); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
if (ref $self){ |
150
|
|
|
|
|
|
|
$self->_dbo_delete(@_); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
else{ |
153
|
|
|
|
|
|
|
$self->_dbo_delete_static(@_); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub insert |
159
|
|
|
|
|
|
|
{ |
160
|
|
|
|
|
|
|
my $self = shift; |
161
|
|
|
|
|
|
|
if (ref $self){ |
162
|
|
|
|
|
|
|
$self->_dbo_insert(@_); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
else{ |
165
|
|
|
|
|
|
|
$self->_dbo_insert_static(@_); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub update |
170
|
|
|
|
|
|
|
{ |
171
|
|
|
|
|
|
|
my $self = shift; |
172
|
|
|
|
|
|
|
if (ref $self){ |
173
|
|
|
|
|
|
|
$self->_dbo_update(@_); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
else{ |
176
|
|
|
|
|
|
|
$self->_dbo_update_static(@_); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub copy_shallow |
181
|
|
|
|
|
|
|
{ |
182
|
|
|
|
|
|
|
my $self = shift; |
183
|
|
|
|
|
|
|
assert(ref($self) && UNIVERSAL::isa($self,__PACKAGE__), "copy_shallow requires an object instance"); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my $id = $self->_dbo_copy_shallow(@_); |
186
|
|
|
|
|
|
|
if (defined $id){ |
187
|
|
|
|
|
|
|
return ref($self)->new($id); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
undef |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub dbo_is_bound |
193
|
|
|
|
|
|
|
{ |
194
|
|
|
|
|
|
|
my $self = shift; |
195
|
|
|
|
|
|
|
$self->_dbo_is_bound; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
1; |
199
|
|
|
|
|
|
|
__END__ |