add quote_field_names/quote_table_names
[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         default => sub { SQL::Translator::Object::Schema->new }
44     );
45
46     has 'parser_args' => (
47         isa => HashRef,
48         is => 'rw',
49     );
50
51     has 'producer_args' => (
52         isa => HashRef,
53         is => 'rw',
54     );
55     
56     has 'add_drop_table' => (isa => Bool, is => 'rw', default => 0);
57     has 'no_comments' => (isa => Bool, is => 'rw', default => 0);
58     has 'show_warnings' => (isa => Bool, is => 'rw', default => 1);
59     has 'trace' => (isa => Bool, is => 'rw', default => 0);
60     has 'quote_table_names' => (isa => Bool, is => 'rw', default => 0);
61     has 'quote_field_names' => (isa => Bool, is => 'rw', default => 0);
62     has 'version' => (isa => Str, is => 'rw');
63     has 'filename' => (isa => Str, is => 'rw');
64
65     method _build__parser {
66         my $class = 'SQL::Translator::Parser';
67     
68         Class::MOP::load_class($class);
69     
70         my $parser;
71         if ($self->has_dbh) {
72             $parser = $class->new({ translator => $self, dbh => $self->dbh });
73         } else {
74             $parser = $class->new({ translator => $self, type => $self->parser || '' });
75         }
76     
77         return $parser;
78     }
79     
80     method _build__producer {
81         my $class = 'SQL::Translator::Producer';
82         my $role = $class . '::' . $self->producer;
83     
84         Class::MOP::load_class($class);
85         try { Class::MOP::load_class($role) } catch ($e) { warn "ERROR: $e"; $role = $class . '::SQL::' . $self->producer; Class::MOP::load_class($role) }
86     
87         my $producer = $class->new({ translator => $self });
88         $role->meta->apply($producer);
89     
90         return $producer;
91     }
92
93     method translate(:$data, :$producer?, :$producer_args?, :$parser?, :$parser_args?) {
94         if ($parser) {
95             $self->_clear_parser;
96             $self->parser($parser);
97             $self->schema($self->parse($data));
98         } elsif ($producer) {
99             $self->_clear_producer;
100             $self->schema($self->parse($data)) if $data;
101             $self->producer($producer);
102             return $self->produce;
103         }
104     }
105
106     method parser_type { return $self->parser }
107     method producer_type { return $self->producer }
108