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
|
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::ObjectDriverFactory;
use strict;
use base qw( MT::ErrorHandler );
use MT::ObjectDriver::Driver::DBI;
use MT::ObjectDriver::SQL;
# Mapping of aliases/identifiers to a particular implementation.
our $drivers = [
[ qr/(db[id]::)?(postgres|pg(sql)?)/ => 'Pg' ],
[ qr/(db[id]::)?mysql/ => 'mysql' ],
[ qr/(db[id]::)?sqlite/ => 'SQLite' ],
];
our @drivers;
sub new {
my $pkg = shift;
my $get_driver = $pkg->driver_for_class();
return $get_driver->();
}
our $DRIVER;
sub instance {
my $pkg = shift;
$DRIVER = $pkg->new unless $DRIVER;
return $DRIVER;
}
# Returns a coderef for an object driver that is suitable
# for the $class given. $class is optional
sub driver_for_class {
my $pkg = shift;
my ($class) = @_;
require MT::ObjectDriver::Driver::CacheWrapper;
my $driver_code = MT::ObjectDriver::Driver::CacheWrapper->wrap(
sub {
my $cfg = MT->config;
my $Password = $cfg->DBPassword;
my $Username = $cfg->DBUser;
my $dbd = $pkg->dbd_class;
my $driver = MT::ObjectDriver::Driver::DBI->new(
dbd => $dbd,
dsn => $dbd->dsn_from_config($cfg),
reuse_dbh => 1,
( $Username ? ( username => $Username ) : () ),
( $Password ? ( password => $Password ) : () ),
);
push @drivers, $driver;
return $driver;
},
$class
);
return $driver_code;
}
our $dbd_class;
sub dbd_class {
return $dbd_class if defined $dbd_class;
my $pkg = shift;
my ($type) = @_;
$type ||= MT->config('ObjectDriver');
my $dbd_class;
foreach my $driver (@$drivers) {
if ( ( lc $type ) =~ m/^$driver->[0]$/ ) {
$dbd_class = $driver->[1];
last;
}
}
unless ($dbd_class) {
my $all_drivers = MT->registry("object_drivers");
foreach my $driver (%$all_drivers) {
if ( my $re = $all_drivers->{$driver}{match} ) {
if ( ( lc $type ) =~ m/^$re$/ ) {
$dbd_class = $all_drivers->{$driver}{config_package};
last;
}
}
}
}
$dbd_class ||= $type;
die "Unsupported driver $type" unless $dbd_class;
$dbd_class = 'MT::ObjectDriver::Driver::DBD::' . $dbd_class
unless $dbd_class =~ m/::/;
eval "use $dbd_class;";
die "Unsupported driver $type: $@" if $@;
return $dbd_class;
}
sub configure {
my $pkg = shift;
$_->configure(@_) for @drivers;
}
sub cleanup {
if ( my $driver = $MT::Object::DRIVER ) {
if ( my $dbh = $driver->dbh ) {
$dbh->disconnect;
$driver->dbh->{private_set_names} = undef;
$driver->dbh(undef);
}
$MT::Object::DRIVER = undef;
$MT::Object::DBI_DRIVER = undef;
}
foreach my $driver (@drivers) {
if ( my $dbh = $driver->dbh ) {
$dbh->disconnect;
$driver->dbh->{private_set_names} = undef;
$driver->dbh(undef);
}
}
@drivers = ();
undef $DRIVER;
}
1;
__END__
=head1 NAME
MT::ObjectDriverFactory
=head1 AUTHOR & COPYRIGHT
Please see L<MT/AUTHOR & COPYRIGHT>.
=cut
|