line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Goo::DatabaseObject; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
############################################################################### |
4
|
|
|
|
|
|
|
# Nigel Hamilton |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright Nigel Hamilton 2005 |
7
|
|
|
|
|
|
|
# All Rights Reserved |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Author: Nigel Hamilton |
10
|
|
|
|
|
|
|
# Filename: GooDatabaseObject.pm |
11
|
|
|
|
|
|
|
# Description: Bridge the relational and OO world! |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# Date Change |
14
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
15
|
|
|
|
|
|
|
# 02/05/2005 Auto generated file |
16
|
|
|
|
|
|
|
# 02/05/2005 Got sick of writing standard SQL |
17
|
|
|
|
|
|
|
# 19/10/2005 Created test file: GooDatabaseObjectTest.tpm |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
############################################################################### |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
6
|
use Goo::Object; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
17
|
|
24
|
1
|
|
|
1
|
|
5
|
use Goo::Database; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# GooDatabaseObject isa Object |
27
|
1
|
|
|
1
|
|
5
|
use base qw(Goo::Object); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
517
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
############################################################################### |
31
|
|
|
|
|
|
|
# |
32
|
|
|
|
|
|
|
# new - construct a goo_database_object object |
33
|
|
|
|
|
|
|
# |
34
|
|
|
|
|
|
|
############################################################################### |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new { |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
0
|
1
|
|
my ($class, $table, $primary_key_value) = @_; |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
my $this = $class->SUPER::new(); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# beware of name clashes in object fields |
43
|
0
|
|
|
|
|
|
$this->{table_name} = $table; |
44
|
0
|
|
|
|
|
|
$this->{primary_key} = Goo::Database::get_primary_key($table); |
45
|
0
|
|
|
|
|
|
$this->{primary_key_value} = $primary_key_value; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# look the object up in the database |
48
|
0
|
0
|
|
|
|
|
if ($this->{primary_key_value}) { |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# look up the database for the object |
51
|
0
|
|
|
|
|
|
my $row = |
52
|
|
|
|
|
|
|
Goo::Database::get_row($this->{table_name}, $this->{primary_key}, |
53
|
|
|
|
|
|
|
$this->{primary_key_value}); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# die if nothing is found |
56
|
0
|
0
|
|
|
|
|
unless ($row) { |
57
|
0
|
|
|
|
|
|
die("No object found for $primary_key_value in $table"); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# it's there! |
61
|
0
|
|
|
|
|
|
$this->{object_exists} = 1; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# add all the columns to the current object - merge hashes |
64
|
0
|
|
|
|
|
|
%$this = (%$this, %$row); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
return $this; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
############################################################################### |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
# delete - delete the current object in the database |
76
|
|
|
|
|
|
|
# |
77
|
|
|
|
|
|
|
############################################################################### |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub delete { |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
0
|
1
|
|
my ($this) = @_; |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
unless ($this->{primary_key_value}) { |
84
|
0
|
|
|
|
|
|
die("Can't delete without a primary key " . $this->to_string()); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
Goo::Database::delete_row($this->{table_name}, $this->{primary_key}, $this->{primary_key_value}); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
############################################################################### |
93
|
|
|
|
|
|
|
# |
94
|
|
|
|
|
|
|
# replace - replace the entire row in the database with the 'state' of the |
95
|
|
|
|
|
|
|
# current object |
96
|
|
|
|
|
|
|
# |
97
|
|
|
|
|
|
|
############################################################################### |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub replace { |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
0
|
1
|
|
my ($this) = @_; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# save the changes to the database |
104
|
0
|
|
|
|
|
|
my @columns = Goo::Database::get_table_columns($this->{table_name}); |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
my $into = join(",", @columns); |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
my @place_holders; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
foreach my $column (@columns) { |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# watch out for dates |
113
|
0
|
0
|
|
|
|
|
if ($this->{$column} eq "now()") { |
114
|
0
|
|
|
|
|
|
push(@place_holders, $this->{$column}); |
115
|
|
|
|
|
|
|
} else { |
116
|
0
|
|
|
|
|
|
push(@place_holders, "?"); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# join up all the place holders |
121
|
0
|
|
|
|
|
|
my $places = join(',', @place_holders); |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $query = Goo::Database::prepare_sql(<<EOSQL); |
124
|
|
|
|
|
|
|
replace into $this->{table_name} ($into) |
125
|
|
|
|
|
|
|
values ($places) |
126
|
|
|
|
|
|
|
EOSQL |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $column_count = 0; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
foreach my $column (@columns) { |
131
|
0
|
0
|
|
|
|
|
next if $this->{$column} eq "now()"; |
132
|
0
|
|
|
|
|
|
$column_count++; |
133
|
0
|
|
|
|
|
|
Goo::Database::bind_param($query, $column_count, $this->{$column}); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
Goo::Database::execute($query); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__END__ |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 NAME |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Goo::DatabaseObject - Bridge between relational and OO model |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 SYNOPSIS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
use Goo::DatabaseObject; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 DESCRIPTION |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 METHODS |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=over |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item new |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
constructor |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item delete |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
delete the current object in the database |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item replace |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
replace the entire row in the database with the current 'state' of the object |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=back |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 AUTHOR |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Nigel Hamilton <nigel@trexy.com> |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 SEE ALSO |
179
|
|
|
|
|
|
|
|