| create_index
| create_constraint
| comment
+ | disable_constraints
| drop
| use
| setuser
object_not_null : /object_id/i '(' ident ')' /is not null/i
+field_not_null : /where/i field_name /is \s+ not \s+ null/ix
+
print : /\s*/ /print/i /.*/
else : /else/i /.*/
drop : if_exists(?) /drop/i tbl_drop END_STATEMENT
-tbl_drop : /table/i NAME
+tbl_drop : /table/i ident
if_exists : /if exists/i '(' /select/i 'name' /from/i 'sysobjects' /[^\)]+/ ')'
}
}
+disable_constraints : if_exists(?) /alter/i /table/i ident /nocheck/i /constraint/i /all/i END_STATEMENT
+
+# this is for the normal case
+create_constraint : /create/i constraint END_STATEMENT
+ {
+ @table_comments = ();
+ push @{ $tables{ $item[2]{'table'} }{'constraints'} }, $item[2];
+ }
+
+# and this is for the BEGIN/END case
create_constraint : /create/i constraint
{
@table_comments = ();
push @{ $tables{ $item[2]{'table'} }{'constraints'} }, $item[2];
}
+
create_constraint : /alter/i /table/i ident /add/i foreign_key_constraint END_STATEMENT
{
push @{ $tables{ $item[3]{name} }{constraints} }, $item[5];
}
+
create_index : /create/i index
{
@table_comments = ();
| unique_constraint
field_name : WORD
+ { $return = $item[1] }
+ | LQUOTE WORD RQUOTE
+ { $return = $item[2] }
index_name : WORD
+ { $return = $item[1] }
+ | LQUOTE WORD RQUOTE
+ { $return = $item[2] }
table_name : WORD
+ { $return = $item[1] }
+ | LQUOTE WORD RQUOTE
+ { $return = $item[2] }
data_type : WORD field_size(?)
{
}
}
-unique_constraint : /unique/i clustered(?) INDEX(?) index_name(?) on_table(?) parens_field_list
+unique_constraint : /unique/i clustered(?) INDEX(?) index_name(?) on_table(?) parens_field_list field_not_null(?)
{
$return = {
supertype => 'constraint',
parens_field_list : '(' field_name(s /,/) ')'
{ $item[2] }
-ident : QUOTE(?) WORD '.' WORD QUOTE(?)
+ident : QUOTE WORD '.' WORD QUOTE | LQUOTE WORD '.' WORD RQUOTE
{ $return = { owner => $item[2], name => $item[4] } }
+ | LQUOTE WORD RQUOTE '.' LQUOTE WORD RQUOTE
+ { $return = { owner => $item[2], name => $item[6] } }
+ | LQUOTE WORD RQUOTE
+ { $return = { name => $item[2] } }
+ | WORD '.' WORD
+ { $return = { owner => $item[1], name => $item[3] } }
| WORD
{ $return = { name => $item[1] } }
QUOTE : /'/
+LQUOTE : '['
+
+RQUOTE : ']'
+
};
# -------------------------------------------------------------------
#bit => 'bit',
#tinyint => 'smallint',
#float => 'double precision',
- #serial => 'numeric',
+ #serial => 'numeric',
#boolean => 'varchar',
#char => 'char',
#long => 'varchar',
# TODO - This is still the Sybase list!
my %reserved = map { $_, 1 } qw[
- ALL ANALYSE ANALYZE AND ANY AS ASC
+ ALL ANALYSE ANALYZE AND ANY AS ASC
BETWEEN BINARY BOTH
CASE CAST CHECK COLLATE COLUMN CONSTRAINT CROSS
- CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER
+ CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER
DEFAULT DEFERRABLE DESC DISTINCT DO
ELSE END EXCEPT
- FALSE FOR FOREIGN FREEZE FROM FULL
- GROUP HAVING
- ILIKE IN INITIALLY INNER INTERSECT INTO IS ISNULL
- JOIN LEADING LEFT LIKE LIMIT
+ FALSE FOR FOREIGN FREEZE FROM FULL
+ GROUP HAVING
+ ILIKE IN INITIALLY INNER INTERSECT INTO IS ISNULL
+ JOIN LEADING LEFT LIKE LIMIT
NATURAL NEW NOT NOTNULL NULL
OFF OFFSET OLD ON ONLY OR ORDER OUTER OVERLAPS
- PRIMARY PUBLIC REFERENCES RIGHT
- SELECT SESSION_USER SOME TABLE THEN TO TRAILING TRUE
+ PRIMARY PUBLIC REFERENCES RIGHT
+ SELECT SESSION_USER SOME TABLE THEN TO TRAILING TRUE
UNION UNIQUE USER USING VERBOSE WHEN WHERE
];
],
);
- push @field_defs, $field_def;
+ push @field_defs, $field_def;
}
#
undef $_ if $_ eq 'RESTRICT'
}
- $c_def =
+ $c_def =
"ALTER TABLE $table_name ADD CONSTRAINT $name FOREIGN KEY".
' (' . join( ', ', @fields ) . ') REFERENCES '.
$constraint->reference_table.
if ( $type eq PRIMARY_KEY ) {
- $name ||= mk_name( $table_name . '_pk' );
- $c_def =
+ $name = ($name ? unreserve($name) : mk_name( $table_name . '_pk' ));
+ $c_def =
"CONSTRAINT $name PRIMARY KEY ".
'(' . join( ', ', @fields ) . ')';
}
elsif ( $type eq UNIQUE ) {
$name ||= mk_name( $table_name . '_uc' );
- $c_def =
+ $c_def =
"CONSTRAINT $name UNIQUE " .
'(' . join( ', ', @fields ) . ')';
}
my $create_statement = "";
$create_statement .= qq[CREATE TABLE $table_name_ur (\n].
- join( ",\n",
+ join( ",\n",
map { " $_" } @field_defs, @constraint_defs
).
"\n);"
$output .= "\n\n";
$output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
my $text = $_->sql();
- $text =~ s/\r//g;
+ $text =~ s/\r//g;
$output .= "$text\nGO\n";
}
=cut
if ( my $prev = $scope->{ $name } ) {
my $name_orig = $name;
$name .= sprintf( "%02d", ++$prev );
- substr($name, $max_id_length - 3) = "00"
+ substr($name, $max_id_length - 3) = "00"
if length( $name ) > $max_id_length;
warn "The name '$name_orig' has been changed to ",
$scope->{ $name_orig }++;
}
- $name = substr( $name, 0, $max_id_length )
+ $name = substr( $name, 0, $max_id_length )
if ((length( $name ) > $max_id_length) && $critical);
$scope->{ $name }++;
return $name;
my ( $suffix ) = ( $name =~ s/(\W.*)$// ) ? $1 : '';
# also trap fields that don't begin with a letter
- return $name if !$reserved{ uc $name } && $name =~ /^[a-z]/i;
+ return $name if !$reserved{ uc $name } && $name =~ /^[a-z]/i;
my $unreserve = sprintf '%s_', $name;
return $unreserve.$suffix;