2 role SQL::Translator::Parser::DDL::SQLite {
3 use MooseX::Types::Moose qw(Str);
4 use MooseX::MultiMethods;
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';
17 around _build_data_type_mapping {
18 my $data_type_mapping = $self->$orig;
19 $data_type_mapping->{date} = SQL_DATE();
21 return $data_type_mapping;
24 multi method parse(Schema $data) { $data }
26 multi method parse(Str $data) {
27 my $translator = $self->translator;
28 my $parser = Parse::RecDescent->new($self->grammar);
30 unless (defined $parser) {
31 return $translator->error("Error instantiating Parse::RecDescent ".
32 "instance: Bad grammar");
35 my $result = $parser->startrule($data);
36 die "Parse failed" unless defined $result;
38 my $schema = $translator->schema;
40 sort { $result->{tables}{$a}{order} <=> $result->{tables}{$b}{order} }
41 keys %{ $result->{tables} };
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);
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},
62 $table->add_column($field);
63 $table->primary_key( $field->name ) if $fdata->{is_primary_key};
65 for my $cdata ( @{ $fdata->{constraints} } ) {
66 next unless $cdata->{type} eq 'foreign_key';
67 $cdata->{fields} ||= [ $field->name ];
68 push @{ $tdata->{constraints} }, $cdata;
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},
79 $index->add_column($table->get_column(@$_[0])) for @columns;
80 $table->add_index($index);
83 for my $cdata ( @{ $tdata->{constraints} || [] } ) {
85 if (uc $cdata->{type} eq 'PRIMARY_KEY') {
86 $constraint = PrimaryKey->new({ name => $cdata->{name} || 'primary_key', table => $table });
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));
93 } elsif (uc $cdata->{type} eq 'FOREIGN_KEY') {
94 $constraint = ForeignKey->new({ name => $cdata->{name} || 'foreign_key',
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));
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}};
110 $constraint->add_column($table->get_column($_)) for @{$cdata->{fields}};
113 $table->add_constraint($constraint);
117 for my $def ( @{ $result->{views} || [] } ) {
118 my $view = View->new({
119 name => $def->{name},
120 fields => $def->{fields},
123 $schema->add_view($view);
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},
134 $schema->add_trigger($trigger);