/
DBR.pm
322 lines (226 loc) · 8.52 KB
/
DBR.pm
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# the contents of this file are Copyright (c) 2004-2009 Daniel Norman
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
package DBR;
use strict;
use DBR::Handle;
use DBR::Config;
use DBR::Config::Instance;
use DBR::Misc::Session;
use Scalar::Util 'blessed';
use base 'DBR::Common';
use DBR::Util::Logger;
use Carp;
our $VERSION = '-DBR-VERSION-TAG-';
my %APP_BY_CONF;
my %CONF_BY_APP;
my %OBJECTS;
my $CT;
sub import {
my $pkg = shift;
my %params = @_;
my ($callpack, $callfile, $callline) = caller;
my $app = $params{app};
my $exc = exists $params{use_exceptions} ? $params{use_exceptions} || 0 : 1;
my $conf;
if( $params{conf} ){
croak "conf file '$params{conf}' not found" unless -e $params{conf};
$conf = $params{conf};
$app ||= $APP_BY_CONF{ $conf } ||= 'auto_' . $CT++; # use existing app id if conf exists, or make one up
$CONF_BY_APP{ $app } = $conf;
}elsif ( defined $app && length $app ){
$conf = $CONF_BY_APP{ $app };
}
return 1 unless $app; # No import requested
if($conf){
$OBJECTS{ $app }{ $exc } ||= DBR->new(
-logger => DBR::Util::Logger->new(
-logpath => $params{logpath} || '/tmp/dbr_auto.log',
-logLevel => $params{loglevel} || 'warn'
),
-conf => $conf,
-use_exceptions => $exc,
);
}
my $dbr = $OBJECTS{ $app }{ $exc } or croak "No DBR object could be located";
no strict 'refs';
*{"${callpack}::dbr_connect"} =
sub {
shift if exists ($_[0]) && (blessed($_[0]) || $_[0]->isa( [caller]->[0] ));
$dbr->connect(@_);
};
*{"${callpack}::dbr_instance"} =
sub {
shift if exists ($_[0]) && (blessed($_[0]) || $_[0]->isa( [caller]->[0] ));
$dbr->get_instance(@_);
};
*{"${callpack}::dbr_schema"} =
sub {
shift if exists ($_[0]) && (blessed($_[0]) || $_[0]->isa( [caller]->[0] ));
$dbr->get_schema(@_);
};
*{"${callpack}::dbr_session"} =
sub {
shift if exists ($_[0]) && (blessed($_[0]) || $_[0]->isa( [caller]->[0] ));
$dbr->session;
};
}
sub new {
my( $package ) = shift;
my %params = @_;
my $self = {logger => $params{-logger}};
bless( $self, $package );
return $self->_error("Error: -conf must be specified") unless $params{-conf};
return $self->_error("Failed to create DBR::Util::Session object") unless
$self->{session} = DBR::Misc::Session->new(
logger => $self->{logger},
admin => $params{-admin} ? 1 : 0, # make the user jump through some hoops for updating metadata
fudge_tz => $params{-fudge_tz},
use_exceptions => $params{-use_exceptions},
);
return $self->_error("Failed to create DBR::Config object") unless
my $config = DBR::Config->new( session => $self->{session} );
$config -> load_file(
dbr => $self,
file => $params{-conf}
) or return $self->_error("Failed to load DBR conf file");
DBR::Config::Instance->flush_all_handles(); # Make it safer for forking
return( $self );
}
sub setlogger {
my $self = shift;
$self->{logger} = shift;
}
sub session { $_[0]->{session} }
sub connect {
my $self = shift;
my $name = shift;
my $class = shift;
my $tag = shift;
my $flag;
if ($class && $class eq 'dbh') { # legacy
$flag = 'dbh';
$class = undef;
}
my $instance = DBR::Config::Instance->lookup(
dbr => $self,
session => $self->{session},
handle => $name,
class => $class,
tag => $tag
) or return $self->_error("No config found for db '$name' class '$class'");
return $instance->connect($flag);
}
sub get_instance {
my $self = shift;
my $name = shift;
my $class = shift;
my $tag = shift;
my $flag;
if ($class && $class eq 'dbh') { # legacy
$flag = 'dbh';
$class = undef;
}
my $instance = DBR::Config::Instance->lookup(
dbr => $self,
session => $self->{session},
handle => $name,
class => $class,
tag => $tag
) or return $self->_error("No config found for db '$name' class '$class'");
return $instance;
}
sub get_schema {
my $self = shift;
my $handle = shift;
my $schema = DBR::Config::Schema->new(
session => $self->{session},
instance_id => -1, # reflection use only
handle => $handle
) or return $self->_error("No schema found for handle '$handle'");
return $schema;
}
sub timezone{
my $self = shift;
my $tz = shift;
$self->{session}->timezone($tz) or return $self->_error('Failed to set timezone');
}
sub remap{
my $self = shift;
my $class = shift;
return $self->_error('class must be specified') unless $class;
$self->{globalclass} = $class;
return 1;
}
sub unmap{ undef $_[0]->{globalclass}; return 1 }
sub flush_handles{ DBR::Config::Instance->flush_all_handles }
sub DESTROY{ $_[0]->flush_handles }
1;
=pod
=head1 NAME
DBR - Database Repository ORM (object-relational mapper).
=head1 DESCRIPTION
DBR (Database Repository) is a fairly directed attempt at an Object Relational Mapper.
It is not trying to be all things to all people. It's focus is on managing large schemas with an
emphasis on metadata, rather than defining schema structure with code.
See L<DBR::Manual> for more details.
=head1 SYNOPSIS
use DBR ( conf => '/path/to/my/DBR.conf' );
my $music = dbr_connect('music');
my $artists = $music->artist->all;
print "Artists:\n";
while (my $artist = $artists->next) {
print "\t" . $artist->name . "\n";
}
=head1 EXPORT
use DBR (
conf => '/path/to/my/DBR.conf' # Required ( unless app is specified )
# Remaining parameters are optional
app => 'myapp' # auto generated by default
use_exceptions => 1, # default
logpath => '/tmp/dbr_auto.log' # default
loglevel => 'warn' # default. allows: none info warn error debug debug2 debug3
);
Note: specify parameter: app => 'myappname' to allow multiple libraries to share one connection pool.
Only the library loaded first needs to specify conf and the other parameters. Subsequent libraries can then specify only app => 'myappname'
When you "use DBR" with arguments, as above, the default behavior is to export the following methods into your class
=head2 dbr_connect( $schema [, $class] );
Connect to an instance of the specified schema
my $music = dbr_connect('music');
Optionally accepts a $class argument, to specify which instance. Defaults to "master"
Returns a L<DBR::Handle> object representing your connection handle
=head2 dbr_instance( $schema [, $class] );
Similar to dbr_connect, but returns a L<DBR::Config::Instance> object instead of a L<DBR::Handle> object.
my $instance = dbr_connect('music');
An instance object represents the instance of the database schema in question, without necessarily being connected to it.
=head1 METHODS
=head2 new
Constructor. Useful in situations where you do not wish to export dbr_connect and dbr_instance into your class ( described above )
my $logger = new DBR::Util::Logger( -logpath => 'dbr.log' );
my $dbr = new DBR(
-logger => $logger,
-conf => '/path/to/my/DBR.conf'
);
my $handle = $dbr->connect( 'music' );
=head3 arguments
=over 1
=item -logger
L<DBR::Util::Logger> object ( required )
=item -conf
path to the DBR.conf you wish to use ( required )
=item -use_exceptions
Boolean. Causes all DBR errors to raise an exception, rather than logging an returning false ( default )
=item -admin
Boolean. Enables configuration objects to write changes to metadata DB ( don't use )
=item -fudge_tz
Boolean. Prevents DBR from aborting in the event that it cannot determine the system timezone.
=back
Returns a L<DBR> object.
=head2 connect
Same arguments as dbr_connect above
=head2 get_instance
Same arguments as dbr_instance above
=head2 flush_handles
Disconnects all active database connections. Useful if you need to fork your process
=cut