2 class SQL::Translator::Producer {
3 use SQL::Translator::Constants qw(:sqlt_types);
4 use MooseX::Types::Moose qw(ArrayRef Bool HashRef ScalarRef Str);
5 use SQL::Translator::Types qw(Column Table Translator);
7 has 'data_type_mapping' => (
18 handles => [ qw(schema) ],
21 method _build_data_type_mapping {
23 SQL_LONGVARCHAR() => 'text',
24 SQL_TIMESTAMP() => 'timestamp',
25 SQL_TYPE_TIMESTAMP() => 'timestamp without time zone',
26 SQL_TYPE_TIMESTAMP_WITH_TIMEZONE() => 'timestamp',
27 SQL_INTEGER() => 'integer',
29 SQL_VARCHAR() => 'varchar',
30 SQL_BIGINT() => 'bigint',
31 SQL_FLOAT() => 'numeric',
36 my $schema = $self->schema;
38 $self->_create_table($_) for values %{$schema->tables};
41 method _create_table(Table $table) {
43 my $add_drop_table = 1;
44 my $sqlite_version = 0;
47 my (@column_defs, @index_defs, @constraint_defs);
49 $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $add_drop_table;
50 $create_table .= 'CREATE TABLE ' . $table->name . " (\n";
52 push @column_defs, $self->_create_column($_) for values %{$table->columns};
53 $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)";
54 print $create_table . "\n";
57 method _create_column(Column $column) {
59 $column_def = $column->name . ' ' . $column->data_type;
60 $column_def .= '(' . $column->size . ')' if $column->size;
61 $column_def .= ' NOT NULL' unless $column->is_nullable;
65 method _default_value(ScalarRef|Str $default, ArrayRef $exceptions) {
66 if ($exceptions and ! ref $default) {
67 for (my $i = 0; $i < @$exceptions; $i += 2) {
68 my ($pat, $val) = @$exceptions[ $i, $i + 1 ];
69 if (ref $pat and $default =~ $pat) {
72 } elsif (lc $default eq lc $pat) {
79 return ref($default) ? " DEFAULT $$default" : " DEFAULT '$default'";