add drop_table and no_comments attributes
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Producer.pm
CommitLineData
c87fe35d 1package SQL::Translator::Producer;
a832857f 2use namespace::autoclean;
c87fe35d 3use Moose;
672dfd6b 4use MooseX::Types::Moose qw(Bool Str);
a832857f 5use SQL::Translator::Types qw(Schema);
6
a832857f 7has 'schema' => (
44547961 8 isa => Schema,
9 is => 'rw',
10 required => 1
a832857f 11);
12
672dfd6b 13has 'no_comments' => (
14 isa => Bool,
15 is => 'rw',
16 lazy => 1,
17 default => 0
18);
19
20has 'drop_table' => (
21 isa => Bool,
22 is => 'rw',
23 lazy => 1,
24 default => 1
25);
26
a832857f 27sub produce {
28 my $self = shift;
29 my $schema = $self->schema;
30
44547961 31 $self->_create_table($_) for values %{$schema->tables};
a832857f 32}
33
34sub _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 53sub _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
661;