2 class SQL::Translator {
4 use MooseX::Types::Moose qw(Bool HashRef Int Str Undef);
6 use SQL::Translator::Types qw(DBIHandle Parser Producer Schema);
7 use SQL::Translator::Object::Schema;
9 our $VERSION = '0.001';
10 $VERSION = eval $VERSION;
28 handles => [ qw(parse) ],
29 predicate => 'has_parser',
36 handles => [ qw(produce) ],
37 predicate => 'has_producer',
43 predicate => 'has_dbh',
50 clearer => '_clear_schema',
51 default => sub { SQL::Translator::Object::Schema->new },
54 has 'parser_args' => (
57 predicate => 'has_parser_args',
60 has 'producer_args' => (
63 predicate => 'has_producer_args',
66 has 'version' => (isa => Str, is => 'ro', default => $VERSION);
67 has 'add_drop_table' => (isa => Bool, is => 'rw', default => 0);
68 has 'no_comments' => (isa => Bool, is => 'rw', default => 0);
69 has 'show_warnings' => (isa => Bool, is => 'rw', default => 0);
70 has 'trace' => (isa => Bool, is => 'rw', default => 0);
71 has 'quote_table_names' => (isa => Bool, is => 'rw', default => 0);
72 has 'quote_field_names' => (isa => Bool, is => 'rw', default => 0);
73 has 'filename' => (isa => Str, is => 'rw');
75 has '_producer_mapping' => (
78 default => sub { { MySQL => 'SQL::MySQL', SQLite => 'SQL::SQLite', PostgreSQL => 'SQL::PostgreSQL', XML => 'XML', YAML => 'YAML' } }
81 method _build__parser {
82 my $class = 'SQL::Translator::Parser';
84 Class::MOP::load_class($class);
88 $parser = $class->new({ translator => $self, dbh => $self->dbh });
90 $parser = $class->new({ translator => $self, type => $self->parser || '' });
96 method _build__producer {
97 my $mapping = $self->_producer_mapping;
99 my $class = 'SQL::Translator::Producer';
100 my $role = $class . '::' . $mapping->{$self->producer};
102 Class::MOP::load_class($class);
103 Class::MOP::load_class($role);
105 my $producer = $class->new({ translator => $self });
106 $role->meta->apply($producer);
111 method translate(:$data, :$producer?, :$producer_args?, :$parser?, :$parser_args?) {
114 $self->_clear_schema if defined $parser;
116 $parser ||= $self->parser unless $self->has_parser;
117 if (defined $parser) {
118 $self->_clear_parser;
119 $self->parser($parser);
121 $return = $self->schema;
124 $producer ||= $self->producer unless $self->has_producer;
125 if (defined $producer) {
126 $self->_clear_producer;
127 $self->producer($producer);
128 $return = $self->produce;
134 method parser_type { return $self->parser }
135 method producer_type { return $self->producer }
137 method engine_version(Int|Str|Undef $v, Str $target = 'perl') {
138 return undef unless $v;
143 if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) {
144 push @vers, $1, $2, $3;
147 # XYYZZ (mysql) style
148 elsif ( $v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x ) {
149 push @vers, $1, $2, $3;
152 # XX.YYYZZZ (perl) style or simply X
153 elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) {
154 push @vers, $1, $2, $3;
157 #how do I croak sanely here?
158 die "Unparseable MySQL version '$v'";
161 if ($target eq 'perl') {
162 return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) );
164 elsif ($target eq 'mysql') {
165 return sprintf ('%d%02d%02d', map { $_ || 0 } (@vers) );
168 #how do I croak sanely here?
169 die "Unknown version target '$target'";