Commit | Line | Data |
4f4fd192 |
1 | use MooseX::Declare; |
2 | class SQL::Translator::Producer { |
a5c5cd44 |
3 | use SQL::Translator::Constants qw(:sqlt_types); |
4 | use MooseX::Types::Moose qw(Bool HashRef Str); |
4f4fd192 |
5 | use SQL::Translator::Types qw(Column Schema Table); |
6 | |
7 | has 'schema' => ( |
8 | isa => Schema, |
9 | is => 'rw', |
10 | required => 1 |
11 | ); |
12 | |
13 | has 'no_comments' => ( |
14 | isa => Bool, |
15 | is => 'rw', |
16 | lazy => 1, |
17 | default => 0 |
18 | ); |
19 | |
20 | has 'drop_table' => ( |
21 | isa => Bool, |
22 | is => 'rw', |
23 | lazy => 1, |
24 | default => 1 |
25 | ); |
a5c5cd44 |
26 | |
27 | has 'data_type_mapping' => ( |
28 | isa => HashRef, |
29 | is => 'ro', |
30 | lazy_build => 1 |
31 | ); |
32 | |
33 | method _build_data_type_mapping { |
34 | return { |
35 | SQL_LONGVARCHAR() => 'text', |
36 | SQL_TIMESTAMP() => 'timestamp', |
37 | SQL_TYPE_TIMESTAMP() => 'timestamp without time zone', |
38 | SQL_TYPE_TIMESTAMP_WITH_TIMEZONE() => 'timestamp', |
39 | SQL_INTEGER() => 'integer', |
2eee6789 |
40 | SQL_CHAR() => 'char', |
a5c5cd44 |
41 | SQL_VARCHAR() => 'varchar', |
42 | SQL_BIGINT() => 'bigint', |
43 | SQL_FLOAT() => 'numeric', |
44 | }; |
45 | } |
46 | |
4f4fd192 |
47 | method produce { |
48 | my $schema = $self->schema; |
49 | |
50 | $self->_create_table($_) for values %{$schema->tables}; |
51 | } |
52 | |
53 | method _create_table(Table $table) { |
54 | my $no_comments = 0; |
55 | my $add_drop_table = 1; |
56 | my $sqlite_version = 0; |
57 | |
58 | my $create_table; |
59 | my (@column_defs, @index_defs, @constraint_defs); |
60 | |
61 | $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $add_drop_table; |
62 | $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; |
63 | |
64 | push @column_defs, $self->_create_column($_) for values %{$table->columns}; |
65 | $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; |
66 | print $create_table . "\n"; |
67 | } |
68 | |
69 | method _create_column(Column $column) { |
70 | my $column_def; |
71 | $column_def = $column->name . ' ' . $column->data_type; |
72 | $column_def .= '(' . $column->size . ')' if $column->size; |
73 | $column_def .= ' NOT NULL' unless $column->is_nullable; |
74 | $column_def; |
75 | } |
a832857f |
76 | } |