| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | FusqlFS::Backend - FusqlFS database backend abstract factory | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use FusqlFS::Backend; | ||
| 15 | |||
| 16 | my $fs = FusqlFS::Backend->new(engine => 'PgSQL', database => 'dbname', user => 'postgres', password => 'pa$$w0rd'); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This class is a factory for the family of L<FusqlFS::Backend::Base> subclasses. | ||
| 21 | Its C<new()> method initializes and returns some kind of real backend class | ||
| 22 | instance (L<FusqlFS::Backend::PgSQL> or L<FusqlFS::Backend::MySQL> for now) | ||
| 23 | depending on `engine' option passed to it. If engine is not given or not | ||
| 24 | recognized, it falls back to `PgSQL' backend with a debug message. | ||
| 25 | |||
| 26 | You better look at one of underlying classes for detailed description. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use Carp; | ||
| 31 | |||
| 32 | our %Engines = ( | ||
| 33 | pgsql => '::PgSQL', | ||
| 34 | mysql => '::MySQL', | ||
| 35 | sqlite => '::SQLite', | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub new | ||
| 39 | { | ||
| 40 | my $class = shift; | ||
| 41 | my %options = @_; | ||
| 42 | my $engine = $options{engine}; | ||
| 43 | |||
| 44 | my $subclass = $Engines{lc $engine} || ''; | ||
| 45 | if (!$subclass) | ||
| 46 | { | ||
| 47 | carp "Unknown engine `$engine', falling back to default `PgSQL' engine"; | ||
| 48 | $subclass = '::PgSQL'; | ||
| 49 | } | ||
| 50 | |||
| 51 | $class .= $subclass; | ||
| 52 | eval "require $class"; | ||
| 53 | $class->new(@_); | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; |