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::Schema';
12 use aliased 'SQL::Translator::Object::Table';
14 around _build_data_type_mapping {
15 my $data_type_mapping = $self->$orig;
16 $data_type_mapping->{date} = SQL_DATE();
18 return $data_type_mapping;
21 multi method parse(Str $data) {
25 # open DUMP_FILE, $self->filename;
26 # my $data = do { local $/; <DUMP_FILE>; };
30 my $parser = Parse::RecDescent->new($self->grammar);
32 # local $::RD_TRACE = undef; #$self->trace ? 1 : undef;
34 unless (defined $parser) {
35 return $self->error("Error instantiating Parse::RecDescent ".
36 "instance: Bad grammar");
39 # my $parser_version = parse_mysql_version(
40 # $translator->parser_args->{mysql_parser_version}, 'mysql'
41 # ) || DEFAULT_PARSER_VERSION;
42 my $parser_version = 30000;
44 while ($data =~ s#/\*!(\d{5})?(.*?)\*/#($1 && $1 > $parser_version ? '' : $2)#es) { }
46 my $result = $parser->startrule($data);
47 # return $self->error( "Parse failed." ) unless defined $result;
48 die "Parse failed" unless defined $result;
49 warn "Parse result:".Dumper( $result ) if $DEBUG;
51 my $schema = Schema->new;
52 $schema->name($result->{'database_name'}) if $result->{'database_name'};
54 my @tables = sort { $result->{'tables'}{ $a }{'order'} <=> $result->{'tables'}{ $b }{'order'} } keys %{ $result->{'tables'} };
56 for my $table_name ( @tables ) {
57 my $tdata = $result->{tables}{ $table_name };
58 my $table = Table->new({ name => $tdata->{table_name} });
59 $schema->add_table($table);
60 $table->comments( join "\n", @{$tdata->{comments}} ) if $tdata->{comments};
62 my @fields = sort { $tdata->{'fields'}->{$a}->{'order'} <=> $tdata->{'fields'}->{$b}->{'order'} } keys %{ $tdata->{'fields'} };
64 for my $fname ( @fields ) {
65 my $fdata = $tdata->{fields}{ $fname };
66 my $field = Column->new({
67 name => $fdata->{name},
68 data_type => $fdata->{data_type},
69 sql_data_type => $self->data_type_mapping->{$fdata->{data_type}} || -999999,
70 size => $fdata->{size},
71 default_value => $fdata->{default},
72 is_auto_increment => $fdata->{is_auto_inc},
73 is_nullable => $fdata->{null},
74 is_primary_key => $fdata->{is_primary_key} ? 1 : 0,
75 comments => (join "\n", @{$fdata->{comments}}) || '',
77 $table->add_column($field);
79 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
82 for my $qual ( qw[ binary unsigned zerofill list collate ],
83 'character set', 'on update' ) {
84 if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
85 next if ref $val eq 'ARRAY' && !@$val;
87 #$field->extra( $qual, $val );
90 $field->extra(\%extra);
92 if ( $fdata->{has_index} ) {
93 my $index = Index->new({ name => '', type => 'NORMAL' });
94 $index->add_column($table->get_column($fdata->{name}));
95 $table->add_index($index);
98 if ( $fdata->{is_unique} ) {
99 push @{ $tdata->{constraints} }, { name => '', type => 'UNIQUE', fields => [ $fdata->{name} ] };
100 # my $constraint = Constraint->new({ name => '', type => 'UNIQUE' });
101 # $constraint->add_column($table->get_column($fdata->{name}));
102 # $table->add_constraint($constraint);
105 for my $cdata ( @{ $fdata->{constraints} } ) {
106 next unless lc $cdata->{type} eq 'foreign_key';
107 $cdata->{fields} ||= [ $field->name ];
108 push @{ $tdata->{constraints} }, $cdata;
112 for my $idata ( @{ $tdata->{indices} || [] } ) {
113 my $index = Index->new({ name => $idata->{name} || '', type => uc($idata->{type}) });
114 map { $index->add_column($table->get_column($_)) } @{$idata->{fields}};
115 $table->add_index($index);
119 # if ( my @options = @{ $tdata->{'table_options'} || [] } ) {
120 # my @cleaned_options;
121 # my @ignore_opts = $self->parser_args->{'ignore_opts'}
122 # ? split( /,/, $self->parser_args->{'ignore_opts'} )
124 # if (@ignore_opts) {
125 # my $ignores = { map { $_ => 1 } @ignore_opts };
126 # foreach my $option (@options) {
127 # # make sure the option isn't in ignore list
128 # my ($option_key) = keys %$option;
129 # if ( !exists $ignores->{$option_key} ) {
130 # push @cleaned_options, $option;
134 # @cleaned_options = @options;
136 # $table->options( \@cleaned_options ) or die $table->error;
139 for my $cdata ( @{ $tdata->{constraints} || [] } ) {
141 if (uc $cdata->{type} eq 'PRIMARY_KEY') {
142 $constraint = PrimaryKey->new({ name => $cdata->{name} || 'primary_key' });
143 map { $constraint->add_column($table->get_column($_)) } @{$cdata->{fields}};
144 $table->get_column($_)->is_primary_key(1) for @{$cdata->{fields}};
145 } elsif (uc $cdata->{type} eq 'FOREIGN_KEY') {
146 $constraint = ForeignKey->new({ name => $cdata->{name} || 'foreign_key',
147 reference_table => $cdata->{reference_table},
148 reference_columns => $cdata->{reference_fields},
149 on_delete => $cdata->{on_delete} || $cdata->{on_delete_do},
150 on_update => $cdata->{on_update} || $cdata->{on_update_do} });
151 $table->get_column($_)->is_foreign_key(1) for @{$cdata->{fields}};
152 $table->get_column($_)->foreign_key_reference($constraint) for @{$cdata->{fields}};
154 $constraint = Constraint->new({ name => $cdata->{name} || 'constraint', type => uc $cdata->{type} });
155 map { $constraint->add_column($table->get_column($_)) } @{$cdata->{fields}};
157 $table->add_constraint($constraint);
159 # my $constraint = $table->add_constraint(
160 # name => $cdata->{'name'},
161 # type => $cdata->{'type'},
162 # fields => $cdata->{'fields'},
163 # reference_table => $cdata->{'reference_table'},
164 # reference_fields => $cdata->{'reference_fields'},
165 # match_type => $cdata->{'match_type'} || '',
166 # on_delete => $cdata->{'on_delete'}
167 # || $cdata->{'on_delete_do'},
168 # on_update => $cdata->{'on_update'}
169 # || $cdata->{'on_update_do'},
170 # ) or die $table->error;
173 # After the constrains and PK/idxs have been created,
174 # we normalize fields
175 normalize_field($_) for $table->get_fields;
178 # my @procedures = sort { $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'} } keys %{ $result->{procedures} };
180 # for my $proc_name ( @procedures ) {
181 # $schema->add_procedure(
182 # name => $proc_name,
183 # owner => $result->{procedures}->{$proc_name}->{owner},
184 # sql => $result->{procedures}->{$proc_name}->{sql},
188 # my @views = sort { $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'} } keys %{ $result->{views} };
190 # for my $view_name ( keys %{ $result->{'views'} } ) {
192 # name => $view_name,
193 # sql => $result->{'views'}->{$view_name}->{sql},
199 # Takes a field, and returns
200 method normalize_field {
201 my ($size, $type, $list, $changed); # = @_;
203 $size = $self->size || 0;
204 $type = $self->data_type;
205 $list = $self->extra->{list} || [];
207 if ( !ref $size && $size == 0 ) {
208 if ( lc $type eq 'tinyint' ) {
209 $changed = $size != 4;
212 elsif ( lc $type eq 'smallint' ) {
213 $changed = $size != 6;
216 elsif ( lc $type eq 'mediumint' ) {
217 $changed = $size != 9;
220 elsif ( $type =~ /^int(eger)?$/i ) {
221 $changed = $size != 11 || $type ne 'int';
225 elsif ( lc $type eq 'bigint' ) {
226 $changed = $size != 20;
229 elsif ( lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ ) {
230 my $old_size = (ref $size || '') eq 'ARRAY' ? $size : [];
231 $changed = @$old_size != 2
232 || $old_size->[0] != 8
233 || $old_size->[1] != 2;
238 if ( $type =~ /^tiny(text|blob)$/i ) {
239 $changed = $size != 255;
242 elsif ( $type =~ /^(blob|text)$/i ) {
243 $changed = $size != 65_535;
246 elsif ( $type =~ /^medium(blob|text)$/i ) {
247 $changed = $size != 16_777_215;
250 elsif ( $type =~ /^long(blob|text)$/i ) {
251 $changed = $size != 4_294_967_295;
252 $size = 4_294_967_295;
255 if ( $type =~ /(set|enum)/i && !$size ) {
256 my %extra = $self->extra;
258 for my $len ( map { length } @{ $extra{'list'} || [] } ) {
259 $longest = $len if $len > $longest;
262 $size = $longest if $longest;
266 # We only want to clone the field, not *everything*
268 # local $field->{table} = undef;
269 # $field->parsed_field( dclone( $field ) );
270 # $field->parsed_field->{table} = $field->table;
272 $self->size( $size );
273 $self->data_type( $type );
274 # $column->sql_data_type( $self->data_type_mapping->{$type} || -99999 );
275 $self->extra->{list} = $list if @$list;