9c848f8236db1ff569d753a85530018bab2afdf8
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator.pm
1 package SQL::Translator;
2 use namespace::autoclean;
3 use Moose;
4 use MooseX::Types::Moose qw(Str);
5 use SQL::Translator::Types qw(DBIHandle Parser Producer);
6
7 has 'parser' => (
8     isa => Str,
9     is => 'ro',
10     init_arg => 'from',
11     required => 1,
12 );
13
14 has 'producer' => (
15     isa => Str,
16     is => 'ro',
17     init_arg => 'to',
18     required => 1,
19 );
20
21 has '_parser' => (
22     isa => Parser,
23     is => 'rw',
24     lazy_build => 1,
25     handles => [ qw(parse) ],
26 );
27
28 has '_producer' => (
29     isa => Producer,
30     is => 'rw',
31     lazy_build => 1,
32     handles => [ qw(produce) ],
33 );
34
35 has 'dbh' => (
36     isa => DBIHandle,
37     is => 'ro',
38     predicate => 'has_dbh',
39 );
40
41 has 'filename' => (
42     isa => Str,
43     is => 'ro',
44     predicate => 'has_ddl',
45 );
46
47 sub _build__parser {
48     my $self = shift;
49     my $class = 'SQL::Translator::Parser';
50
51     Class::MOP::load_class($class);
52
53     my $parser = $class->new({ dbh => $self->dbh });
54
55     return $parser;
56 }
57
58 sub _build__producer {
59     my $self = shift;
60     my $class = 'SQL::Translator::Producer';
61     my $role = $class . '::' . $self->producer;
62
63     Class::MOP::load_class($class);
64     eval { Class::MOP::load_class($role); };
65     if ($@) {
66         $role = $class . '::SQL::' . $self->producer;
67         eval { Class::MOP::load_class($role); };
68         die $@ if $@;
69     }
70
71     my $producer = $class->new({ schema => $self->parse });
72     $role->meta->apply($producer);
73
74     return $producer;
75 }
76
77 __PACKAGE__->meta->make_immutable;
78
79 1;