add 'extra' to produce result
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator.pm
1 use MooseX::Declare;
2 class SQL::Translator {
3     use TryCatch;
4     use MooseX::Types::Moose qw(Bool HashRef Str);
5     use SQL::Translator::Types qw(DBIHandle Parser Producer Schema);
6     use SQL::Translator::Object::Schema;
7
8     has 'parser' => (
9         isa => Str,
10         is => 'rw',
11         init_arg => 'from',
12     );
13     
14     has 'producer' => (
15         isa => Str,
16         is => 'rw',
17         init_arg => 'to',
18     );
19     
20     has '_parser' => (
21         isa => Parser,
22         is => 'rw',
23         lazy_build => 1,
24         handles => [ qw(parse) ],
25     );
26     
27     has '_producer' => (
28         isa => Producer,
29         is => 'rw',
30         lazy_build => 1,
31         handles => [ qw(produce) ],
32     );
33     
34     has 'dbh' => (
35         isa => DBIHandle,
36         is => 'ro',
37         predicate => 'has_dbh',
38     );
39
40     has 'schema' => (
41         isa => Schema,
42         is => 'rw',
43         lazy => 1,
44         default => sub { SQL::Translator::Object::Schema->new },
45     );
46
47     has 'parser_args' => (
48         isa => HashRef,
49         is => 'rw',
50     );
51
52     has 'producer_args' => (
53         isa => HashRef,
54         is => 'rw',
55     );
56     
57     has 'add_drop_table' => (isa => Bool, is => 'rw', default => 0);
58     has 'no_comments' => (isa => Bool, is => 'rw', default => 0);
59     has 'show_warnings' => (isa => Bool, is => 'rw', default => 1);
60     has 'trace' => (isa => Bool, is => 'rw', default => 0);
61     has 'quote_table_names' => (isa => Bool, is => 'rw', default => 0);
62     has 'quote_field_names' => (isa => Bool, is => 'rw', default => 0);
63     has 'version' => (isa => Str, is => 'rw');
64     has 'filename' => (isa => Str, is => 'rw');
65
66     method _build__parser {
67         my $class = 'SQL::Translator::Parser';
68     
69         Class::MOP::load_class($class);
70     
71         my $parser;
72         if ($self->has_dbh) {
73             $parser = $class->new({ translator => $self, dbh => $self->dbh });
74         } else {
75             $parser = $class->new({ translator => $self, type => $self->parser || '' });
76         }
77     
78         return $parser;
79     }
80     
81     method _build__producer {
82         my $class = 'SQL::Translator::Producer';
83         my $role = $class . '::' . $self->producer;
84
85         Class::MOP::load_class($class);
86         try {
87             Class::MOP::load_class($role)
88         } catch ($e) {
89             $role = $class . '::SQL::' . $self->producer;
90             Class::MOP::load_class($role)
91         }
92     
93         my $producer = $class->new({ translator => $self });
94         $role->meta->apply($producer);
95     
96         return $producer;
97     }
98
99     method translate(:$data, :$producer?, :$producer_args?, :$parser?, :$parser_args?) {
100         if ($parser) {
101             $self->_clear_parser;
102             $self->parser($parser);
103             $self->parse($data);
104             $self->schema;
105         } elsif ($producer) {
106             $self->_clear_producer;
107             $self->parse($data) if $data;
108             $self->producer($producer);
109             $self->produce;
110         }
111     }
112
113     method parser_type { return $self->parser }
114     method producer_type { return $self->producer }
115