my $max_id_length = 128;
my %global_names;
-my %unreserve;
=pod
my $schema = $translator->schema;
%global_names = (); #reset
- %unreserve = ();
my $output;
$output .= header_comment."\n" unless ($no_comments);
}
# Generate the CREATE sql
+
+ my @foreign_constraints = (); # these need to be added separately, as tables may not exist yet
+
for my $table ( $schema->get_tables ) {
my $table_name = $table->name or next;
my $table_name_ur = unreserve($table_name) || '';
elsif ( $data_type eq 'set' ) {
$data_type .= 'character varying';
}
+ elsif ( grep { $data_type eq $_ } qw/bytea blob clob/ ) {
+ $data_type = 'varbinary';
+ }
else {
if ( defined $translate{ $data_type } ) {
$data_type = $translate{ $data_type };
#
# Default value
#
- my $default = $field->default_value;
- if ( defined $default ) {
- SQL::Translator::Producer->_apply_default_value(
- \$field_def,
- $default,
- [
- 'NULL' => \'NULL',
- ],
- );
- }
+ SQL::Translator::Producer->_apply_default_value(
+ $field,
+ \$field_def,
+ [
+ 'NULL' => \'NULL',
+ ],
+ );
push @field_defs, $field_def;
}
$constraint->reference_fields;
next unless @fields;
- my $c_def;
+ my $c_def;
+ if ( $type eq FOREIGN_KEY ) {
+ $name ||= mk_name( $table_name . '_fk' );
+ my $on_delete = uc ($constraint->on_delete || '');
+ my $on_update = uc ($constraint->on_update || '');
+
+ # The default implicit constraint action in MSSQL is RESTRICT
+ # but you can not specify it explicitly. Go figure :)
+ for ($on_delete, $on_update) {
+ undef $_ if $_ eq 'RESTRICT'
+ }
+
+ $c_def =
+ "ALTER TABLE $table_name ADD CONSTRAINT $name FOREIGN KEY".
+ ' (' . join( ', ', @fields ) . ') REFERENCES '.
+ $constraint->reference_table.
+ ' (' . join( ', ', @rfields ) . ')'
+ ;
+
+ if ( $on_delete && $on_delete ne "NO ACTION") {
+ $c_def .= " ON DELETE $on_delete";
+ }
+ if ( $on_update && $on_update ne "NO ACTION") {
+ $c_def .= " ON UPDATE $on_update";
+ }
+
+ $c_def .= ";";
+
+ push @foreign_constraints, $c_def;
+ next;
+ }
+
+
if ( $type eq PRIMARY_KEY ) {
$name ||= mk_name( $table_name . '_pk' );
$c_def =
"CONSTRAINT $name PRIMARY KEY ".
'(' . join( ', ', @fields ) . ')';
}
- elsif ( $type eq FOREIGN_KEY ) {
- $name ||= mk_name( $table_name . '_fk' );
- $c_def =
- "CONSTRAINT $name FOREIGN KEY".
- ' (' . join( ', ', @fields ) . ') REFERENCES '.
- $constraint->reference_table.
- ' (' . join( ', ', @rfields ) . ')';
- my $on_delete = $constraint->on_delete;
- if ( $on_delete && $on_delete ne "NO ACTION") {
- $c_def .= " ON DELETE $on_delete";
- }
- my $on_update = $constraint->on_update;
- if ( $on_update && $on_update ne "NO ACTION") {
- $c_def .= " ON UPDATE $on_update";
- }
- }
elsif ( $type eq UNIQUE ) {
$name ||= mk_name( $table_name . '_uc' );
$c_def =
);
}
+# Add FK constraints
+ $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
+
# create view/procedure are NOT prepended to the input $sql, needs
# to be filled in with the proper syntax
-=begin
+=pod
# Text of view is already a 'create view' statement so no need to
# be fancy
# also trap fields that don't begin with a letter
return $name if !$reserved{ uc $name } && $name =~ /^[a-z]/i;
- if ( $schema_obj_name ) {
- ++$unreserve{"$schema_obj_name.$name"};
- }
- else {
- ++$unreserve{"$name (table name)"};
- }
-
my $unreserve = sprintf '%s_', $name;
return $unreserve.$suffix;
}