2 role SQL::Translator::Parser::DDL::MySQL {
3 use MooseX::Types::Moose qw(Str);
4 use MooseX::MultiMethods;
5 use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants);
6 use aliased 'SQL::Translator::Object::Column';
7 use aliased 'SQL::Translator::Object::Constraint';
8 use aliased 'SQL::Translator::Object::ForeignKey';
9 use aliased 'SQL::Translator::Object::Index';
10 use aliased 'SQL::Translator::Object::PrimaryKey';
11 use aliased 'SQL::Translator::Object::Procedure';
12 use aliased 'SQL::Translator::Object::Schema';
13 use aliased 'SQL::Translator::Object::Table';
14 use aliased 'SQL::Translator::Object::View';
16 around _build_data_type_mapping {
17 my $data_type_mapping = $self->$orig;
18 $data_type_mapping->{date} = SQL_DATE();
20 return $data_type_mapping;
23 multi method parse(Schema $data) { $data }
25 multi method parse(Str $data) {
26 my $parser = Parse::RecDescent->new($self->grammar);
28 unless (defined $parser) {
29 return $self->error("Error instantiating Parse::RecDescent ".
30 "instance: Bad grammar");
33 my $translator = $self->translator;
35 my $parser_version = $translator->has_parser_args
36 ? $translator->engine_version($translator->parser_args->{mysql_parser_version}, 'mysql') || DEFAULT_PARSER_VERSION
37 : DEFAULT_PARSER_VERSION;
39 while ($data =~ s#/\*!(\d{5})?(.*?)\*/#($1 && $1 > $parser_version ? '' : $2)#es) { }
41 my $result = $parser->startrule($data);
42 die "Parse failed" unless defined $result;
44 my $schema = $translator->schema;
45 $schema->name($result->{'database_name'}) if $result->{'database_name'};
47 my @tables = sort { $result->{'tables'}{ $a }{'order'} <=> $result->{'tables'}{ $b }{'order'} } keys %{ $result->{'tables'} };
49 for my $table_name ( @tables ) {
50 my $tdata = $result->{tables}{ $table_name };
51 my $table = Table->new({ name => $tdata->{table_name}, schema => $schema });
52 $schema->add_table($table);
53 $table->comments( join "\n", @{$tdata->{comments}} ) if $tdata->{comments};
55 my @fields = sort { $tdata->{'fields'}->{$a}->{'order'} <=> $tdata->{'fields'}->{$b}->{'order'} } keys %{ $tdata->{'fields'} };
57 for my $fname ( @fields ) {
58 my $fdata = $tdata->{fields}{ $fname };
59 my $field = Column->new({
60 name => $fdata->{name},
61 data_type => $fdata->{data_type},
62 sql_data_type => $self->data_type_mapping->{$fdata->{data_type}} || -999999,
63 size => $fdata->{size},
64 default_value => $fdata->{default},
65 is_auto_increment => $fdata->{is_auto_inc},
66 is_nullable => $fdata->{null},
67 is_primary_key => $fdata->{is_primary_key} ? 1 : 0,
70 $field->comments($fdata->{comments});
71 $table->add_column($field);
73 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
76 for my $qual ( qw[ binary unsigned zerofill list collate ],
77 'character set', 'on update' ) {
78 if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
79 next if ref $val eq 'ARRAY' && !@$val;
81 #$field->extra( $qual, $val );
84 $field->extra(\%extra);
86 if ( $fdata->{has_index} ) {
87 my $index = Index->new({ name => '', type => 'NORMAL', table => $table });
88 $index->add_column($table->get_column($fdata->{name}));
89 $table->add_index($index);
92 if ( $fdata->{is_unique} ) {
93 push @{ $tdata->{constraints} }, { name => '', type => 'UNIQUE', fields => [ $fdata->{name} ] };
96 for my $cdata ( @{ $fdata->{constraints} } ) {
97 next unless lc $cdata->{type} eq 'foreign_key';
98 $cdata->{fields} ||= [ $field->name ];
99 push @{ $tdata->{constraints} }, $cdata;
103 for my $idata ( @{ $tdata->{indices} || [] } ) {
104 my $index = Index->new({ name => $idata->{name} || '', type => uc($idata->{type}), table => $table });
105 map { $index->add_column($table->get_column($_)) } @{$idata->{fields}};
106 $table->add_index($index);
110 if ( my @options = @{ $tdata->{'table_options'} || [] } ) {
112 my @ignore_opts = $translator->has_parser_args && $translator->parser_args->{'ignore_opts'}
113 ? split( /,/, $translator->parser_args->{'ignore_opts'} )
116 my $ignores = { map { $_ => 1 } @ignore_opts };
117 foreach my $option (@options) {
118 # make sure the option isn't in ignore list
119 my ($option_key) = keys %$option;
120 if ( !exists $ignores->{$option_key} ) {
121 push @cleaned_options, $option;
125 @cleaned_options = @options;
127 $table->options( \@cleaned_options ); # or die $table->error;
130 for my $cdata ( @{ $tdata->{constraints} || [] } ) {
132 if (uc $cdata->{type} eq 'PRIMARY_KEY') {
133 $constraint = PrimaryKey->new({ name => $cdata->{name} || '', table => $table });
134 $table->get_column($_)->is_primary_key(1) for @{$cdata->{fields}};
135 } elsif (uc $cdata->{type} eq 'FOREIGN_KEY') {
136 $constraint = ForeignKey->new({ name => $cdata->{name} || '',
138 reference_table => $cdata->{reference_table},
139 defined $cdata->{reference_fields} ? (reference_columns => $cdata->{reference_fields}) : (),
140 on_delete => $cdata->{on_delete} || $cdata->{on_delete_do},
141 on_update => $cdata->{on_update} || $cdata->{on_update_do} });
142 $table->get_column($_)->is_foreign_key(1) for @{$cdata->{fields}};
143 $table->get_column($_)->foreign_key_reference($constraint) for @{$cdata->{fields}};
145 $constraint = Constraint->new({ name => $cdata->{name} || '', type => uc $cdata->{type}, table => $table });
147 $constraint->add_column($table->get_column($_)) for @{$cdata->{fields}};
148 $table->add_constraint($constraint);
152 for my $proc_name ( keys %{ $result->{procedures} } ) {
153 my $procedure = Procedure->new({ name => $proc_name,
154 owner => $result->{procedures}->{$proc_name}->{owner},
155 sql => $result->{procedures}->{$proc_name}->{sql}
157 $schema->add_procedure($procedure);
160 for my $view_name ( keys %{ $result->{'views'} } ) {
161 my $view = View->new({
163 sql => $result->{'views'}->{$view_name}->{sql},
165 $schema->add_view($view);