fix up is_nullable and is_unique being wrong at times
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Parser / DDL / SQLite.pm
1 use MooseX::Declare;
2 role SQL::Translator::Parser::DDL::SQLite {
3     use MooseX::Types::Moose qw(Str);
4     use MooseX::MultiMethods;
5     use Moose::Autobox;
6     use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants);
7     use SQL::Translator::Types qw(Schema);
8     use aliased 'SQL::Translator::Object::Column';
9     use aliased 'SQL::Translator::Object::Constraint';
10     use aliased 'SQL::Translator::Object::ForeignKey';
11     use aliased 'SQL::Translator::Object::Index';
12     use aliased 'SQL::Translator::Object::PrimaryKey';
13     use aliased 'SQL::Translator::Object::Table';
14     use aliased 'SQL::Translator::Object::Trigger';
15     use aliased 'SQL::Translator::Object::View';
16
17     around _build_data_type_mapping {
18         my $data_type_mapping = $self->$orig;
19         $data_type_mapping->{date} = SQL_DATE();
20
21         return $data_type_mapping;
22     };
23
24     multi method parse(Schema $data) { $data }
25
26     multi method parse(Str $data) {
27         my $translator = $self->translator;
28         my $parser = Parse::RecDescent->new($self->grammar);
29     
30         unless (defined $parser) {
31             return $translator->error("Error instantiating Parse::RecDescent ".
32                 "instance: Bad grammar");
33         }
34     
35         my $result = $parser->startrule($data);
36         die "Parse failed" unless defined $result;
37     
38         my $schema = $translator->schema;
39         my @tables = 
40             sort  { $result->{tables}{$a}{order} <=> $result->{tables}{$b}{order} }
41             keys %{ $result->{tables} };
42
43         for my $table_name ( @tables ) {
44             my $tdata = $result->{tables}{ $table_name };
45             my $table = Table->new({ name => $tdata->{name}, schema => $schema });
46             $table->comments( $tdata->{comments}->flatten ) if $tdata->{comments};
47             $schema->add_table($table);
48     
49             for my $fdata ( @{ $tdata->{fields} } ) {
50                 my $field = Column->new({
51                     name              => $fdata->{name},
52                     data_type         => $fdata->{data_type},
53                     sql_data_type     => $self->data_type_mapping->{$fdata->{data_type}} || -999999,
54                     size              => $fdata->{size},
55                     default_value     => $fdata->{default},
56                     is_auto_increment => $fdata->{is_auto_inc},
57                     is_nullable       => $fdata->{is_primary_key} ? 0 : $fdata->{is_nullable},
58                     comments          => $fdata->{comments},
59                     table             => $table,
60                 });
61
62                 $table->add_column($field);
63                 $table->primary_key( $field->name ) if $fdata->{is_primary_key};
64
65                 for my $cdata ( @{ $fdata->{constraints} } ) {
66                     next unless $cdata->{type} eq 'foreign_key';
67                     $cdata->{fields} ||= [ $field->name ];
68                     push @{ $tdata->{constraints} }, $cdata;
69                 }
70             }
71     
72             for my $idata ( @{ $tdata->{indices} || [] } ) {
73                 my @columns = delete $idata->{fields};
74                 my $index = Index->new({
75                     name    => $idata->{name},
76                     type    => uc $idata->{type},
77                     table   => $table,
78                 });
79                 $index->add_column($table->get_column(@$_[0])) for @columns;
80                 $table->add_index($index);
81             }
82     
83             for my $cdata ( @{ $tdata->{constraints} || [] } ) {
84                 my $constraint;
85                 if (uc $cdata->{type} eq 'PRIMARY_KEY') {
86                     $constraint = PrimaryKey->new({ name => $cdata->{name} || 'primary_key', table => $table });
87
88                     for my $field (@{$cdata->{fields}}) {
89                         $table->get_column($field)->is_primary_key(1);
90                         $table->get_column($field)->is_nullable(0);
91                         $constraint->add_column($table->get_column($field));
92                     }
93                 } elsif (uc $cdata->{type} eq 'FOREIGN_KEY') {
94                     $constraint = ForeignKey->new({ name => $cdata->{name} || 'foreign_key',
95                                                     table => $table,
96                                                     reference_table => $cdata->{reference_table},
97                                                     reference_columns => ref $cdata->{reference_fields} ? $cdata->{reference_fields} : [ $cdata->{reference_fields} ],
98                                                     on_delete => $cdata->{on_delete} || $cdata->{on_delete_do},
99                                                     on_update => $cdata->{on_update} || $cdata->{on_update_do} });
100                     for my $field (@{$cdata->{fields}}) {
101                         $table->get_column($field)->is_foreign_key(1);
102                         $table->get_column($field)->foreign_key_reference($constraint);
103                         $constraint->add_column($table->get_column($field));
104                     }
105                 } else {
106                     $constraint = Constraint->new({ name => $cdata->{name} || 'constraint', type => uc $cdata->{type}, table => $table });
107                     if (uc $cdata->{type} eq 'UNIQUE') {
108                         $table->get_column($_)->is_unique(1) for @{$cdata->{fields}};
109                     }
110                     $constraint->add_column($table->get_column($_)) for @{$cdata->{fields}};
111                 }
112   
113                 $table->add_constraint($constraint);
114             }
115         }
116     
117         for my $def ( @{ $result->{views} || [] } ) {
118             my $view = View->new({
119                 name   => $def->{name},
120                 fields => $def->{fields},
121                 sql    => $def->{sql},
122             });
123             $schema->add_view($view);
124         }
125     
126         for my $def ( @{ $result->{triggers} || [] } ) {
127             my $trigger = Trigger->new({
128                 name                => $def->{name},
129                 perform_action_when => $def->{when},
130                 database_events     => $def->{db_events},
131                 action              => $def->{action},
132                 on_table            => $def->{on_table},
133             });
134             $schema->add_trigger($trigger);
135         }
136         return 1;
137     }
138 }