turn data_type_mapping into an attribute
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Producer / SQL / SQLite.pm
CommitLineData
e287b229 1use MooseX::Declare;
4f4fd192 2role SQL::Translator::Producer::SQL::SQLite {
3 use SQL::Translator::Constants qw(:sqlt_types);
4 use SQL::Translator::Types qw(Column Table);
a5c5cd44 5
6 around _build_data_type_mapping {
7 my $data_type_mapping = $self->$orig;
8 $data_type_mapping->{SQL_FLOAT()} = 'real';
9 $data_type_mapping->{SQL_BIGINT()} = 'integer';
10
11 return $data_type_mapping;
12 };
4f4fd192 13
14 method _create_table(Table $table) {
15 my $sqlite_version = 0;
16
17 my $create_table;
18 my (@create, @column_defs, @index_defs, @constraint_defs);
19
20 $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $self->drop_table;
21 $create_table .= 'CREATE TABLE ' . $table->name . " (\n";
22
23 push @column_defs, $self->_create_column($_) for values %{$table->columns};
24 $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)";
25
26 print $create_table . ";\n";
27 return (@create, $create_table, @index_defs, @constraint_defs );
28 }
29
30 method _create_column(Column $column) {
31 my $size = $column->data_type == SQL_TIMESTAMP() ? undef : $column->size;
32 my $default_value = $column->default_value;
33 $default_value =~ s/^now[()]*/CURRENT_TIMESTAMP/i if $default_value;
34
35 my $column_def;
36 $column_def = $column->name . ' ';
a5c5cd44 37 $column_def .= defined $self->data_type_mapping->{$column->data_type}
38 ? $self->data_type_mapping->{$column->data_type}
4f4fd192 39 : $column->data_type;
40 #$column_def .= '(' . $column->size . ')' if $size;
41 $column_def .= ' NOT NULL' unless $column->is_nullable;
42 $column_def .= ' PRIMARY KEY' if $column->is_auto_increment;
43 $column_def .= ' DEFAULT ' . $default_value if $column->default_value && !$column->is_auto_increment;
44 $column_def;
45 }
287d4603 46}