More work on default field sizes for numerics.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
CommitLineData
16dc9970 1package SQL::Translator::Parser::MySQL;
2
49e1eb70 3# -------------------------------------------------------------------
c736c39c 4# $Id: MySQL.pm,v 1.21 2003-06-03 22:42:10 kycl4rk Exp $
49e1eb70 5# -------------------------------------------------------------------
abfa405a 6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>,
8# Chris Mungall <cjm@fruitfly.org>
077ebf34 9#
10# This program is free software; you can redistribute it and/or
11# modify it under the terms of the GNU General Public License as
12# published by the Free Software Foundation; version 2.
13#
14# This program is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22# 02111-1307 USA
23# -------------------------------------------------------------------
16dc9970 24
d529894e 25=head1 NAME
26
27SQL::Translator::Parser::MySQL - parser for MySQL
28
29=head1 SYNOPSIS
30
31 use SQL::Translator;
32 use SQL::Translator::Parser::MySQL;
33
34 my $translator = SQL::Translator->new;
35 $translator->parser("SQL::Translator::Parser::MySQL");
36
37=head1 DESCRIPTION
38
39The grammar is influenced heavily by Tim Bunce's "mysql2ora" grammar.
40
629b76f9 41Here's the word from the MySQL site
42(http://www.mysql.com/doc/en/CREATE_TABLE.html):
43
44 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
45 [table_options] [select_statement]
46
47 or
48
49 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name LIKE old_table_name;
50
51 create_definition:
52 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
53 [PRIMARY KEY] [reference_definition]
54 or PRIMARY KEY (index_col_name,...)
55 or KEY [index_name] (index_col_name,...)
56 or INDEX [index_name] (index_col_name,...)
57 or UNIQUE [INDEX] [index_name] (index_col_name,...)
58 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
59 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
60 [reference_definition]
61 or CHECK (expr)
62
63 type:
64 TINYINT[(length)] [UNSIGNED] [ZEROFILL]
65 or SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
66 or MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
67 or INT[(length)] [UNSIGNED] [ZEROFILL]
68 or INTEGER[(length)] [UNSIGNED] [ZEROFILL]
69 or BIGINT[(length)] [UNSIGNED] [ZEROFILL]
70 or REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
71 or DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
72 or FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
73 or DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]
74 or NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]
75 or CHAR(length) [BINARY]
76 or VARCHAR(length) [BINARY]
77 or DATE
78 or TIME
79 or TIMESTAMP
80 or DATETIME
81 or TINYBLOB
82 or BLOB
83 or MEDIUMBLOB
84 or LONGBLOB
85 or TINYTEXT
86 or TEXT
87 or MEDIUMTEXT
88 or LONGTEXT
89 or ENUM(value1,value2,value3,...)
90 or SET(value1,value2,value3,...)
91
92 index_col_name:
93 col_name [(length)]
94
95 reference_definition:
96 REFERENCES tbl_name [(index_col_name,...)]
97 [MATCH FULL | MATCH PARTIAL]
98 [ON DELETE reference_option]
99 [ON UPDATE reference_option]
100
101 reference_option:
102 RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
103
104 table_options:
105 TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
106 or AUTO_INCREMENT = #
107 or AVG_ROW_LENGTH = #
108 or CHECKSUM = {0 | 1}
109 or COMMENT = "string"
110 or MAX_ROWS = #
111 or MIN_ROWS = #
112 or PACK_KEYS = {0 | 1 | DEFAULT}
113 or PASSWORD = "string"
114 or DELAY_KEY_WRITE = {0 | 1}
115 or ROW_FORMAT= { default | dynamic | fixed | compressed }
116 or RAID_TYPE= {1 | STRIPED | RAID0 } RAID_CHUNKS=# RAID_CHUNKSIZE=#
117 or UNION = (table_name,[table_name...])
118 or INSERT_METHOD= {NO | FIRST | LAST }
119 or DATA DIRECTORY="absolute path to directory"
120 or INDEX DIRECTORY="absolute path to directory"
121
d529894e 122=cut
123
16dc9970 124use strict;
d529894e 125use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
c736c39c 126$VERSION = sprintf "%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/;
8d0f3086 127$DEBUG = 0 unless defined $DEBUG;
077ebf34 128
d529894e 129use Data::Dumper;
077ebf34 130use Parse::RecDescent;
131use Exporter;
132use base qw(Exporter);
133
134@EXPORT_OK = qw(parse);
135
d529894e 136# Enable warnings within the Parse::RecDescent module.
137$::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
138$::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
139$::RD_HINT = 1; # Give out hints to help fix problems.
140
d529894e 141$GRAMMAR = q!
142
8ccdeb42 143{
144 our ( %tables, $table_order );
145}
d529894e 146
629b76f9 147#
148# The "eofile" rule makes the parser fail if any "statement" rule
149# fails. Otherwise, the first successful match by a "statement"
150# won't cause the failure needed to know that the parse, as a whole,
151# failed. -ky
152#
153startrule : statement(s) eofile { \%tables }
154
155eofile : /^\Z/
d529894e 156
157statement : comment
61745327 158 | drop
d529894e 159 | create
160 | <error>
161
61745327 162drop : /drop/i WORD(s) ';'
163
40c1ade1 164create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
d529894e 165 {
166 my $table_name = $item{'table_name'};
167 $tables{ $table_name }{'order'} = ++$table_order;
168 $tables{ $table_name }{'table_name'} = $table_name;
169
61745327 170 my $i = 1;
40c1ade1 171 for my $definition ( @{ $item[7] } ) {
d529894e 172 if ( $definition->{'type'} eq 'field' ) {
173 my $field_name = $definition->{'name'};
174 $tables{ $table_name }{'fields'}{ $field_name } =
175 { %$definition, order => $i };
176 $i++;
177
178 if ( $definition->{'is_primary_key'} ) {
179 push @{ $tables{ $table_name }{'indices'} },
180 {
181 type => 'primary_key',
182 fields => [ $field_name ],
16dc9970 183 }
d529894e 184 ;
185 }
dd2ef5ae 186 }
40c1ade1 187 elsif ( $definition->{'type'} eq 'foreign_key' ) {
188 for my $field ( @{ $definition->{'fields'} } ) {
189 push @{
190 $tables{$table_name}{'fields'}{$field}{'constraints'}
191 },
192 $definition;
193 }
194 }
d529894e 195 else {
196 push @{ $tables{ $table_name }{'indices'} },
197 $definition;
dd2ef5ae 198 }
d529894e 199 }
dd2ef5ae 200
58a88238 201 for my $opt ( @{ $item{'table_option(s?)'} } ) {
d529894e 202 if ( my ( $key, $val ) = each %$opt ) {
203 $tables{ $table_name }{'table_options'}{ $key } = $val;
dd2ef5ae 204 }
d529894e 205 }
58a88238 206
207 1;
d529894e 208 }
dd2ef5ae 209
40c1ade1 210opt_if_not_exists : /if not exists/i
211
d529894e 212create : /CREATE/i unique(?) /(INDEX|KEY)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
213 {
214 push @{ $tables{ $item{'table_name'} }{'indices'} },
215 {
216 name => $item[4],
217 type => $item[2] ? 'unique' : 'normal',
218 fields => $item[8],
dd2ef5ae 219 }
d529894e 220 ;
221 }
dd2ef5ae 222
d529894e 223create_definition : index
40c1ade1 224 | foreign_key
d529894e 225 | field
226 | <error>
227
228comment : /^\s*(?:#|-{2}).*\n/
229
230blank : /\s*/
231
658637cd 232field : field_name data_type field_qualifier(s?) reference_definition(?)
d529894e 233 {
58a88238 234 my %qualifiers = map { %$_ } @{ $item{'field_qualifier(s?)'} || [] };
d529894e 235 my $null = defined $item{'not_null'} ? $item{'not_null'} : 1;
236 delete $qualifiers{'not_null'};
237 if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) {
238 $qualifiers{ $_ } = 1 for @type_quals;
239 }
240
241 $return = {
242 type => 'field',
243 name => $item{'field_name'},
244 data_type => $item{'data_type'}{'type'},
245 size => $item{'data_type'}{'size'},
246 list => $item{'data_type'}{'list'},
247 null => $null,
58a88238 248 constraints => $item{'reference_definition(?)'},
d529894e 249 %qualifiers,
250 }
251 }
252 | <error>
dd2ef5ae 253
d529894e 254field_qualifier : not_null
255 {
256 $return = {
257 null => $item{'not_null'},
258 }
259 }
16dc9970 260
d529894e 261field_qualifier : default_val
262 {
263 $return = {
264 default => $item{'default_val'},
265 }
266 }
16dc9970 267
d529894e 268field_qualifier : auto_inc
269 {
270 $return = {
271 is_auto_inc => $item{'auto_inc'},
272 }
273 }
16dc9970 274
d529894e 275field_qualifier : primary_key
276 {
277 $return = {
278 is_primary_key => $item{'primary_key'},
279 }
280 }
16dc9970 281
d529894e 282field_qualifier : unsigned
283 {
284 $return = {
285 is_unsigned => $item{'unsigned'},
286 }
287 }
16dc9970 288
658637cd 289reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete_do(?) on_update_do(?)
290 {
40c1ade1 291 $return = {
658637cd 292 type => 'foreign_key',
293 reference_table => $item[2],
294 reference_fields => $item[3][0],
295 match_type => $item[4][0],
296 on_delete_do => $item[5][0],
297 on_update_do => $item[6][0],
298 }
299 }
300
40c1ade1 301
658637cd 302match_type : /match full/i { 'match_full' }
303 |
304 /match partial/i { 'match_partial' }
305
306on_delete_do : /on delete/i reference_option
307 { $item[2] }
308
309on_update_do : /on update/i reference_option
310 { $item[2] }
311
312reference_option: /restrict/i |
313 /cascade/i |
314 /set null/i |
315 /no action/i |
316 /set default/i
317 { $item[1] }
318
d529894e 319index : primary_key_index
320 | unique_index
371f5f88 321 | fulltext_index
d529894e 322 | normal_index
58a88238 323 | <error>
d529894e 324
325table_name : WORD
326
327field_name : WORD
328
329index_name : WORD
330
331data_type : WORD parens_value_list(s?) type_qualifier(s?)
332 {
333 my $type = $item[1];
334 my $size; # field size, applicable only to non-set fields
335 my $list; # set list, applicable only to sets (duh)
336
44fcd0b5 337 if ( uc($type) =~ /^(SET|ENUM)$/ ) {
d529894e 338 $size = undef;
339 $list = $item[2][0];
340 }
341 else {
342 $size = $item[2][0];
343 $list = [];
344 }
345
6333c482 346 unless ( @{ $size || [] } ) {
347 if ( lc $type eq 'tinyint' ) {
348 $size = [4];
349 }
350 elsif ( lc $type eq 'smallint' ) {
351 $size = [6];
352 }
353 elsif ( lc $type eq 'mediumint' ) {
354 $size = [9];
355 }
c736c39c 356 elsif ( $type =~ /^int(eger)?$/ ) {
6333c482 357 $size = [11];
358 }
359 elsif ( lc $type eq 'bigint' ) {
360 $size = [20];
361 }
c736c39c 362 elsif ( lc $type =~ /(float|double|decimal|numeric|real)/ ) {
6333c482 363 $size = [8,2];
364 }
365 }
366
d529894e 367 $return = {
368 type => $type,
369 size => $size,
370 list => $list,
371 qualifiers => $item[3],
372 }
373 }
16dc9970 374
658637cd 375parens_field_list : '(' field_name(s /,/) ')'
376 { $item[2] }
377
d529894e 378parens_value_list : '(' VALUE(s /,/) ')'
379 { $item[2] }
16dc9970 380
d529894e 381type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
382 { lc $item[1] }
16dc9970 383
d529894e 384field_type : WORD
16dc9970 385
d529894e 386create_index : /create/i /index/i
dd2ef5ae 387
d529894e 388not_null : /not/i /null/i { $return = 0 }
16dc9970 389
d529894e 390unsigned : /unsigned/i { $return = 0 }
16dc9970 391
be019aae 392default_val : /default/i /(?:')?[\w\d:.-]*(?:')?/
d529894e 393 {
394 $item[2] =~ s/'//g;
395 $return = $item[2];
396 }
16dc9970 397
d529894e 398auto_inc : /auto_increment/i { 1 }
16dc9970 399
d529894e 400primary_key : /primary/i /key/i { 1 }
16dc9970 401
40c1ade1 402foreign_key : opt_constraint(?) /foreign key/i WORD(?) parens_field_list reference_definition
403 {
404 $return = {
405 type => 'foreign_key',
406 name => $item[3][0],
407 fields => $item[4],
408 %{ $item{'reference_definition'} },
409 }
410 }
411
412opt_constraint : /constraint/i WORD
413
d529894e 414primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')'
415 {
416 $return = {
58a88238 417 name => $item{'index_name(?)'}[0],
d529894e 418 type => 'primary_key',
419 fields => $item[4],
58a88238 420 };
d529894e 421 }
16dc9970 422
d529894e 423normal_index : key index_name(?) '(' name_with_opt_paren(s /,/) ')'
424 {
425 $return = {
58a88238 426 name => $item{'index_name(?)'}[0],
d529894e 427 type => 'normal',
428 fields => $item[4],
429 }
430 }
16dc9970 431
d529894e 432unique_index : unique key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
433 {
434 $return = {
58a88238 435 name => $item{'index_name(?)'}[0],
d529894e 436 type => 'unique',
437 fields => $item[5],
438 }
439 }
16dc9970 440
371f5f88 441fulltext_index : fulltext key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
442 {
443 $return = {
58a88238 444 name => $item{'index_name(?)'}[0],
371f5f88 445 type => 'fulltext',
446 fields => $item[5],
447 }
448 }
449
d529894e 450name_with_opt_paren : NAME parens_value_list(s?)
451 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
16dc9970 452
371f5f88 453fulltext : /fulltext/i { 1 }
454
d529894e 455unique : /unique/i { 1 }
16dc9970 456
d529894e 457key : /key/i | /index/i
16dc9970 458
44fcd0b5 459table_option : /[^\s;]*/
d529894e 460 {
461 $return = { split /=/, $item[1] }
462 }
16dc9970 463
40c1ade1 464CREATE : /create/i
465
466TEMPORARY : /temporary/i
467
468TABLE : /table/i
469
d529894e 470WORD : /\w+/
16dc9970 471
d529894e 472DIGITS : /\d+/
16dc9970 473
d529894e 474COMMA : ','
16dc9970 475
d529894e 476NAME : "`" /\w+/ "`"
477 { $item[2] }
478 | /\w+/
479 { $item[1] }
16dc9970 480
d529894e 481VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
482 { $item[1] }
483 | /'.*?'/ # XXX doesn't handle embedded quotes
484 { $item[1] }
485 | /NULL/
486 { 'NULL' }
16dc9970 487
d529894e 488!;
16dc9970 489
d529894e 490# -------------------------------------------------------------------
491sub parse {
70944bc5 492 my ( $translator, $data ) = @_;
40c1ade1 493 my $parser = Parse::RecDescent->new($GRAMMAR);
077ebf34 494
e099bee9 495 local $::RD_TRACE = $translator->trace ? 1 : undef;
496 local $DEBUG = $translator->debug;
d529894e 497
498 unless (defined $parser) {
499 return $translator->error("Error instantiating Parse::RecDescent ".
500 "instance: Bad grammer");
501 }
502
503 my $result = $parser->startrule($data);
40c1ade1 504 return $translator->error( "Parse failed." ) unless defined $result;
8ccdeb42 505 warn Dumper( $result ) if $DEBUG;
506
70944bc5 507 my $schema = $translator->schema;
8ccdeb42 508 for my $table_name ( keys %{ $result } ) {
509 my $tdata = $result->{ $table_name };
510 my $table = $schema->add_table(
511 name => $tdata->{'table_name'},
40c1ade1 512 ) or die $schema->error;
8ccdeb42 513
514 my @fields = sort {
515 $tdata->{'fields'}->{$a}->{'order'}
516 <=>
517 $tdata->{'fields'}->{$b}->{'order'}
518 } keys %{ $tdata->{'fields'} };
519
520 for my $fname ( @fields ) {
521 my $fdata = $tdata->{'fields'}{ $fname };
522 my $field = $table->add_field(
523 name => $fdata->{'name'},
524 data_type => $fdata->{'data_type'},
525 size => $fdata->{'size'},
526 default_value => $fdata->{'default'},
527 is_auto_increment => $fdata->{'is_auto_inc'},
528 is_nullable => $fdata->{'null'},
40c1ade1 529 ) or die $table->error;
8ccdeb42 530 }
531 }
532
d529894e 533 return $result;
534}
535
5361;
537
8ccdeb42 538# ----------------------------------------------------
d529894e 539# Where man is not nature is barren.
540# William Blake
8ccdeb42 541# ----------------------------------------------------
16dc9970 542
d529894e 543=pod
16dc9970 544
545=head1 AUTHOR
546
d529894e 547Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
8ccdeb42 548Chris Mungall E<lt>cjm@fruitfly.orgE<gt>.
16dc9970 549
550=head1 SEE ALSO
551
8ccdeb42 552perl(1), Parse::RecDescent, SQL::Translator::Schema.
16dc9970 553
554=cut