From: Justin Hunter Date: Thu, 23 Jul 2009 07:16:10 +0000 (-0700) Subject: move to MooseX::Declare X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4f4fd192801c55deb75aa8e4bc440b220e24c396;p=dbsrgits%2FSQL-Translator-2.0-ish.git move to MooseX::Declare --- diff --git a/lib/SQL/Translator.pm b/lib/SQL/Translator.pm index 9c848f8..bdca03a 100644 --- a/lib/SQL/Translator.pm +++ b/lib/SQL/Translator.pm @@ -1,79 +1,74 @@ -package SQL::Translator; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(Str); -use SQL::Translator::Types qw(DBIHandle Parser Producer); - -has 'parser' => ( - isa => Str, - is => 'ro', - init_arg => 'from', - required => 1, -); - -has 'producer' => ( - isa => Str, - is => 'ro', - init_arg => 'to', - required => 1, -); - -has '_parser' => ( - isa => Parser, - is => 'rw', - lazy_build => 1, - handles => [ qw(parse) ], -); - -has '_producer' => ( - isa => Producer, - is => 'rw', - lazy_build => 1, - handles => [ qw(produce) ], -); - -has 'dbh' => ( - isa => DBIHandle, - is => 'ro', - predicate => 'has_dbh', -); - -has 'filename' => ( - isa => Str, - is => 'ro', - predicate => 'has_ddl', -); - -sub _build__parser { - my $self = shift; - my $class = 'SQL::Translator::Parser'; - - Class::MOP::load_class($class); - - my $parser = $class->new({ dbh => $self->dbh }); - - return $parser; -} - -sub _build__producer { - my $self = shift; - my $class = 'SQL::Translator::Producer'; - my $role = $class . '::' . $self->producer; - - Class::MOP::load_class($class); - eval { Class::MOP::load_class($role); }; - if ($@) { - $role = $class . '::SQL::' . $self->producer; +use MooseX::Declare; +class SQL::Translator { + use MooseX::Types::Moose qw(Str); + use SQL::Translator::Types qw(DBIHandle Parser Producer); + + has 'parser' => ( + isa => Str, + is => 'ro', + init_arg => 'from', + required => 1, + ); + + has 'producer' => ( + isa => Str, + is => 'ro', + init_arg => 'to', + required => 1, + ); + + has '_parser' => ( + isa => Parser, + is => 'rw', + lazy_build => 1, + handles => [ qw(parse) ], + ); + + has '_producer' => ( + isa => Producer, + is => 'rw', + lazy_build => 1, + handles => [ qw(produce) ], + ); + + has 'dbh' => ( + isa => DBIHandle, + is => 'ro', + predicate => 'has_dbh', + ); + + has 'filename' => ( + isa => Str, + is => 'ro', + predicate => 'has_ddl', + ); + + method _build__parser { + my $class = 'SQL::Translator::Parser'; + + Class::MOP::load_class($class); + + my $parser = $class->new({ dbh => $self->dbh }); + + return $parser; + } + + method _build__producer { + my $class = 'SQL::Translator::Producer'; + my $role = $class . '::' . $self->producer; + + Class::MOP::load_class($class); eval { Class::MOP::load_class($role); }; - die $@ if $@; + if ($@) { + $role = $class . '::SQL::' . $self->producer; + eval { Class::MOP::load_class($role); }; + die $@ if $@; + } + + my $producer = $class->new({ schema => $self->parse }); + $role->meta->apply($producer); + + return $producer; } - - my $producer = $class->new({ schema => $self->parse }); - $role->meta->apply($producer); - - return $producer; -} - -__PACKAGE__->meta->make_immutable; - -1; + +} diff --git a/lib/SQL/Translator/Object.pm b/lib/SQL/Translator/Object.pm index 0a7e78d..15302b5 100644 --- a/lib/SQL/Translator/Object.pm +++ b/lib/SQL/Translator/Object.pm @@ -1,6 +1,4 @@ -package SQL::Translator::Object; -use namespace::autoclean; -use Moose; -use Tie::IxHash; - -1; +use MooseX::Declare; +class SQL::Translator::Object { + use Tie::IxHash; +} diff --git a/lib/SQL/Translator/Object/Column.pm b/lib/SQL/Translator/Object/Column.pm index 16e0a86..3594d87 100644 --- a/lib/SQL/Translator/Object/Column.pm +++ b/lib/SQL/Translator/Object/Column.pm @@ -1,57 +1,53 @@ -package SQL::Translator::Object::Column; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(Bool Int Maybe Str); -use SQL::Translator::Types qw(Trigger); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'data_type' => ( - is => 'rw', - isa => Int, - required => 1 -); - -has 'size' => ( - is => 'rw', - isa => Maybe[Int], - required => 1 -); - -has 'is_nullable' => ( - is => 'rw', - isa => Bool, - required => 1, - default => 1 -); - -has 'is_auto_increment' => ( - is => 'rw', - isa => Bool, - required => 1, - default => 0 -); - -has 'default_value' => ( - is => 'rw', - isa => Maybe[Str], -); - -has 'remarks' => ( - is => 'rw', - isa => Maybe[Str], -); - -has 'trigger' => ( - is => 'rw', - isa => Trigger, -); - -__PACKAGE__->meta->make_immutable; - -1; +use MooseX::Declare; +class SQL::Translator::Object::Column { + use MooseX::Types::Moose qw(Bool Int Maybe Str); + use SQL::Translator::Types qw(Trigger); + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'data_type' => ( + is => 'rw', + isa => Int, + required => 1 + ); + + has 'size' => ( + is => 'rw', + isa => Maybe[Int], + required => 1 + ); + + has 'is_nullable' => ( + is => 'rw', + isa => Bool, + required => 1, + default => 1 + ); + + has 'is_auto_increment' => ( + is => 'rw', + isa => Bool, + required => 1, + default => 0 + ); + + has 'default_value' => ( + is => 'rw', + isa => Maybe[Str], + ); + + has 'remarks' => ( + is => 'rw', + isa => Maybe[Str], + ); + + has 'trigger' => ( + is => 'rw', + isa => Trigger, + ); +} diff --git a/lib/SQL/Translator/Object/Constraint.pm b/lib/SQL/Translator/Object/Constraint.pm index 992d414..ef673ea 100644 --- a/lib/SQL/Translator/Object/Constraint.pm +++ b/lib/SQL/Translator/Object/Constraint.pm @@ -1,43 +1,39 @@ -package SQL::Translator::Object::Constraint; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(HashRef Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(Column); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'columns' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Column], - provides => { - exists => 'exists_column', - keys => 'column_ids', - get => 'get_column', - }, - curries => { - set => { - add_column => sub { - my ($self, $body, $column) = @_; - $self->$body($column->name, $column); +use MooseX::Declare; +class SQL::Translator::Object::Constraint { + use MooseX::Types::Moose qw(HashRef Str); + use MooseX::AttributeHelpers; + use SQL::Translator::Types qw(Column); + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'columns' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Column], + provides => { + exists => 'exists_column', + keys => 'column_ids', + get => 'get_column', + }, + curries => { + set => { + add_column => sub { + my ($self, $body, $column) = @_; + $self->$body($column->name, $column); + } } - } - }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, -); - -has 'type' => ( - is => 'rw', - isa => Str, - required => 1 -); - -__PACKAGE__->meta->make_immutable; - -1; + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); + + has 'type' => ( + is => 'rw', + isa => Str, + required => 1 + ); +} diff --git a/lib/SQL/Translator/Object/ForeignKey.pm b/lib/SQL/Translator/Object/ForeignKey.pm index 6f0f19f..e3ed307 100644 --- a/lib/SQL/Translator/Object/ForeignKey.pm +++ b/lib/SQL/Translator/Object/ForeignKey.pm @@ -1,16 +1,15 @@ -package SQL::Translator::Object::ForeignKey; -use Moose; -use SQL::Translator::Types qw(Index PrimaryKey); -extends 'SQL::Translator::Object::Constraint'; - -has '+type' => ( - default => 'FOREIGN_KEY', -); - -has 'references' => ( - isa => PrimaryKey | Index, - is => 'rw', - required => 1, -); - -1; +use MooseX::Declare; +class SQL::Translator::Object::ForeignKey { + use SQL::Translator::Types qw(Index PrimaryKey); + extends 'SQL::Translator::Object::Constraint'; + + has '+type' => ( + default => 'FOREIGN_KEY', + ); + + has 'references' => ( + isa => PrimaryKey | Index, + is => 'rw', + required => 1, + ); +} diff --git a/lib/SQL/Translator/Object/Index.pm b/lib/SQL/Translator/Object/Index.pm index 64e540a..dd78b20 100644 --- a/lib/SQL/Translator/Object/Index.pm +++ b/lib/SQL/Translator/Object/Index.pm @@ -1,43 +1,39 @@ -package SQL::Translator::Object::Index; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(HashRef Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(Column); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'columns' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Column], - provides => { - exists => 'exists_column', - keys => 'column_ids', - get => 'get_column', - }, - curries => { - set => { - add_column => sub { - my ($self, $body, $column) = @_; - $self->$body($column->name, $column); +use MooseX::Declare; +class SQL::Translator::Object::Index { + use MooseX::Types::Moose qw(HashRef Str); + use MooseX::AttributeHelpers; + use SQL::Translator::Types qw(Column); + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'columns' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Column], + provides => { + exists => 'exists_column', + keys => 'column_ids', + get => 'get_column', + }, + curries => { + set => { + add_column => sub { + my ($self, $body, $column) = @_; + $self->$body($column->name, $column); + } } - } - }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, -); - -has 'type' => ( - is => 'rw', - isa => Str, - required => 1 -); - -__PACKAGE__->meta->make_immutable; - -1; + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); + + has 'type' => ( + is => 'rw', + isa => Str, + required => 1 + ); +} diff --git a/lib/SQL/Translator/Object/PrimaryKey.pm b/lib/SQL/Translator/Object/PrimaryKey.pm index 8a1b16c..490786c 100644 --- a/lib/SQL/Translator/Object/PrimaryKey.pm +++ b/lib/SQL/Translator/Object/PrimaryKey.pm @@ -1,9 +1,8 @@ -package SQL::Translator::Object::PrimaryKey; -use Moose; -extends 'SQL::Translator::Object::Index'; +use MooseX::Declare; +class SQL::Translator::Object::PrimaryKey { + extends 'SQL::Translator::Object::Index'; -has '+type' => ( - default => 'PRIMARY_KEY', -); - -1; + has '+type' => ( + default => 'PRIMARY_KEY', + ); +} diff --git a/lib/SQL/Translator/Object/Procedure.pm b/lib/SQL/Translator/Object/Procedure.pm index fd6fed9..f53df08 100644 --- a/lib/SQL/Translator/Object/Procedure.pm +++ b/lib/SQL/Translator/Object/Procedure.pm @@ -1,54 +1,52 @@ -package SQL::Translator::Object::Procedure; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(HashRef Int Maybe Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(); -use aliased 'SQL::Translator::Object::Schema'; -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'contents' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'parameters' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => Maybe[HashRef[Int|Str]], - provides => { - exists => 'exists_parameter', - keys => 'parameter_ids', - get => 'get_parameter', - set => 'set_parameter', - }, -); - -has 'owner' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'comments' => ( - is => 'rw', - isa => Str, -); - -has 'schema' => ( - is => 'rw', - isa => Schema, - required => 1, - default => sub { Schema->new } -); - -__PACKAGE__->meta->make_immutable; - -1; +use MooseX::Declare; +class SQL::Translator::Object::Procedure { + use namespace::autoclean; + use Moose; + use MooseX::Types::Moose qw(HashRef Int Maybe Str); + use MooseX::AttributeHelpers; + use SQL::Translator::Types qw(); + use aliased 'SQL::Translator::Object::Schema'; + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'contents' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'parameters' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => Maybe[HashRef[Int|Str]], + provides => { + exists => 'exists_parameter', + keys => 'parameter_ids', + get => 'get_parameter', + set => 'set_parameter', + }, + ); + + has 'owner' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'comments' => ( + is => 'rw', + isa => Str, + ); + + has 'schema' => ( + is => 'rw', + isa => Schema, + required => 1, + default => sub { Schema->new } + ); +} diff --git a/lib/SQL/Translator/Object/Schema.pm b/lib/SQL/Translator/Object/Schema.pm index 7498454..8b7084e 100644 --- a/lib/SQL/Translator/Object/Schema.pm +++ b/lib/SQL/Translator/Object/Schema.pm @@ -1,78 +1,74 @@ -package SQL::Translator::Object::Schema; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(HashRef Maybe Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(Procedure Table View); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Maybe[Str], - required => 1, - default => '' -); - -has 'tables' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Table], - provides => { - exists => 'exists_table', - keys => 'table_ids', - get => 'get_table', - }, - curries => { - set => { - add_table => sub { - my ($self, $body, $table) = @_; - $self->$body($table->name, $table); +use MooseX::Declare; +class SQL::Translator::Object::Schema { + use MooseX::Types::Moose qw(HashRef Maybe Str); + use MooseX::AttributeHelpers; + use SQL::Translator::Types qw(Procedure Table View); + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Maybe[Str], + required => 1, + default => '' + ); + + has 'tables' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Table], + provides => { + exists => 'exists_table', + keys => 'table_ids', + get => 'get_table', + }, + curries => { + set => { + add_table => sub { + my ($self, $body, $table) = @_; + $self->$body($table->name, $table); + } } - } - }, - default => sub { {} }, -); - -has 'views' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[View], - provides => { - exists => 'exists_view', - keys => 'view_ids', - get => 'get_view', - }, - curries => { - set => { - add_view => sub { - my ($self, $body, $view) = @_; - $self->$body($view->name, $view); + }, + default => sub { {} }, + ); + + has 'views' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[View], + provides => { + exists => 'exists_view', + keys => 'view_ids', + get => 'get_view', + }, + curries => { + set => { + add_view => sub { + my ($self, $body, $view) = @_; + $self->$body($view->name, $view); + } } - } - }, - default => sub { {} }, -); - -has 'procedures' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Procedure], - provides => { - exists => 'exists_procedure', - keys => 'procedure_ids', - get => 'get_procedure', - }, - curries => { - set => { - add_procedure => sub { - my ($self, $body, $procedure) = @_; - $self->$body($procedure->name, $procedure); + }, + default => sub { {} }, + ); + + has 'procedures' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Procedure], + provides => { + exists => 'exists_procedure', + keys => 'procedure_ids', + get => 'get_procedure', + }, + curries => { + set => { + add_procedure => sub { + my ($self, $body, $procedure) = @_; + $self->$body($procedure->name, $procedure); + } } - } - }, - default => sub { {} }, -); - -__PACKAGE__->meta->make_immutable; - -1; + }, + default => sub { {} }, + ); +} diff --git a/lib/SQL/Translator/Object/Sequence.pm b/lib/SQL/Translator/Object/Sequence.pm index f5ff257..1b0ea39 100644 --- a/lib/SQL/Translator/Object/Sequence.pm +++ b/lib/SQL/Translator/Object/Sequence.pm @@ -1,16 +1,12 @@ -package SQL::Translator::Object::Sequence; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(Str); -use SQL::Translator::Types qw(); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'ro', - isa => Str, - required => 1 -); - -__PACKAGE__->meta->make_immutable; - -1; +use MooseX::Declare; +class SQL::Translator::Object::Sequence { + use MooseX::Types::Moose qw(Str); + use SQL::Translator::Types qw(); + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'ro', + isa => Str, + required => 1 + ); +} diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index 6531486..e91731f 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -1,104 +1,100 @@ -package SQL::Translator::Object::Table; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(Bool HashRef Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); -use SQL::Translator::Object::Schema; -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'columns' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Column], - provides => { - exists => 'exists_column', - keys => 'column_ids', - get => 'get_column', - }, - curries => { - set => { - add_column => sub { - my ($self, $body, $column) = @_; - $self->$body($column->name, $column); +use MooseX::Declare; +class SQL::Translator::Object::Table { + use MooseX::Types::Moose qw(Bool HashRef Str); + use MooseX::AttributeHelpers; + use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); + use SQL::Translator::Object::Schema; + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'columns' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Column], + provides => { + exists => 'exists_column', + keys => 'column_ids', + get => 'get_column', + }, + curries => { + set => { + add_column => sub { + my ($self, $body, $column) = @_; + $self->$body($column->name, $column); + } } - } - }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, -); - -has 'indexes' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Index], - provides => { - exists => 'exists_index', - keys => 'index_ids', - get => 'get_index', - }, - curries => { - set => { - add_index => sub { - my ($self, $body, $index) = @_; - $self->$body($index->name, $index); + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); + + has 'indexes' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Index], + provides => { + exists => 'exists_index', + keys => 'index_ids', + get => 'get_index', + }, + curries => { + set => { + add_index => sub { + my ($self, $body, $index) = @_; + $self->$body($index->name, $index); + } } - } - }, - default => sub { {} }, -); - -has 'constraints' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Constraint], - provides => { - exists => 'exists_constraint', - keys => 'constraint_ids', - get => 'get_constraint', - }, - curries => { - set => { - add_constraint => sub { - my ($self, $body, $constraint) = @_; - $self->$body($constraint->name, $constraint); + }, + default => sub { {} }, + ); + + has 'constraints' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Constraint], + provides => { + exists => 'exists_constraint', + keys => 'constraint_ids', + get => 'get_constraint', + }, + curries => { + set => { + add_constraint => sub { + my ($self, $body, $constraint) = @_; + $self->$body($constraint->name, $constraint); + } } - } - }, - default => sub { {} }, -); - -has 'sequences' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Sequence], - provides => { - exists => 'exists_sequence', - keys => 'sequence_ids', - get => 'get_sequence', - }, - curries => { - set => { - add_sequence => sub { - my ($self, $body, $sequence) = @_; - $self->$body($sequence->name, $sequence); + }, + default => sub { {} }, + ); + + has 'sequences' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Sequence], + provides => { + exists => 'exists_sequence', + keys => 'sequence_ids', + get => 'get_sequence', + }, + curries => { + set => { + add_sequence => sub { + my ($self, $body, $sequence) = @_; + $self->$body($sequence->name, $sequence); + } } - } - }, - default => sub { {} }, -); - -has 'temporary' => ( - is => 'rw', - isa => Bool, - default => 0 -); - -__PACKAGE__->meta->make_immutable; - -1; + }, + default => sub { {} }, + ); + + has 'temporary' => ( + is => 'rw', + isa => Bool, + default => 0 + ); +} diff --git a/lib/SQL/Translator/Object/Trigger.pm b/lib/SQL/Translator/Object/Trigger.pm index 2b77ffa..a1d1de9 100644 --- a/lib/SQL/Translator/Object/Trigger.pm +++ b/lib/SQL/Translator/Object/Trigger.pm @@ -1,16 +1,12 @@ -package SQL::Translator::Object::Trigger; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(Str); -use SQL::Translator::Types qw(); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'ro', - isa => Str, - required => 1 -); - -__PACKAGE__->meta->make_immutable; - -1; +use MooseX::Declare; +class SQL::Translator::Object::Trigger { + use MooseX::Types::Moose qw(Str); + use SQL::Translator::Types qw(); + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'ro', + isa => Str, + required => 1 + ); +} diff --git a/lib/SQL/Translator/Object/View.pm b/lib/SQL/Translator/Object/View.pm index 987c728..625472f 100644 --- a/lib/SQL/Translator/Object/View.pm +++ b/lib/SQL/Translator/Object/View.pm @@ -1,43 +1,39 @@ -package SQL::Translator::Object::View; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(HashRef Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(Column); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'columns' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Column], - provides => { - exists => 'exists_column', - keys => 'column_ids', - get => 'get_column', - }, - curries => { - set => { - add_column => sub { - my ($self, $body, $column) = @_; - $self->$body($column->name, $column); +use MooseX::Declare; +class SQL::Translator::Object::View { + use MooseX::Types::Moose qw(HashRef Str); + use MooseX::AttributeHelpers; + use SQL::Translator::Types qw(Column); + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'columns' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Column], + provides => { + exists => 'exists_column', + keys => 'column_ids', + get => 'get_column', + }, + curries => { + set => { + add_column => sub { + my ($self, $body, $column) = @_; + $self->$body($column->name, $column); + } } - } - }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, -); - -has 'sql' => ( - is => 'rw', - isa => Str, - required => 1 -); - -__PACKAGE__->meta->make_immutable; - -1; + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); + + has 'sql' => ( + is => 'rw', + isa => Str, + required => 1 + ); +} diff --git a/lib/SQL/Translator/Parser.pm b/lib/SQL/Translator/Parser.pm index 7b59ced..1075a1f 100644 --- a/lib/SQL/Translator/Parser.pm +++ b/lib/SQL/Translator/Parser.pm @@ -1,41 +1,36 @@ -package SQL::Translator::Parser; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(Str); -use SQL::Translator::Types qw(DBIHandle); -use aliased 'SQL::Translator::Object::Schema'; +use MooseX::Declare; +class SQL::Translator::Parser { + use MooseX::Types::Moose qw(Str); + use SQL::Translator::Types qw(DBIHandle); + use aliased 'SQL::Translator::Object::Schema'; -my $apply_role_dbi = sub { - my $self = shift; - my $class = __PACKAGE__ . '::DBI'; - Class::MOP::load_class($class); - $class->meta->apply($self); - $self->_subclass(); -}; + my $apply_role_dbi = sub { + my $self = shift; + my $class = __PACKAGE__ . '::DBI'; + Class::MOP::load_class($class); + $class->meta->apply($self); + $self->_subclass(); + }; -my $apply_role_ddl = sub { }; + my $apply_role_ddl = sub { }; -has 'dbh' => ( - isa => DBIHandle, - is => 'ro', - predicate => 'has_dbh', - trigger => $apply_role_dbi, -); + has 'dbh' => ( + isa => DBIHandle, + is => 'ro', + predicate => 'has_dbh', + trigger => $apply_role_dbi, + ); -has 'filename' => ( - isa => Str, - is => 'ro', - predicate => 'has_ddl', - trigger => $apply_role_ddl, -); + has 'filename' => ( + isa => Str, + is => 'ro', + predicate => 'has_ddl', + trigger => $apply_role_ddl, + ); -sub parse { - my $self = shift; - my $schema = Schema->new({ name => $self->schema_name }); - $self->_add_tables($schema); - $schema; + method parse { + my $schema = Schema->new({ name => $self->schema_name }); + $self->_add_tables($schema); + $schema; + } } - -__PACKAGE__->meta->make_immutable; - -1; diff --git a/lib/SQL/Translator/Parser/DBI.pm b/lib/SQL/Translator/Parser/DBI.pm index 532c5fd..8372ccf 100644 --- a/lib/SQL/Translator/Parser/DBI.pm +++ b/lib/SQL/Translator/Parser/DBI.pm @@ -1,160 +1,143 @@ -package SQL::Translator::Parser::DBI; -use namespace::autoclean; -use Moose::Role; -use MooseX::Types::Moose qw(Maybe Str); -use DBI::Const::GetInfoType; -use DBI::Const::GetInfo::ANSI; -use DBI::Const::GetInfoReturn; -use aliased 'SQL::Translator::Object::Column'; -use aliased 'SQL::Translator::Object::ForeignKey'; -use aliased 'SQL::Translator::Object::Index'; -use aliased 'SQL::Translator::Object::PrimaryKey'; -use aliased 'SQL::Translator::Object::Table'; -use aliased 'SQL::Translator::Object::View'; - -has 'quoter' => ( - is => 'rw', - isa => Str, - lazy => 1, - default => sub { shift->dbh->get_info(29) || q{"} } -); - -has 'namesep' => ( - is => 'rw', - isa => Str, - lazy => 1, - default => sub { shift->dbh->get_info(41) || '.' } -); - -has 'schema_name' => ( - is => 'rw', - isa => Maybe[Str], - lazy => 1, - default => undef -); - -has 'catalog_name' => ( - is => 'rw', - isa => Maybe[Str], - lazy => 1, - default => undef -); - -sub _subclass { - my $self = shift; - - my $dbtype = $self->dbh->get_info($GetInfoType{SQL_DBMS_NAME}) || $self->dbh->{Driver}{Name}; - - my $class = __PACKAGE__ . '::'. $dbtype; - Class::MOP::load_class($class); - $class->meta->apply($self); -} - -sub _is_auto_increment { 0 } - -sub _column_default_value { - my $self = shift; - my $column_info = shift; - - return $column_info->{COLUMN_DEF}; -} +use MooseX::Declare; +role SQL::Translator::Parser::DBI { + use DBI::Const::GetInfoType; + use DBI::Const::GetInfo::ANSI; + use DBI::Const::GetInfoReturn; + + use MooseX::Types::Moose qw(HashRef Maybe Str); + + use SQL::Translator::Object::Column; + use SQL::Translator::Object::ForeignKey; + use SQL::Translator::Object::Index; + use SQL::Translator::Object::PrimaryKey; + use SQL::Translator::Object::Table; + use SQL::Translator::Object::View; + + use SQL::Translator::Types qw(Schema Table); + + has 'quoter' => ( + is => 'rw', + isa => Str, + lazy => 1, + default => sub { shift->dbh->get_info(29) || q{"} } + ); + + has 'namesep' => ( + is => 'rw', + isa => Str, + lazy => 1, + default => sub { shift->dbh->get_info(41) || '.' } + ); + + has 'schema_name' => ( + is => 'rw', + isa => Maybe[Str], + lazy => 1, + default => undef + ); + + has 'catalog_name' => ( + is => 'rw', + isa => Maybe[Str], + lazy => 1, + default => undef + ); + + method _subclass { + my $dbtype = $self->dbh->get_info($GetInfoType{SQL_DBMS_NAME}) || $self->dbh->{Driver}{Name}; + + my $class = __PACKAGE__ . '::'. $dbtype; + Class::MOP::load_class($class); + $class->meta->apply($self); + } -sub _add_tables { - my $self = shift; - my $schema = shift; - - my $sth = $self->dbh->table_info($self->catalog_name, $self->schema_name, '%', "TABLE,VIEW,'LOCAL TEMPORARY','GLOBAL TEMPORARY'"); - while (my $table_info = $sth->fetchrow_hashref) { - if ($table_info->{TABLE_TYPE} =~ /^(TABLE|LOCAL TEMPORARY|GLOBAL TEMPORARY)$/) { - my $temp = $table_info->{TABLE_TYPE} =~ /TEMPORARY$/ ? 1 : 0; - my $table = Table->new({ name => $table_info->{TABLE_NAME}, temporary => $temp }); - $schema->add_table($table); - $self->_add_columns($table); - $self->_add_primary_key($table); - $self->_add_indexes($table); - } - elsif ($table_info->{TABLE_TYPE} eq 'VIEW') { - my $sql = $self->_get_view_sql($table_info->{TABLE_NAME}); - my $view = View->new({ name => $table_info->{TABLE_NAME}, sql => $sql }); - $schema->add_view($view); - $self->_add_columns($view); + method _is_auto_increment { 0 } + + method _column_default_value(HashRef $column_info) { return $column_info->{COLUMN_DEF}; } + + method _add_tables(Schema $schema) { + my $sth = $self->dbh->table_info($self->catalog_name, $self->schema_name, '%', "TABLE,VIEW,'LOCAL TEMPORARY','GLOBAL TEMPORARY'"); + while (my $table_info = $sth->fetchrow_hashref) { + if ($table_info->{TABLE_TYPE} =~ /^(TABLE|LOCAL TEMPORARY|GLOBAL TEMPORARY)$/) { + my $temp = $table_info->{TABLE_TYPE} =~ /TEMPORARY$/ ? 1 : 0; + my $table = SQL::Translator::Object::Table->new({ name => $table_info->{TABLE_NAME}, temporary => $temp }); + $schema->add_table($table); + + $self->_add_columns($table); + $self->_add_primary_key($table); + $self->_add_indexes($table); + } + elsif ($table_info->{TABLE_TYPE} eq 'VIEW') { + my $sql = $self->_get_view_sql($table_info->{TABLE_NAME}); + my $view = SQL::Translator::Object::View->new({ name => $table_info->{TABLE_NAME}, sql => $sql }); + $schema->add_view($view); + $self->_add_columns($view); + } } + $self->_add_foreign_keys($schema->get_table($_), $schema) for $schema->table_ids; } - $self->_add_foreign_keys($schema->get_table($_), $schema) for $schema->table_ids; -} -sub _add_columns { - my $self = shift; - my $table = shift; - - my $sth = $self->dbh->column_info($self->catalog_name, $self->schema_name, $table->name, '%'); - while (my $column_info = $sth->fetchrow_hashref) { - my $column = Column->new({ name => $column_info->{COLUMN_NAME}, - data_type => $column_info->{DATA_TYPE}, - size => $column_info->{COLUMN_SIZE}, - default_value => $self->_column_default_value($column_info), - is_auto_increment => $self->_is_auto_increment($column_info), - is_nullable => $column_info->{NULLABLE}, - }); - $table->add_column($column); + method _add_columns(Table $table) { + my $sth = $self->dbh->column_info($self->catalog_name, $self->schema_name, $table->name, '%'); + while (my $column_info = $sth->fetchrow_hashref) { + my $column = SQL::Translator::Object::Column->new({ name => $column_info->{COLUMN_NAME}, + data_type => $column_info->{DATA_TYPE}, + size => $column_info->{COLUMN_SIZE}, + default_value => $self->_column_default_value($column_info), + is_auto_increment => $self->_is_auto_increment($column_info), + is_nullable => $column_info->{NULLABLE}, + }); + $table->add_column($column); + } } -} -sub _add_primary_key { - my $self = shift; - my $table = shift; + method _add_primary_key(Table $table) { + my $pk_info = $self->dbh->primary_key_info($self->catalog_name, $self->schema_name, $table->name); - my $pk_info = $self->dbh->primary_key_info($self->catalog_name, $self->schema_name, $table->name); - my ($pk_name, @pk_cols); - while (my $pk_col = $pk_info->fetchrow_hashref) { - $pk_name = $pk_col->{PK_NAME}; - push @pk_cols, $pk_col->{COLUMN_NAME}; - } - my $pk = PrimaryKey->new({ name => $pk_name }); - $pk->add_column($table->get_column($_)) for @pk_cols; - $table->add_index($pk); -} + my ($pk_name, @pk_cols); + while (my $pk_col = $pk_info->fetchrow_hashref) { + $pk_name = $pk_col->{PK_NAME}; + push @pk_cols, $pk_col->{COLUMN_NAME}; + } + return unless $pk_name; -sub _add_foreign_keys { - my $self = shift; - my $table = shift; - my $schema = shift; + my $pk = SQL::Translator::Object::PrimaryKey->new({ name => $pk_name }); + $pk->add_column($table->get_column($_)) for @pk_cols; + $table->add_index($pk); + } - my $fk_info = $self->dbh->foreign_key_info($self->catalog_name, $self->schema_name, $table->name, $self->catalog_name, $self->schema_name, undef); - return unless $fk_info; + method _add_foreign_keys(Table $table, Schema $schema) { + my $fk_info = $self->dbh->foreign_key_info($self->catalog_name, $self->schema_name, $table->name, $self->catalog_name, $self->schema_name, undef); + return unless $fk_info; - my $fk_data; - while (my $fk_col = $fk_info->fetchrow_hashref) { - my $fk_name = $fk_col->{FK_NAME}; + my $fk_data; + while (my $fk_col = $fk_info->fetchrow_hashref) { + my $fk_name = $fk_col->{FK_NAME}; - push @{$fk_data->{$fk_name}{columns}}, $fk_col->{FK_COLUMN_NAME}; - $fk_data->{$fk_name}{table} = $fk_col->{FK_TABLE_NAME}; - $fk_data->{$fk_name}{uk} = $schema->get_table($fk_col->{UK_TABLE_NAME})->get_index($fk_col->{UK_NAME}); - } + push @{$fk_data->{$fk_name}{columns}}, $fk_col->{FK_COLUMN_NAME}; + $fk_data->{$fk_name}{table} = $fk_col->{FK_TABLE_NAME}; + $fk_data->{$fk_name}{uk} = $schema->get_table($fk_col->{UK_TABLE_NAME})->get_index($fk_col->{UK_NAME}); + } - foreach my $fk_name (keys %$fk_data) { - my $fk = ForeignKey->new({ name => $fk_name, references => $fk_data->{$fk_name}{uk} }); - $fk->add_column($schema->get_table($fk_data->{$fk_name}{table})->get_column($_)) for @{$fk_data->{$fk_name}{columns}}; - $table->add_constraint($fk); + foreach my $fk_name (keys %$fk_data) { + my $fk = SQL::Translator::Object::ForeignKey->new({ name => $fk_name, references => $fk_data->{$fk_name}{uk} }); + $fk->add_column($schema->get_table($fk_data->{$fk_name}{table})->get_column($_)) for @{$fk_data->{$fk_name}{columns}}; + $table->add_constraint($fk); + } } -} - -sub _add_indexes { - my $self = shift; - my $table = shift; - my $index_info = $self->dbh->statistics_info($self->catalog_name, $self->schema_name, $table->name, 1, 0); + method _add_indexes(Table $table) { + my $index_info = $self->dbh->statistics_info($self->catalog_name, $self->schema_name, $table->name, 1, 0); - my ($index_name, $index_type, @index_cols); - while (my $index_col = $index_info->fetchrow_hashref) { - $index_name = $index_col->{INDEX_NAME}; - $index_type = $index_col->{NON_UNIQUE} ? 'NORMAL' : 'UNIQUE'; - push @index_cols, $index_col->{COLUMN_NAME}; + my ($index_name, $index_type, @index_cols); + while (my $index_col = $index_info->fetchrow_hashref) { + $index_name = $index_col->{INDEX_NAME}; + $index_type = $index_col->{NON_UNIQUE} ? 'NORMAL' : 'UNIQUE'; + push @index_cols, $index_col->{COLUMN_NAME}; + } + return if $table->exists_index($index_name); + my $index = SQL::Translator::Object::Index->new({ name => $index_name, type => $index_type }); + $index->add_column($table->get_column($_)) for @index_cols; + $table->add_index($index); } - return if $table->exists_index($index_name); - my $index = Index->new({ name => $index_name, type => $index_type }); - $index->add_column($table->get_column($_)) for @index_cols; - $table->add_index($index); } - -1; diff --git a/lib/SQL/Translator/Parser/DBI/MySQL.pm b/lib/SQL/Translator/Parser/DBI/MySQL.pm index 6f39b5f..6085b81 100644 --- a/lib/SQL/Translator/Parser/DBI/MySQL.pm +++ b/lib/SQL/Translator/Parser/DBI/MySQL.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DBI::MySQL; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DBI::MySQL { +} diff --git a/lib/SQL/Translator/Parser/DBI/Oracle.pm b/lib/SQL/Translator/Parser/DBI/Oracle.pm index be89f91..a9fcf11 100644 --- a/lib/SQL/Translator/Parser/DBI/Oracle.pm +++ b/lib/SQL/Translator/Parser/DBI/Oracle.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DBI::Oracle; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DBI::Oracle { +} diff --git a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm index 10c7be6..b768060 100644 --- a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm @@ -1,38 +1,29 @@ -package SQL::Translator::Parser::DBI::PostgreSQL; -use namespace::autoclean; -use Moose::Role; -use MooseX::Types::Moose qw(Str); - -has '+schema_name' => ( - isa => Str, - lazy => 1, - default => 'public' -); - -sub _get_view_sql { - my $self = shift; - my $view = shift; - - my ($sql) = $self->dbh->selectrow_array("SELECT pg_get_viewdef('$view'::regclass)"); - return $sql; -} - -sub _is_auto_increment { - my $self = shift; - my $column_info = shift; - - return $column_info->{COLUMN_DEF} && $column_info->{COLUMN_DEF} =~ /^nextval\(/ ? 1 : 0; -} - -sub _column_default_value { - my $self = shift; - my $column_info = shift; - my $default_value = $column_info->{COLUMN_DEF}; - - if (defined $default_value) { - $default_value =~ s/::.*$// +use MooseX::Declare; +role SQL::Translator::Parser::DBI::PostgreSQL { + use MooseX::Types::Moose qw(HashRef Str); + use SQL::Translator::Types qw(View); + + has '+schema_name' => ( + isa => Str, + lazy => 1, + default => 'public' + ); + + method _get_view_sql(View $view) { + my ($sql) = $self->dbh->selectrow_array("SELECT pg_get_viewdef('$view'::regclass)"); + return $sql; + } + + method _is_auto_increment(HashRef $column_info) { + return $column_info->{COLUMN_DEF} && $column_info->{COLUMN_DEF} =~ /^nextval\(/ ? 1 : 0; + } + + method _column_default_value(HashRef $column_info) { + my $default_value = $column_info->{COLUMN_DEF}; + + if (defined $default_value) { + $default_value =~ s/::.*$// + } + return $default_value; } - return $default_value; } - -1; diff --git a/lib/SQL/Translator/Parser/DBI/SQLite.pm b/lib/SQL/Translator/Parser/DBI/SQLite.pm index 2df7a62..cb6fb7f 100644 --- a/lib/SQL/Translator/Parser/DBI/SQLite.pm +++ b/lib/SQL/Translator/Parser/DBI/SQLite.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DBI::SQLite; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DBI::SQLite { +} diff --git a/lib/SQL/Translator/Parser/DBI/Sybase.pm b/lib/SQL/Translator/Parser/DBI/Sybase.pm index 055f2bb..19afbfd 100644 --- a/lib/SQL/Translator/Parser/DBI/Sybase.pm +++ b/lib/SQL/Translator/Parser/DBI/Sybase.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DBI::Sybase; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DBI::Sybase { +} diff --git a/lib/SQL/Translator/Parser/DDL.pm b/lib/SQL/Translator/Parser/DDL.pm index 2e29d40..4c48910 100644 --- a/lib/SQL/Translator/Parser/DDL.pm +++ b/lib/SQL/Translator/Parser/DDL.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DDL; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DDL { +} diff --git a/lib/SQL/Translator/Parser/DDL/MySQL.pm b/lib/SQL/Translator/Parser/DDL/MySQL.pm index 0f13bef..db48d28 100644 --- a/lib/SQL/Translator/Parser/DDL/MySQL.pm +++ b/lib/SQL/Translator/Parser/DDL/MySQL.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DDL::MySQL; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DDL::MySQL { +} diff --git a/lib/SQL/Translator/Parser/DDL/Oracle.pm b/lib/SQL/Translator/Parser/DDL/Oracle.pm index 4045a3b..ff9ebd7 100644 --- a/lib/SQL/Translator/Parser/DDL/Oracle.pm +++ b/lib/SQL/Translator/Parser/DDL/Oracle.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DDL::Oracle; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DDL::Oracle { +} diff --git a/lib/SQL/Translator/Parser/DDL/PostgreSQL.pm b/lib/SQL/Translator/Parser/DDL/PostgreSQL.pm index 9715e95..98ac674 100644 --- a/lib/SQL/Translator/Parser/DDL/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/DDL/PostgreSQL.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DDL::PostgreSQL; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DDL::PostgreSQL { +} diff --git a/lib/SQL/Translator/Parser/DDL/SQLite.pm b/lib/SQL/Translator/Parser/DDL/SQLite.pm index 448ba3d..b699f9f 100644 --- a/lib/SQL/Translator/Parser/DDL/SQLite.pm +++ b/lib/SQL/Translator/Parser/DDL/SQLite.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DDL::SQLite; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DDL::SQLite { +} diff --git a/lib/SQL/Translator/Parser/DDL/Sybase.pm b/lib/SQL/Translator/Parser/DDL/Sybase.pm index 70dbde6..1e691fa 100644 --- a/lib/SQL/Translator/Parser/DDL/Sybase.pm +++ b/lib/SQL/Translator/Parser/DDL/Sybase.pm @@ -1,5 +1,3 @@ -package SQL::Translator::Parser::DDL::Sybase; -use namespace::autoclean; -use Moose::Role; - -1; +use MooseX::Declare; +role SQL::Translator::Parser::DDL::Sybase { +} diff --git a/lib/SQL/Translator/Producer.pm b/lib/SQL/Translator/Producer.pm index 4cc3084..8c31957 100644 --- a/lib/SQL/Translator/Producer.pm +++ b/lib/SQL/Translator/Producer.pm @@ -1,66 +1,55 @@ -package SQL::Translator::Producer; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(Bool Str); -use SQL::Translator::Types qw(Schema); - -has 'schema' => ( - isa => Schema, - is => 'rw', - required => 1 -); - -has 'no_comments' => ( - isa => Bool, - is => 'rw', - lazy => 1, - default => 0 -); - -has 'drop_table' => ( - isa => Bool, - is => 'rw', - lazy => 1, - default => 1 -); - -sub produce { - my $self = shift; - my $schema = $self->schema; - - $self->_create_table($_) for values %{$schema->tables}; +use MooseX::Declare; +class SQL::Translator::Producer { + use MooseX::Types::Moose qw(Bool Str); + use SQL::Translator::Types qw(Column Schema Table); + + has 'schema' => ( + isa => Schema, + is => 'rw', + required => 1 + ); + + has 'no_comments' => ( + isa => Bool, + is => 'rw', + lazy => 1, + default => 0 + ); + + has 'drop_table' => ( + isa => Bool, + is => 'rw', + lazy => 1, + default => 1 + ); + + method produce { + my $schema = $self->schema; + + $self->_create_table($_) for values %{$schema->tables}; + } + + method _create_table(Table $table) { + my $no_comments = 0; + my $add_drop_table = 1; + my $sqlite_version = 0; + + my $create_table; + my (@column_defs, @index_defs, @constraint_defs); + + $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $add_drop_table; + $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; + + push @column_defs, $self->_create_column($_) for values %{$table->columns}; + $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; + print $create_table . "\n"; + } + + method _create_column(Column $column) { + my $column_def; + $column_def = $column->name . ' ' . $column->data_type; + $column_def .= '(' . $column->size . ')' if $column->size; + $column_def .= ' NOT NULL' unless $column->is_nullable; + $column_def; + } } - -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; - my (@column_defs, @index_defs, @constraint_defs); - - $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $add_drop_table; - $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; - - push @column_defs, $self->_create_column($_) for values %{$table->columns}; - $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; - print $create_table . "\n"; -} - -sub _create_column { - my $self = shift; - my $column = shift; - - my $column_def; - $column_def = $column->name . ' ' . $column->data_type; - $column_def .= '(' . $column->size . ')' if $column->size; - $column_def .= ' NOT NULL' unless $column->is_nullable; - $column_def; -} - -__PACKAGE__->meta->make_immutable; - -1; diff --git a/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm b/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm index 5860d92..3f3679b 100644 --- a/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm @@ -1,84 +1,71 @@ -package SQL::Translator::Producer::SQL::PostgreSQL; -use namespace::autoclean; -use Moose::Role; -use SQL::Translator::Constants qw(:sqlt_types); - -my %data_type_mapping = ( - SQL_LONGVARCHAR() => 'text', - SQL_TIMESTAMP() => 'timestamp', - SQL_TYPE_TIMESTAMP() => 'timestamp without time zone', - SQL_TYPE_TIMESTAMP_WITH_TIMEZONE() => 'timestamp', - SQL_INTEGER() => 'integer', - SQL_CHAR() => 'character', - SQL_VARCHAR() => 'varchar', - SQL_BIGINT() => 'bigint', -); - -sub _create_table { - my $self = shift; - my $table = shift; - - my $pg_version = 0; - - my $create_table; - my (@create, @column_defs, @index_defs, @constraint_defs); - - $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $self->drop_table; - $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; - - push @column_defs, $self->_create_column($_) for values %{$table->columns}; - $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; - - #use Data::Dumper; print Dumper($table->indexes); - foreach my $index (values %{$table->indexes}) { - if ($index->type eq 'NORMAL') { - push @index_defs, $self->_create_index($index, $table); - } else { - push @constraint_defs, $self->_create_index($index); +use MooseX::Declare; +role SQL::Translator::Producer::SQL::PostgreSQL { + use SQL::Translator::Constants qw(:sqlt_types); + use SQL::Translator::Types qw(Column Index Table); + + my %data_type_mapping = ( + SQL_LONGVARCHAR() => 'text', + SQL_TIMESTAMP() => 'timestamp', + SQL_TYPE_TIMESTAMP() => 'timestamp without time zone', + SQL_TYPE_TIMESTAMP_WITH_TIMEZONE() => 'timestamp', + SQL_INTEGER() => 'integer', + SQL_CHAR() => 'character', + SQL_VARCHAR() => 'varchar', + SQL_BIGINT() => 'bigint', + ); + + method _create_table(Table $table) { + my $pg_version = 0; + + my $create_table; + my (@create, @column_defs, @index_defs, @constraint_defs); + + $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $self->drop_table; + $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; + + push @column_defs, $self->_create_column($_) for values %{$table->columns}; + $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; + + foreach my $index (values %{$table->indexes}) { + if ($index->type eq 'NORMAL') { + push @index_defs, $self->_create_index($index, $table); + } else { + push @constraint_defs, $self->_create_index($index); + } } + + print $create_table . ";\n"; + return (@create, $create_table, @index_defs, @constraint_defs); } - - print $create_table . ";\n"; -# use Data::Dumper; print Dumper(@index_defs); print Dumper(@constraint_defs); - return (@create, $create_table, @index_defs, @constraint_defs); -} - -sub _create_column { - my $self = shift; - my $column = shift; - - my $size = $column->size; - my $default_value = $column->default_value; - - my $column_def; - $column_def = $column->name . ' '; - $column_def .= defined $data_type_mapping{$column->data_type} - ? $data_type_mapping{$column->data_type} - : $column->data_type; - $column_def .= '(' . $column->size . ')' if $size; - $column_def .= ' NOT NULL' unless $column->is_nullable; - $column_def .= ' PRIMARY KEY' if $column->is_auto_increment; - $column_def .= ' DEFAULT ' . $default_value if $column->default_value && !$column->is_auto_increment; - $column_def; -} - -sub _create_index { - my $self = shift; - my $index = shift; - my $table = shift; - - my $index_def; - if ($index->type eq 'PRIMARY_KEY') { - $index_def = 'CONSTRAINT ' . $index->name . ' PRIMARY KEY ' . '(' . (join ', ', map { $_->name } values %{$index->columns}) . ')'; - } - elsif ($index->type eq 'UNIQUE') { - $index_def = 'CONSTRAINT ' . $index->name . ' UNIQUE ' . '(' . (join ', ', map { $_->name } values %{$index->columns}) . ')'; + + method _create_column(Column $column) { + my $size = $column->size; + my $default_value = $column->default_value; + + my $column_def; + $column_def = $column->name . ' '; + $column_def .= defined $data_type_mapping{$column->data_type} + ? $data_type_mapping{$column->data_type} + : $column->data_type; + $column_def .= '(' . $column->size . ')' if $size; + $column_def .= ' NOT NULL' unless $column->is_nullable; + $column_def .= ' PRIMARY KEY' if $column->is_auto_increment; + $column_def .= ' DEFAULT ' . $default_value if $column->default_value && !$column->is_auto_increment; + $column_def; } - elsif ($index->type eq 'NORMAL') { - $index_def = 'CREATE INDEX ' . $index->name . ' ON ' . $table->name . '('. (join ', ', map { $_->name } values %{$index->columns}) . ')'; + + method _create_index(Index $index, Table $table?) { + my $index_def; + if ($index->type eq 'PRIMARY_KEY') { + $index_def = 'CONSTRAINT ' . $index->name . ' PRIMARY KEY ' . '(' . (join ', ', map { $_->name } values %{$index->columns}) . ')'; + } + elsif ($index->type eq 'UNIQUE') { + $index_def = 'CONSTRAINT ' . $index->name . ' UNIQUE ' . '(' . (join ', ', map { $_->name } values %{$index->columns}) . ')'; + } + elsif ($index->type eq 'NORMAL') { + $index_def = 'CREATE INDEX ' . $index->name . ' ON ' . $table->name . '('. (join ', ', map { $_->name } values %{$index->columns}) . ')'; + } + + $index_def; } - - $index_def; } - -1; diff --git a/lib/SQL/Translator/Producer/SQL/SQLite.pm b/lib/SQL/Translator/Producer/SQL/SQLite.pm index b014344..e00ac22 100644 --- a/lib/SQL/Translator/Producer/SQL/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQL/SQLite.pm @@ -1,54 +1,47 @@ -package SQL::Translator::Producer::SQL::SQLite; -use namespace::autoclean; -use Moose::Role; -use SQL::Translator::Constants qw(:sqlt_types); - -my %data_type_mapping = ( - SQL_LONGVARCHAR() => 'text', - SQL_TIMESTAMP() => 'timestamp', - SQL_INTEGER() => 'integer', - SQL_CHAR() => 'character', - SQL_VARCHAR() => 'varchar', - SQL_BIGINT() => 'integer', -); - -sub _create_table { - my $self = shift; - my $table = shift; - - my $sqlite_version = 0; - - my $create_table; - my (@create, @column_defs, @index_defs, @constraint_defs); - - $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $self->drop_table; - $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; - - push @column_defs, $self->_create_column($_) for values %{$table->columns}; - $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; - - print $create_table . ";\n"; - return (@create, $create_table, @index_defs, @constraint_defs ); +use MooseX:Declare; +role SQL::Translator::Producer::SQL::SQLite { + use SQL::Translator::Constants qw(:sqlt_types); + use SQL::Translator::Types qw(Column Table); + + my %data_type_mapping = ( + SQL_LONGVARCHAR() => 'text', + SQL_TIMESTAMP() => 'timestamp', + SQL_INTEGER() => 'integer', + SQL_CHAR() => 'character', + SQL_VARCHAR() => 'varchar', + SQL_BIGINT() => 'integer', + ); + + method _create_table(Table $table) { + my $sqlite_version = 0; + + my $create_table; + my (@create, @column_defs, @index_defs, @constraint_defs); + + $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $self->drop_table; + $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; + + push @column_defs, $self->_create_column($_) for values %{$table->columns}; + $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; + + print $create_table . ";\n"; + return (@create, $create_table, @index_defs, @constraint_defs ); + } + + method _create_column(Column $column) { + my $size = $column->data_type == SQL_TIMESTAMP() ? undef : $column->size; + my $default_value = $column->default_value; + $default_value =~ s/^now[()]*/CURRENT_TIMESTAMP/i if $default_value; + + my $column_def; + $column_def = $column->name . ' '; + $column_def .= defined $data_type_mapping{$column->data_type} + ? $data_type_mapping{$column->data_type} + : $column->data_type; + #$column_def .= '(' . $column->size . ')' if $size; + $column_def .= ' NOT NULL' unless $column->is_nullable; + $column_def .= ' PRIMARY KEY' if $column->is_auto_increment; + $column_def .= ' DEFAULT ' . $default_value if $column->default_value && !$column->is_auto_increment; + $column_def; + } } - -sub _create_column { - my $self = shift; - my $column = shift; - - my $size = $column->data_type == SQL_TIMESTAMP() ? undef : $column->size; - my $default_value = $column->default_value; - $default_value =~ s/^now[()]*/CURRENT_TIMESTAMP/i if $default_value; - - my $column_def; - $column_def = $column->name . ' '; - $column_def .= defined $data_type_mapping{$column->data_type} - ? $data_type_mapping{$column->data_type} - : $column->data_type; - #$column_def .= '(' . $column->size . ')' if $size; - $column_def .= ' NOT NULL' unless $column->is_nullable; - $column_def .= ' PRIMARY KEY' if $column->is_auto_increment; - $column_def .= ' DEFAULT ' . $default_value if $column->default_value && !$column->is_auto_increment; - $column_def; -} - -1;