move to MooseX::Declare
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Producer.pm
CommitLineData
4f4fd192 1use MooseX::Declare;
2class SQL::Translator::Producer {
3 use MooseX::Types::Moose qw(Bool Str);
4 use SQL::Translator::Types qw(Column Schema Table);
5
6 has 'schema' => (
7 isa => Schema,
8 is => 'rw',
9 required => 1
10 );
11
12 has 'no_comments' => (
13 isa => Bool,
14 is => 'rw',
15 lazy => 1,
16 default => 0
17 );
18
19 has 'drop_table' => (
20 isa => Bool,
21 is => 'rw',
22 lazy => 1,
23 default => 1
24 );
25
26 method produce {
27 my $schema = $self->schema;
28
29 $self->_create_table($_) for values %{$schema->tables};
30 }
31
32 method _create_table(Table $table) {
33 my $no_comments = 0;
34 my $add_drop_table = 1;
35 my $sqlite_version = 0;
36
37 my $create_table;
38 my (@column_defs, @index_defs, @constraint_defs);
39
40 $create_table .= 'DROP TABLE ' . $table->name . ";\n" if $add_drop_table;
41 $create_table .= 'CREATE TABLE ' . $table->name . " (\n";
42
43 push @column_defs, $self->_create_column($_) for values %{$table->columns};
44 $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)";
45 print $create_table . "\n";
46 }
47
48 method _create_column(Column $column) {
49 my $column_def;
50 $column_def = $column->name . ' ' . $column->data_type;
51 $column_def .= '(' . $column->size . ')' if $column->size;
52 $column_def .= ' NOT NULL' unless $column->is_nullable;
53 $column_def;
54 }
a832857f 55}