From: Justin Hunter Date: Fri, 7 Aug 2009 05:49:48 +0000 (-0700) Subject: turn data_type_mapping into an attribute X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a5c5cd44b669fc520d238df3ee9c891a7ddf261c;p=dbsrgits%2FSQL-Translator-2.0-ish.git turn data_type_mapping into an attribute --- diff --git a/lib/SQL/Translator/Producer.pm b/lib/SQL/Translator/Producer.pm index 8c31957..0e8183b 100644 --- a/lib/SQL/Translator/Producer.pm +++ b/lib/SQL/Translator/Producer.pm @@ -1,6 +1,7 @@ use MooseX::Declare; class SQL::Translator::Producer { - use MooseX::Types::Moose qw(Bool Str); + use SQL::Translator::Constants qw(:sqlt_types); + use MooseX::Types::Moose qw(Bool HashRef Str); use SQL::Translator::Types qw(Column Schema Table); has 'schema' => ( @@ -22,7 +23,27 @@ class SQL::Translator::Producer { lazy => 1, default => 1 ); - + + has 'data_type_mapping' => ( + isa => HashRef, + is => 'ro', + lazy_build => 1 + ); + + method _build_data_type_mapping { + return { + 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', + SQL_FLOAT() => 'numeric', + }; + } + method produce { my $schema = $self->schema; diff --git a/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm b/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm index 8c73dd8..edef6f9 100644 --- a/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm @@ -3,18 +3,6 @@ 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', - SQL_FLOAT() => 'numeric', - ); - method _create_table(Table $table) { my $pg_version = 0; @@ -46,8 +34,8 @@ role SQL::Translator::Producer::SQL::PostgreSQL { my $column_def; $column_def = $column->name . ' '; - $column_def .= defined $data_type_mapping{$column->data_type} - ? $data_type_mapping{$column->data_type} + $column_def .= defined $self->data_type_mapping->{$column->data_type} + ? $self->data_type_mapping->{$column->data_type} : $column->data_type; $column_def .= '(' . $column->size . ')' if $size; $column_def .= ' NOT NULL' unless $column->is_nullable; diff --git a/lib/SQL/Translator/Producer/SQL/SQLite.pm b/lib/SQL/Translator/Producer/SQL/SQLite.pm index d11c769..f4f093a 100644 --- a/lib/SQL/Translator/Producer/SQL/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQL/SQLite.pm @@ -2,15 +2,14 @@ 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', - ); + + around _build_data_type_mapping { + my $data_type_mapping = $self->$orig; + $data_type_mapping->{SQL_FLOAT()} = 'real'; + $data_type_mapping->{SQL_BIGINT()} = 'integer'; + + return $data_type_mapping; + }; method _create_table(Table $table) { my $sqlite_version = 0; @@ -35,8 +34,8 @@ role SQL::Translator::Producer::SQL::SQLite { my $column_def; $column_def = $column->name . ' '; - $column_def .= defined $data_type_mapping{$column->data_type} - ? $data_type_mapping{$column->data_type} + $column_def .= defined $self->data_type_mapping->{$column->data_type} + ? $self->data_type_mapping->{$column->data_type} : $column->data_type; #$column_def .= '(' . $column->size . ')' if $size; $column_def .= ' NOT NULL' unless $column->is_nullable;