X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer.pm;h=7363ee936ff91d0762863d67aadbab5750ef03e9;hb=b62122e666f4975b52ffad3386fdcc83554c847b;hp=8c31957e491cbaef2111bc334c577d761168c35f;hpb=4f4fd192801c55deb75aa8e4bc440b220e24c396;p=dbsrgits%2FSQL-Translator-2.0-ish.git diff --git a/lib/SQL/Translator/Producer.pm b/lib/SQL/Translator/Producer.pm index 8c31957..7363ee9 100644 --- a/lib/SQL/Translator/Producer.pm +++ b/lib/SQL/Translator/Producer.pm @@ -1,31 +1,40 @@ use MooseX::Declare; class SQL::Translator::Producer { - use MooseX::Types::Moose qw(Bool Str); - use SQL::Translator::Types qw(Column Schema Table); + use SQL::Translator::Constants qw(:sqlt_types); + use MooseX::Types::Moose qw(ArrayRef Bool HashRef ScalarRef Str); + use SQL::Translator::Types qw(Column Table Translator); - has 'schema' => ( - isa => Schema, - is => 'rw', - required => 1 + has 'data_type_mapping' => ( + isa => HashRef, + is => 'ro', + lazy_build => 1 ); - - has 'no_comments' => ( - isa => Bool, - is => 'rw', - lazy => 1, - default => 0 - ); - - has 'drop_table' => ( - isa => Bool, - is => 'rw', - lazy => 1, - default => 1 + + has 'translator' => ( + isa => Translator, + is => 'ro', + weak_ref => 1, + required => 1, + handles => [ qw(schema) ], ); - + + 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() => 'char', + SQL_VARCHAR() => 'varchar', + SQL_BIGINT() => 'bigint', + SQL_FLOAT() => 'numeric', + }; + } + method produce { my $schema = $self->schema; - + $self->_create_table($_) for values %{$schema->tables}; } @@ -52,4 +61,21 @@ class SQL::Translator::Producer { $column_def .= ' NOT NULL' unless $column->is_nullable; $column_def; } + + method _default_value(ScalarRef|Str $default, ArrayRef $exceptions) { + if ($exceptions and ! ref $default) { + for (my $i = 0; $i < @$exceptions; $i += 2) { + my ($pat, $val) = @$exceptions[ $i, $i + 1 ]; + if (ref $pat and $default =~ $pat) { + $default = $val; + last; + } elsif (lc $default eq lc $pat) { + $default = $val; + last + } + } + } + + return ref($default) ? " DEFAULT $$default" : " DEFAULT '$default'"; + } }