Commit | Line | Data |
c87fe35d |
1 | package SQL::Translator::Producer; |
a832857f |
2 | use namespace::autoclean; |
c87fe35d |
3 | use Moose; |
672dfd6b |
4 | use MooseX::Types::Moose qw(Bool Str); |
a832857f |
5 | use SQL::Translator::Types qw(Schema); |
6 | |
a832857f |
7 | has 'schema' => ( |
44547961 |
8 | isa => Schema, |
9 | is => 'rw', |
10 | required => 1 |
a832857f |
11 | ); |
12 | |
672dfd6b |
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 | ); |
26 | |
a832857f |
27 | sub produce { |
28 | my $self = shift; |
29 | my $schema = $self->schema; |
30 | |
44547961 |
31 | $self->_create_table($_) for values %{$schema->tables}; |
a832857f |
32 | } |
33 | |
34 | sub _create_table { |
35 | my $self = shift; |
36 | my $table = shift; |
37 | |
38 | my $no_comments = 0; |
39 | my $add_drop_table = 1; |
40 | my $sqlite_version = 0; |
41 | |
42 | my $create_table; |
44547961 |
43 | my (@column_defs, @index_defs, @constraint_defs); |
44 | |
45 | $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $add_drop_table; |
46 | $create_table .= 'CREATE TABLE ' . $table->name . " (\n"; |
a832857f |
47 | |
44547961 |
48 | push @column_defs, $self->_create_column($_) for values %{$table->columns}; |
49 | $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; |
a832857f |
50 | print $create_table . "\n"; |
51 | } |
52 | |
44547961 |
53 | sub _create_column { |
54 | my $self = shift; |
55 | my $column = shift; |
56 | |
57 | my $column_def; |
58 | $column_def = $column->name . ' ' . $column->data_type; |
59 | $column_def .= '(' . $column->size . ')' if $column->size; |
60 | $column_def .= ' NOT NULL' unless $column->is_nullable; |
61 | $column_def; |
62 | } |
63 | |
a832857f |
64 | __PACKAGE__->meta->make_immutable; |
c87fe35d |
65 | |
66 | 1; |