line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# DESCRIPTION |
3
|
|
|
|
|
|
|
# PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl |
4
|
|
|
|
|
|
|
# library that implements object-relational mapping. Its features are |
5
|
|
|
|
|
|
|
# much similar to those of Java's Hibernate library, but interface is |
6
|
|
|
|
|
|
|
# much different and easier to use. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# AUTHOR |
9
|
|
|
|
|
|
|
# Alexey V. Akimov |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# COPYRIGHT |
12
|
|
|
|
|
|
|
# Copyright (C) 2005-2006 Alexey V. Akimov |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# This library is free software; you can redistribute it and/or |
15
|
|
|
|
|
|
|
# modify it under the terms of the GNU Lesser General Public |
16
|
|
|
|
|
|
|
# License as published by the Free Software Foundation; either |
17
|
|
|
|
|
|
|
# version 2.1 of the License, or (at your option) any later version. |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
# This library is distributed in the hope that it will be useful, |
20
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
22
|
|
|
|
|
|
|
# Lesser General Public License for more details. |
23
|
|
|
|
|
|
|
# |
24
|
|
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public |
25
|
|
|
|
|
|
|
# License along with this library; if not, write to the Free Software |
26
|
|
|
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
package ORM::Db; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$VERSION=0.8; |
32
|
|
|
|
|
|
|
|
33
|
4
|
|
|
4
|
|
2949
|
use ORM; |
|
4
|
|
|
|
|
21
|
|
|
4
|
|
|
|
|
121
|
|
34
|
4
|
|
|
4
|
|
3105
|
use ORM::DbLog; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
3447
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
## use: $db = $class->new( ... ); |
37
|
|
|
|
|
|
|
## |
38
|
|
|
|
|
|
|
## Constructor of database connection. |
39
|
|
|
|
|
|
|
## Parameters is up to derived class. |
40
|
|
|
|
|
|
|
## |
41
|
|
|
|
|
|
|
sub new |
42
|
|
|
|
|
|
|
{ |
43
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'new' in '$_[0]'"; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
## use: $db->disconnect; |
47
|
|
|
|
|
|
|
## |
48
|
|
|
|
|
|
|
## Close connection to storage engine if makes sense. |
49
|
|
|
|
|
|
|
## $db should automatically reconnect to storage engine |
50
|
|
|
|
|
|
|
## upon any request that uses the connection. |
51
|
|
|
|
|
|
|
## |
52
|
|
|
|
|
|
|
## Used to implement backup servers. |
53
|
|
|
|
|
|
|
## |
54
|
|
|
|
|
|
|
sub disconnect |
55
|
|
|
|
|
|
|
{ |
56
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'disconnect' in '$_[0]'"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
## use: $number = $class->count |
60
|
|
|
|
|
|
|
## ( |
61
|
|
|
|
|
|
|
## filter => ORM::Expr, |
62
|
|
|
|
|
|
|
## error => ORM::Error, |
63
|
|
|
|
|
|
|
## ); |
64
|
|
|
|
|
|
|
## |
65
|
|
|
|
|
|
|
## Count objects of class 'class' filtered by 'filter' |
66
|
|
|
|
|
|
|
## |
67
|
|
|
|
|
|
|
sub count |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'count' in '$_[0]'"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
## use: $result_set = $class->select_base |
73
|
|
|
|
|
|
|
## ( |
74
|
|
|
|
|
|
|
## filter => ORM::Expr, |
75
|
|
|
|
|
|
|
## order => ORM::Order, |
76
|
|
|
|
|
|
|
## page => interger, |
77
|
|
|
|
|
|
|
## pagesize => interger, |
78
|
|
|
|
|
|
|
## error => ORM::Error, |
79
|
|
|
|
|
|
|
## ); |
80
|
|
|
|
|
|
|
## |
81
|
|
|
|
|
|
|
## Select rows from tables corresponding to base class 'class' |
82
|
|
|
|
|
|
|
## matched by 'filter'. |
83
|
|
|
|
|
|
|
## |
84
|
|
|
|
|
|
|
sub select_base |
85
|
|
|
|
|
|
|
{ |
86
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'select_base' in '$_[0]'"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
## use: $result_set = $class->select_full |
90
|
|
|
|
|
|
|
## ( |
91
|
|
|
|
|
|
|
## filter => ORM::Expr, |
92
|
|
|
|
|
|
|
## order => ORM::Order, |
93
|
|
|
|
|
|
|
## page => interger, |
94
|
|
|
|
|
|
|
## pagesize => interger, |
95
|
|
|
|
|
|
|
## error => ORM::Error, |
96
|
|
|
|
|
|
|
## ); |
97
|
|
|
|
|
|
|
## |
98
|
|
|
|
|
|
|
## Select rows from tables corresponding to base class 'class' |
99
|
|
|
|
|
|
|
## or its descendants matched by 'filter'. |
100
|
|
|
|
|
|
|
## |
101
|
|
|
|
|
|
|
## This method have to check presence of each object in cache on it's own. |
102
|
|
|
|
|
|
|
## If it has found one, then cached object should be returned instead of |
103
|
|
|
|
|
|
|
## raw data hash. |
104
|
|
|
|
|
|
|
## |
105
|
|
|
|
|
|
|
## Must not be called for sealed classes. |
106
|
|
|
|
|
|
|
## |
107
|
|
|
|
|
|
|
sub select_full |
108
|
|
|
|
|
|
|
{ |
109
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'select_full' in '$_[0]'"; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
## use: $result_set = $class->select_tables |
113
|
|
|
|
|
|
|
## ( |
114
|
|
|
|
|
|
|
## id => (integer || string), |
115
|
|
|
|
|
|
|
## tables => HASH, |
116
|
|
|
|
|
|
|
## error => ORM::Error, |
117
|
|
|
|
|
|
|
## ); |
118
|
|
|
|
|
|
|
## |
119
|
|
|
|
|
|
|
## Select joined rows from tables 'tables' with id='id'. |
120
|
|
|
|
|
|
|
## 'id' can be string of format 'id1,id2,id3,...'. |
121
|
|
|
|
|
|
|
## Caller should make sure that 'id' string is quoted properly. |
122
|
|
|
|
|
|
|
## |
123
|
|
|
|
|
|
|
sub select_tables |
124
|
|
|
|
|
|
|
{ |
125
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'select_tables' in '$_[0]'"; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
## use: $result_set = $class->select_stat |
129
|
|
|
|
|
|
|
## ( |
130
|
|
|
|
|
|
|
## filter => ORM::Expr, |
131
|
|
|
|
|
|
|
## |
132
|
|
|
|
|
|
|
## data => { alias=>ORM::Expr, ... }, |
133
|
|
|
|
|
|
|
## group_by => [ ORM::Ident|ORM::Metaprop, ... ], |
134
|
|
|
|
|
|
|
## post_filter => ORM::Expr, |
135
|
|
|
|
|
|
|
## |
136
|
|
|
|
|
|
|
## order => ORM::Order, |
137
|
|
|
|
|
|
|
## page => integer, |
138
|
|
|
|
|
|
|
## pagesize => integer, |
139
|
|
|
|
|
|
|
## error => ORM::Error->new, |
140
|
|
|
|
|
|
|
## ); |
141
|
|
|
|
|
|
|
## |
142
|
|
|
|
|
|
|
sub select_stat |
143
|
|
|
|
|
|
|
{ |
144
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'select_stat' in '$_[0]'"; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
## use: $insert_id = $class->insert_object |
148
|
|
|
|
|
|
|
## ( |
149
|
|
|
|
|
|
|
## id => number, |
150
|
|
|
|
|
|
|
## object => ORM, |
151
|
|
|
|
|
|
|
## error => ORM::Error, |
152
|
|
|
|
|
|
|
## ); |
153
|
|
|
|
|
|
|
## |
154
|
|
|
|
|
|
|
## Insert values of object properties into corresponding tables. |
155
|
|
|
|
|
|
|
## |
156
|
|
|
|
|
|
|
sub insert_object |
157
|
|
|
|
|
|
|
{ |
158
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'insert_object' in '$_[0]'"; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
## use: $update_id = $class->update_object |
162
|
|
|
|
|
|
|
## ( |
163
|
|
|
|
|
|
|
## object => ORM, |
164
|
|
|
|
|
|
|
## values => hash, |
165
|
|
|
|
|
|
|
## error => ORM::Error, |
166
|
|
|
|
|
|
|
## ); |
167
|
|
|
|
|
|
|
## |
168
|
|
|
|
|
|
|
## Update values of object properties in corresponding tables. |
169
|
|
|
|
|
|
|
## |
170
|
|
|
|
|
|
|
sub update_object |
171
|
|
|
|
|
|
|
{ |
172
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'update_object' in '$_[0]'"; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
## use: $delete_id = $class->delete_object |
176
|
|
|
|
|
|
|
## ( |
177
|
|
|
|
|
|
|
## object => ORM, |
178
|
|
|
|
|
|
|
## error => ORM::Error, |
179
|
|
|
|
|
|
|
## emulate_foreign_keys => boolean, |
180
|
|
|
|
|
|
|
## ); |
181
|
|
|
|
|
|
|
## |
182
|
|
|
|
|
|
|
## Delete rows with values of object properties from corresponding tables. |
183
|
|
|
|
|
|
|
## |
184
|
|
|
|
|
|
|
sub delete_object |
185
|
|
|
|
|
|
|
{ |
186
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'delete_object' in '$_[0]'"; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
## use: $db->optimize_tables( class=>string, error=>ORM::Error ); |
190
|
|
|
|
|
|
|
## |
191
|
|
|
|
|
|
|
## Simply call SQL's 'OPTIMIZE TABLE' |
192
|
|
|
|
|
|
|
## |
193
|
|
|
|
|
|
|
sub optimize_tables |
194
|
|
|
|
|
|
|
{ |
195
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'optimize_tables' in '$_[0]'"; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
## use: ( $fields, $defaults ) = $class->table_struct |
199
|
|
|
|
|
|
|
## ( |
200
|
|
|
|
|
|
|
## class => string, |
201
|
|
|
|
|
|
|
## table => string, |
202
|
|
|
|
|
|
|
## error => ORM::Error, |
203
|
|
|
|
|
|
|
## ); |
204
|
|
|
|
|
|
|
## |
205
|
|
|
|
|
|
|
## $fields - reference to hash of table field names and types |
206
|
|
|
|
|
|
|
## $defaults - reference to hash of fields default values |
207
|
|
|
|
|
|
|
## |
208
|
|
|
|
|
|
|
sub table_struct |
209
|
|
|
|
|
|
|
{ |
210
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'table_struct' in '$_[0]'"; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
## use: @classes = $class->referencing_classes |
214
|
|
|
|
|
|
|
## ( |
215
|
|
|
|
|
|
|
## class => string, |
216
|
|
|
|
|
|
|
## error => ORM::Error, |
217
|
|
|
|
|
|
|
## ); |
218
|
|
|
|
|
|
|
## |
219
|
|
|
|
|
|
|
## @classes contains references to hashes with |
220
|
|
|
|
|
|
|
## keys 'class' and 'prop' meaning that property |
221
|
|
|
|
|
|
|
## 'prop' of class 'class' references to class 'class' |
222
|
|
|
|
|
|
|
## specified as parameter to the method. |
223
|
|
|
|
|
|
|
## |
224
|
|
|
|
|
|
|
sub referencing_classes |
225
|
|
|
|
|
|
|
{ |
226
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'referencing_classes' in '$_[0]'"; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
## use: $quoted_str = $db->qc( $str ) |
230
|
|
|
|
|
|
|
## |
231
|
|
|
|
|
|
|
## Used to quote constant values. |
232
|
|
|
|
|
|
|
## |
233
|
|
|
|
|
|
|
## If value being quoted is undef, then 'qc' |
234
|
|
|
|
|
|
|
## should return SQL NULL value. |
235
|
|
|
|
|
|
|
## |
236
|
|
|
|
|
|
|
sub qc |
237
|
|
|
|
|
|
|
{ |
238
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
239
|
0
|
|
|
|
|
|
die "You forget to override 'qc' method in '$class'"; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
## use: $quoted_str = $db->qt( $str ) |
243
|
|
|
|
|
|
|
## |
244
|
|
|
|
|
|
|
## Used to table names. |
245
|
|
|
|
|
|
|
## |
246
|
|
|
|
|
|
|
sub qt |
247
|
|
|
|
|
|
|
{ |
248
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
249
|
0
|
|
|
|
|
|
die "You forget to override 'qt' method in '$class'"; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
## use: $quoted_str = $db->qf( $str ) |
253
|
|
|
|
|
|
|
## |
254
|
|
|
|
|
|
|
## Used to quote field names. |
255
|
|
|
|
|
|
|
## |
256
|
|
|
|
|
|
|
sub qf |
257
|
|
|
|
|
|
|
{ |
258
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
259
|
0
|
|
|
|
|
|
die "You forget to override 'qf' method in '$class'"; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
## use: $quoted_str = $db->qi( $str ) |
263
|
|
|
|
|
|
|
## |
264
|
|
|
|
|
|
|
## Used to quote identifier names. |
265
|
|
|
|
|
|
|
## |
266
|
|
|
|
|
|
|
sub qi |
267
|
|
|
|
|
|
|
{ |
268
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
269
|
0
|
|
|
|
|
|
die "You forget to override 'qi' method in '$class'"; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
## use: $quoted_str = $db->ql( $str ) |
273
|
|
|
|
|
|
|
## |
274
|
|
|
|
|
|
|
## Used to quote strings that should take place in LIKE-string. |
275
|
|
|
|
|
|
|
## |
276
|
|
|
|
|
|
|
sub ql |
277
|
|
|
|
|
|
|
{ |
278
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
279
|
0
|
|
|
|
|
|
die "You forget to override 'ql' method in '$class'"; |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
## |
283
|
|
|
|
|
|
|
## TRANSACTIONS |
284
|
|
|
|
|
|
|
## |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
## use: $class->begin_transaction( error=>ORM::Error ); |
287
|
|
|
|
|
|
|
## |
288
|
|
|
|
|
|
|
## This method is used by ORM::Ta, must not be used explicitly. |
289
|
|
|
|
|
|
|
## |
290
|
|
|
|
|
|
|
sub begin_transaction |
291
|
|
|
|
|
|
|
{ |
292
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'begin_transaction' method in '$_[0]'"; |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
## use: $class->commit_transaction( error=>ORM::Error ); |
296
|
|
|
|
|
|
|
## |
297
|
|
|
|
|
|
|
## This method is used by ORM::Ta, must not be used explicitly. |
298
|
|
|
|
|
|
|
## |
299
|
|
|
|
|
|
|
sub commit_transaction |
300
|
|
|
|
|
|
|
{ |
301
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'commit_transaction' method in '$_[0]'"; |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
## use: $class->rollback_transaction( error=>ORM::Error ); |
305
|
|
|
|
|
|
|
## |
306
|
|
|
|
|
|
|
## This method is used by ORM::Ta, must not be used explicitly. |
307
|
|
|
|
|
|
|
## |
308
|
|
|
|
|
|
|
sub rollback_transaction |
309
|
|
|
|
|
|
|
{ |
310
|
0
|
|
|
0
|
0
|
|
die "You forget to override 'rollback_transaction' method in '$_[0]'"; |
311
|
|
|
|
|
|
|
} |