commit : /commit/i SEMICOLON
-drop : /drop/i TABLE <commit> table_name SEMICOLON
+drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON
+
+tbl_drop: TABLE <commit> table_name
+
+view_drop: VIEW if_exists(?) view_name
+
+trg_drop: TRIGGER if_exists(?) trigger_name
comment : /^\s*(?:#|-{2}).*\n/
{
definition : constraint_def | column_def
-column_def: NAME type(?) column_constraint(s?)
+column_def: comment(s?) NAME type(?) column_constraint(s?)
{
my $column = {
supertype => 'column',
- name => $item[1],
- data_type => $item[2][0]->{'type'},
- size => $item[2][0]->{'size'},
+ name => $item[2],
+ data_type => $item[3][0]->{'type'},
+ size => $item[3][0]->{'size'},
is_nullable => 1,
is_primary_key => 0,
is_unique => 0,
check => '',
default => undef,
- constraints => $item[3],
+ constraints => $item[4],
+ comments => $item[1],
};
- for my $c ( @{ $item[3] } ) {
+
+ for my $c ( @{ $item[4] } ) {
if ( $c->{'type'} eq 'not_null' ) {
$column->{'is_nullable'} = 0;
}
instead_of : /instead of/i
+if_exists : /if exists/i
+
view_name : qualified_name
+trigger_name : qualified_name
+
#
# Create View
#
$DEBUG = 0 unless defined $DEBUG;
$WARN = 0 unless defined $WARN;
-our %used_identifiers = ();
our $max_id_length = 30;
-our %global_names;
-our %truncated;
+my %global_names;
sub produce {
my $translator = shift;
debug("PKG: Beginning production\n");
+ %global_names = (); #reset
+
my @create = ();
push @create, header_comment unless ($no_comments);
$create[0] .= "\n\nBEGIN TRANSACTION" unless $no_txn;
# -------------------------------------------------------------------
sub mk_name {
- my ($basename, $type, $scope, $critical) = @_;
- my $basename_orig = $basename;
- my $max_name = !$max_id_length
- ? length($type) + 1
- : $type
- ? $max_id_length - (length($type) + 1)
- : $max_id_length;
- $basename = substr( $basename, 0, $max_name )
- if length( $basename ) > $max_name;
- $basename =~ s/\./_/g;
- my $name = $type ? "${type}_$basename" : $basename;
-
- if ( $basename ne $basename_orig and $critical ) {
- my $show_type = $type ? "+'$type'" : "";
- warn "Truncating '$basename_orig'$show_type to $max_id_length ",
- "character limit to make '$name'\n" if $WARN;
- $truncated{ $basename_orig } = $name;
- }
+ my ($name, $scope, $critical) = @_;
$scope ||= \%global_names;
if ( my $prev = $scope->{ $name } ) {
my ($index, $options) = @_;
my $name = $index->name;
- $name = mk_name($index->table->name, $name);
+ $name = mk_name($name);
my $type = $index->type eq 'UNIQUE' ? "UNIQUE " : '';
my ($c, $options) = @_;
my $name = $c->name;
- $name = mk_name($c->table->name, $name);
+ $name = mk_name($name);
my @fields = $c->fields;
(my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;