From: Justin Hunter Date: Fri, 3 Jul 2009 21:57:56 +0000 (-0700) Subject: first try at producing X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a832857f4625ade7ff2683fc4d0a6c8a416b11c0;p=dbsrgits%2FSQL-Translator-2.0-ish.git first try at producing --- diff --git a/lib/SQL/Translator.pm b/lib/SQL/Translator.pm index 78a0041..78512bc 100644 --- a/lib/SQL/Translator.pm +++ b/lib/SQL/Translator.pm @@ -1,3 +1,56 @@ package SQL::Translator; +use namespace::autoclean; +use Moose; +use MooseX::Types::Moose qw(Str); +use SQL::Translator::Types qw(DBIHandle); + +has 'parser' => ( + isa => Str, + is => 'ro', + init_arg => 'from', + required => 1, +); + +has 'producer' => ( + isa => Str, + is => 'ro', + init_arg => 'to', + required => 1, +); + +has 'dbh' => ( + isa => DBIHandle, + is => 'ro', + predicate => 'has_dbh', +); + +has 'filename' => ( + isa => Str, + is => 'ro', + predicate => 'has_ddl', +); + +sub BUILD {} + +after BUILD => sub { + my $self = shift; + + my $parser_class = 'SQL::Translator::Parser'; + my $producer_class = 'SQL::Translator::Producer'; + my $producer_role = $producer_class . '::' . $self->producer; + + Class::MOP::load_class($parser_class); + + my $parser = $parser_class->new({ dbh => $self->dbh }); + + Class::MOP::load_class($producer_class); + Class::MOP::load_class($producer_role); + + my $producer = $producer_class->new({ schema => $parser->parse }); + $producer_role->meta->apply($producer); + $producer->produce; +}; + +__PACKAGE__->meta->make_immutable; 1; diff --git a/lib/SQL/Translator/Producer.pm b/lib/SQL/Translator/Producer.pm index a8f4283..64035f3 100644 --- a/lib/SQL/Translator/Producer.pm +++ b/lib/SQL/Translator/Producer.pm @@ -1,5 +1,53 @@ package SQL::Translator::Producer; +use namespace::autoclean; use Moose; -extends 'SQL::Translator'; +use MooseX::Types::Moose qw(Str); +use SQL::Translator::Types qw(Schema); + +use Data::Dumper; + +has 'schema' => ( + isa => Schema, + is => 'rw', + required => 1 +); + +sub produce { + my $self = shift; + my $schema = $self->schema; + + my $tables = $schema->tables; + foreach my $tname (keys %$tables) { + $self->_create_table($tables->{$tname}); + } +} + +sub _create_table { + my $self = shift; + my $table = shift; + + my $no_comments = 0; + my $add_drop_table = 1; + my $sqlite_version = 0; + + my $create_table; + + $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $add_drop_table; + $create_table .= "CREATE TABLE " . $table->name . " (\n"; + + my $columns = $table->columns; + foreach my $cname (keys %$columns) { + my $column = $columns->{$cname}; + $create_table .= ' ' . $column->name . ' ' . $column->data_type; + $create_table .= '(' . $column->size . ')' if $column->size; + $create_table .= ' NOT NULL' unless $column->is_nullable; + $create_table .= ",\n"; + } + $create_table =~ s/,$//; + $create_table .= ");"; + print $create_table . "\n"; +} + +__PACKAGE__->meta->make_immutable; 1;