parse mysql ddl procedures & views
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator.pm
index bd07882..72541f9 100644 (file)
@@ -1,21 +1,20 @@
 use MooseX::Declare;
 class SQL::Translator {
-    use MooseX::Types::Moose qw(Str);
     use TryCatch;
-    use SQL::Translator::Types qw(DBIHandle Parser Producer);
-    
+    use MooseX::Types::Moose qw(Bool HashRef Str);
+    use SQL::Translator::Types qw(DBIHandle Parser Producer Schema);
+    use SQL::Translator::Object::Schema;
+
     has 'parser' => (
         isa => Str,
-        is => 'ro',
+        is => 'rw',
         init_arg => 'from',
-        required => 1,
     );
     
     has 'producer' => (
         isa => Str,
-        is => 'ro',
+        is => 'rw',
         init_arg => 'to',
-        required => 1,
     );
     
     has '_parser' => (
@@ -37,19 +36,44 @@ class SQL::Translator {
         is => 'ro',
         predicate => 'has_dbh',
     );
-    
-    has 'filename' => (
-        isa => Str,
-        is => 'ro',
-        predicate => 'has_ddl',
+
+    has 'schema' => (
+        isa => Schema,
+        is => 'rw',
+        lazy => 1,
+        default => sub { SQL::Translator::Object::Schema->new },
+    );
+
+    has 'parser_args' => (
+        isa => HashRef,
+        is => 'rw',
+    );
+
+    has 'producer_args' => (
+        isa => HashRef,
+        is => 'rw',
     );
     
+    has 'add_drop_table' => (isa => Bool, is => 'rw', default => 0);
+    has 'no_comments' => (isa => Bool, is => 'rw', default => 0);
+    has 'show_warnings' => (isa => Bool, is => 'rw', default => 1);
+    has 'trace' => (isa => Bool, is => 'rw', default => 0);
+    has 'quote_table_names' => (isa => Bool, is => 'rw', default => 0);
+    has 'quote_field_names' => (isa => Bool, is => 'rw', default => 0);
+    has 'version' => (isa => Str, is => 'rw');
+    has 'filename' => (isa => Str, is => 'rw');
+
     method _build__parser {
         my $class = 'SQL::Translator::Parser';
     
         Class::MOP::load_class($class);
     
-        my $parser = $class->new({ dbh => $self->dbh });
+        my $parser;
+        if ($self->has_dbh) {
+            $parser = $class->new({ translator => $self, dbh => $self->dbh });
+        } else {
+            $parser = $class->new({ translator => $self, type => $self->parser || '' });
+        }
     
         return $parser;
     }
@@ -57,14 +81,35 @@ class SQL::Translator {
     method _build__producer {
         my $class = 'SQL::Translator::Producer';
         my $role = $class . '::' . $self->producer;
-    
+
         Class::MOP::load_class($class);
-        try { Class::MOP::load_class($role) } catch ($e) { $role = $class . '::SQL::' . $self->producer; Class::MOP::load_class($role) }
+        try {
+            Class::MOP::load_class($role)
+        } catch ($e) {
+            $role = $class . '::SQL::' . $self->producer;
+            Class::MOP::load_class($role)
+        }
     
-        my $producer = $class->new({ schema => $self->parse });
+        my $producer = $class->new({ translator => $self });
         $role->meta->apply($producer);
     
         return $producer;
     }
-    
+
+    method translate(:$data, :$producer?, :$producer_args?, :$parser?, :$parser_args?) {
+        if ($parser) {
+            $self->_clear_parser;
+            $self->parser($parser);
+            $self->parse($data);
+            $self->schema;
+        } elsif ($producer) {
+            $self->_clear_producer;
+            $self->parse($data) if $data;
+            $self->producer($producer);
+            $self->produce;
+        }
+    }
+
+    method parser_type { return $self->parser }
+    method producer_type { return $self->producer }
 }