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