-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;
+
+}
-package SQL::Translator::Object;
-use namespace::autoclean;
-use Moose;
-use Tie::IxHash;
-
-1;
+use MooseX::Declare;
+class SQL::Translator::Object {
+ use Tie::IxHash;
+}
-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,
+ );
+}
-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
+ );
+}
-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,
+ );
+}
-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
+ );
+}
-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',
+ );
+}
-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 }
+ );
+}
-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 { {} },
+ );
+}
-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
+ );
+}
-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
+ );
+}
-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
+ );
+}
-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
+ );
+}
-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;
-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;
-package SQL::Translator::Parser::DBI::MySQL;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DBI::MySQL {
+}
-package SQL::Translator::Parser::DBI::Oracle;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DBI::Oracle {
+}
-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;
-package SQL::Translator::Parser::DBI::SQLite;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DBI::SQLite {
+}
-package SQL::Translator::Parser::DBI::Sybase;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DBI::Sybase {
+}
-package SQL::Translator::Parser::DDL;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DDL {
+}
-package SQL::Translator::Parser::DDL::MySQL;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DDL::MySQL {
+}
-package SQL::Translator::Parser::DDL::Oracle;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DDL::Oracle {
+}
-package SQL::Translator::Parser::DDL::PostgreSQL;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DDL::PostgreSQL {
+}
-package SQL::Translator::Parser::DDL::SQLite;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DDL::SQLite {
+}
-package SQL::Translator::Parser::DDL::Sybase;
-use namespace::autoclean;
-use Moose::Role;
-
-1;
+use MooseX::Declare;
+role SQL::Translator::Parser::DDL::Sybase {
+}
-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;
-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;
-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;