package SQL::Translator::Parser::MySQL;
# -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.52 2006-02-22 22:54:12 kycl4rk Exp $
+# $Id: MySQL.pm,v 1.54 2006-06-09 13:56:58 schiffbruechige Exp $
# -------------------------------------------------------------------
# Copyright (C) 2002-4 SQLFairy Authors
#
use strict;
use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.52 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.54 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
use Data::Dumper;
$::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
$::RD_HINT = 1; # Give out hints to help fix problems.
-$GRAMMAR = q!
+$GRAMMAR = << 'END_OF_GRAMMAR';
{
my ( $database_name, %tables, $table_order, @table_comments );
drop : /drop/i WORD(s) ';'
{ @table_comments = () }
-insert : /insert/i /[^;]+/ ';'
+string :
+ # MySQL strings, unlike common SQL strings, can be double-quoted or
+ # single-quoted, and you can escape the delmiters by doubling (but only the
+ # delimiter) or by backslashing.
+
+ /'(\\.|''|[^\\\'])*'/ |
+ /"(\\.|""|[^\\\"])*"/
+ # For reference, std sql str: /(?:(?:\')(?:[^\']*(?:(?:\'\')[^\']*)*)(?:\'))//
+
+nonstring : /[^;\'"]+/
+
+statement_body : (string | nonstring)(s?)
+
+insert : /insert/i statement_body ';'
alter : ALTER TABLE table_name alter_specification(s /,/) ';'
{
}
}
+field_qualifier : /unique/i KEY(?)
+ {
+ $return = {
+ is_unique => 1,
+ }
+ }
+
+field_qualifier : KEY
+ {
+ $return = {
+ has_index => 1,
+ }
+ }
+
reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete(?) on_update(?)
{
$return = {
| /now\(\)/i
{ 'CURRENT_TIMESTAMP' }
-!;
+END_OF_GRAMMAR
# -------------------------------------------------------------------
sub parse {
}
}
+ if ( $fdata->{'has_index'} ) {
+ $table->add_index(
+ name => '',
+ type => 'NORMAL',
+ fields => $fdata->{'name'},
+ ) or die $table->error;
+ }
+
+ if ( $fdata->{'is_unique'} ) {
+ $table->add_constraint(
+ name => '',
+ type => 'UNIQUE',
+ fields => $fdata->{'name'},
+ ) or die $table->error;
+ }
+
if ( $field->data_type =~ /(set|enum)/i && !$field->size ) {
my %extra = $field->extra;
my $longest = 0;