class SQL::Translator {
use TryCatch;
use MooseX::Types::Moose qw(Bool HashRef Int Str Undef);
+ use MooseX::Aliases;
use SQL::Translator::Types qw(DBIHandle Parser Producer Schema);
use SQL::Translator::Object::Schema;
+ our $VERSION = '0.001';
+
has 'parser' => (
isa => Str,
is => 'rw',
- init_arg => 'from',
+ alias => 'from',
);
has 'producer' => (
isa => Str,
is => 'rw',
- init_arg => 'to',
+ alias => 'to',
);
has '_parser' => (
is => 'rw',
lazy_build => 1,
handles => [ qw(parse) ],
+ predicate => 'has_parser',
);
has '_producer' => (
is => 'rw',
lazy_build => 1,
handles => [ qw(produce) ],
+ predicate => 'has_producer',
);
has 'dbh' => (
isa => Schema,
is => 'rw',
lazy => 1,
+ clearer => '_clear_schema',
default => sub { SQL::Translator::Object::Schema->new },
);
has 'version' => (isa => Str, is => 'rw');
has 'filename' => (isa => Str, is => 'rw');
+ has '_producer_mapping' => (
+ isa => HashRef,
+ is => 'ro',
+ default => sub { { MySQL => 'SQL::MySQL', SQLite => 'SQL::SQLite', PostgreSQL => 'SQL::PostgreSQL', XML => 'XML', YAML => 'YAML' } }
+ );
+
method _build__parser {
my $class = 'SQL::Translator::Parser';
}
method _build__producer {
+ my $mapping = $self->_producer_mapping;
+
my $class = 'SQL::Translator::Producer';
- my $role = $class . '::' . $self->producer;
+ my $role = $class . '::' . $mapping->{$self->producer};
Class::MOP::load_class($class);
- try {
- Class::MOP::load_class($role)
- } catch ($e) {
- $role = $class . '::SQL::' . $self->producer;
- Class::MOP::load_class($role)
- }
+ Class::MOP::load_class($role);
my $producer = $class->new({ translator => $self });
$role->meta->apply($producer);
method translate(:$data, :$producer?, :$producer_args?, :$parser?, :$parser_args?) {
my $return;
- $parser ||= $self->parser;
+ $self->_clear_schema if defined $parser;
+
+ $parser ||= $self->parser unless $self->has_parser;
if (defined $parser) {
$self->_clear_parser;
$self->parser($parser);
$return = $self->schema;
}
- $producer ||= $self->producer;
+ $producer ||= $self->producer unless $self->has_producer;
if (defined $producer) {
$self->_clear_producer;
$self->producer($producer);