2 class SQL::Translator {
4 use MooseX::Types::Moose qw(Bool HashRef Int Str Undef);
5 use SQL::Translator::Types qw(DBIHandle Parser Producer Schema);
6 use SQL::Translator::Object::Schema;
24 handles => [ qw(parse) ],
31 handles => [ qw(produce) ],
37 predicate => 'has_dbh',
44 default => sub { SQL::Translator::Object::Schema->new },
47 has 'parser_args' => (
50 predicate => 'has_parser_args',
53 has 'producer_args' => (
56 predicate => 'has_producer_args',
59 has 'add_drop_table' => (isa => Bool, is => 'rw', default => 0);
60 has 'no_comments' => (isa => Bool, is => 'rw', default => 0);
61 has 'show_warnings' => (isa => Bool, is => 'rw', default => 1);
62 has 'trace' => (isa => Bool, is => 'rw', default => 0);
63 has 'quote_table_names' => (isa => Bool, is => 'rw', default => 0);
64 has 'quote_field_names' => (isa => Bool, is => 'rw', default => 0);
65 has 'version' => (isa => Str, is => 'rw');
66 has 'filename' => (isa => Str, is => 'rw');
68 method _build__parser {
69 my $class = 'SQL::Translator::Parser';
71 Class::MOP::load_class($class);
75 $parser = $class->new({ translator => $self, dbh => $self->dbh });
77 $parser = $class->new({ translator => $self, type => $self->parser || '' });
83 method _build__producer {
84 my $class = 'SQL::Translator::Producer';
85 my $role = $class . '::' . $self->producer;
87 Class::MOP::load_class($class);
89 Class::MOP::load_class($role)
91 $role = $class . '::SQL::' . $self->producer;
92 Class::MOP::load_class($role)
95 my $producer = $class->new({ translator => $self });
96 $role->meta->apply($producer);
101 method translate(:$data, :$producer?, :$producer_args?, :$parser?, :$parser_args?) {
104 $parser ||= $self->parser;
105 if (defined $parser) {
106 $self->_clear_parser;
107 $self->parser($parser);
109 $return = $self->schema;
112 $producer ||= $self->producer;
113 if (defined $producer) {
114 $self->_clear_producer;
115 $self->producer($producer);
116 $return = $self->produce;
122 method parser_type { return $self->parser }
123 method producer_type { return $self->producer }
125 method engine_version(Int|Str|Undef $v, Str $target = 'perl') {
126 return undef unless $v;
131 if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) {
132 push @vers, $1, $2, $3;
135 # XYYZZ (mysql) style
136 elsif ( $v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x ) {
137 push @vers, $1, $2, $3;
140 # XX.YYYZZZ (perl) style or simply X
141 elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) {
142 push @vers, $1, $2, $3;
145 #how do I croak sanely here?
146 die "Unparseable MySQL version '$v'";
149 if ($target eq 'perl') {
150 return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) );
152 elsif ($target eq 'mysql') {
153 return sprintf ('%d%02d%02d', map { $_ || 0 } (@vers) );
156 #how do I croak sanely here?
157 die "Unknown version target '$target'";