| 1 | package SQL::Translator::Producer::Sybase; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | SQL::Translator::Producer::Sybase - Sybase producer for SQL::Translator | ||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | use SQL::Translator; | ||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | my $t = SQL::Translator->new( parser => '...', producer => 'Sybase' ); | ||||||||||||||||||||||||||||||||||||||
| 12 | $t->translate; | ||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | This module will produce text output of the schema suitable for Sybase. | ||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | 1 | our ($DEBUG, $WARN); | |||||||||||||||||||||||||||||||||||||
| 23 | 1 | our $VERSION = '1.66'; | |||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | $DEBUG = 1 unless defined $DEBUG; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use SQL::Translator::Schema::Constants; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use SQL::Translator::Utils qw(debug header_comment); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | 1 | my %translate = ( | |||||||||||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||||||||||||
| 32 | # Sybase types | ||||||||||||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||||||||||||
| 34 | integer => 'numeric', | ||||||||||||||||||||||||||||||||||||||
| 35 | int => 'numeric', | ||||||||||||||||||||||||||||||||||||||
| 36 | number => 'numeric', | ||||||||||||||||||||||||||||||||||||||
| 37 | money => 'money', | ||||||||||||||||||||||||||||||||||||||
| 38 | varchar => 'varchar', | ||||||||||||||||||||||||||||||||||||||
| 39 | varchar2 => 'varchar', | ||||||||||||||||||||||||||||||||||||||
| 40 | timestamp => 'datetime', | ||||||||||||||||||||||||||||||||||||||
| 41 | text => 'varchar', | ||||||||||||||||||||||||||||||||||||||
| 42 | real => 'double precision', | ||||||||||||||||||||||||||||||||||||||
| 43 | comment => 'text', | ||||||||||||||||||||||||||||||||||||||
| 44 | bit => 'bit', | ||||||||||||||||||||||||||||||||||||||
| 45 | tinyint => 'smallint', | ||||||||||||||||||||||||||||||||||||||
| 46 | float => 'double precision', | ||||||||||||||||||||||||||||||||||||||
| 47 | serial => 'numeric', | ||||||||||||||||||||||||||||||||||||||
| 48 | boolean => 'varchar', | ||||||||||||||||||||||||||||||||||||||
| 49 | char => 'char', | ||||||||||||||||||||||||||||||||||||||
| 50 | long => 'varchar', | ||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | 1 | my %reserved = map { $_, 1 } qw[ | |||||||||||||||||||||||||||||||||||||
| 54 | ALL ANALYSE ANALYZE AND ANY AS ASC | ||||||||||||||||||||||||||||||||||||||
| 55 | BETWEEN BINARY BOTH | ||||||||||||||||||||||||||||||||||||||
| 56 | CASE CAST CHECK COLLATE COLUMN CONSTRAINT CROSS | ||||||||||||||||||||||||||||||||||||||
| 57 | CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER | ||||||||||||||||||||||||||||||||||||||
| 58 | DEFAULT DEFERRABLE DESC DISTINCT DO | ||||||||||||||||||||||||||||||||||||||
| 59 | ELSE END EXCEPT | ||||||||||||||||||||||||||||||||||||||
| 60 | FALSE FOR FOREIGN FREEZE FROM FULL | ||||||||||||||||||||||||||||||||||||||
| 61 | GROUP HAVING | ||||||||||||||||||||||||||||||||||||||
| 62 | ILIKE IN INITIALLY INNER INTERSECT INTO IS ISNULL | ||||||||||||||||||||||||||||||||||||||
| 63 | JOIN LEADING LEFT LIKE LIMIT | ||||||||||||||||||||||||||||||||||||||
| 64 | NATURAL NEW NOT NOTNULL NULL | ||||||||||||||||||||||||||||||||||||||
| 65 | OFF OFFSET OLD ON ONLY OR ORDER OUTER OVERLAPS | ||||||||||||||||||||||||||||||||||||||
| 66 | PRIMARY PUBLIC REFERENCES RIGHT | ||||||||||||||||||||||||||||||||||||||
| 67 | SELECT SESSION_USER SOME TABLE THEN TO TRAILING TRUE | ||||||||||||||||||||||||||||||||||||||
| 68 | UNION UNIQUE USER USING VERBOSE WHEN WHERE | ||||||||||||||||||||||||||||||||||||||
| 69 | ]; | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | 1 | my $max_id_length = 30; | |||||||||||||||||||||||||||||||||||||
| 72 | 1 | my %used_identifiers = (); | |||||||||||||||||||||||||||||||||||||
| 73 | 1 | my %global_names; | |||||||||||||||||||||||||||||||||||||
| 74 | my %unreserve; | ||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my %truncated; | |||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | =pod | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | =head1 Sybase Create Table Syntax | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( | ||||||||||||||||||||||||||||||||||||||
| 82 | { column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ] | ||||||||||||||||||||||||||||||||||||||
| 83 | | table_constraint } [, ... ] | ||||||||||||||||||||||||||||||||||||||
| 84 | ) | ||||||||||||||||||||||||||||||||||||||
| 85 | [ INHERITS ( parent_table [, ... ] ) ] | ||||||||||||||||||||||||||||||||||||||
| 86 | [ WITH OIDS | WITHOUT OIDS ] | ||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | where column_constraint is: | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | [ CONSTRAINT constraint_name ] | ||||||||||||||||||||||||||||||||||||||
| 91 | { NOT NULL | NULL | UNIQUE | PRIMARY KEY | | ||||||||||||||||||||||||||||||||||||||
| 92 | CHECK (expression) | | ||||||||||||||||||||||||||||||||||||||
| 93 | REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ] | ||||||||||||||||||||||||||||||||||||||
| 94 | [ ON DELETE action ] [ ON UPDATE action ] } | ||||||||||||||||||||||||||||||||||||||
| 95 | [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | and table_constraint is: | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | [ CONSTRAINT constraint_name ] | ||||||||||||||||||||||||||||||||||||||
| 100 | { UNIQUE ( column_name [, ... ] ) | | ||||||||||||||||||||||||||||||||||||||
| 101 | PRIMARY KEY ( column_name [, ... ] ) | | ||||||||||||||||||||||||||||||||||||||
| 102 | CHECK ( expression ) | | ||||||||||||||||||||||||||||||||||||||
| 103 | FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ] | ||||||||||||||||||||||||||||||||||||||
| 104 | [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] } | ||||||||||||||||||||||||||||||||||||||
| 105 | [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | =head1 Create Index Syntax | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | CREATE [ UNIQUE ] INDEX index_name ON table | ||||||||||||||||||||||||||||||||||||||
| 110 | [ USING acc_method ] ( column [ ops_name ] [, ...] ) | ||||||||||||||||||||||||||||||||||||||
| 111 | [ WHERE predicate ] | ||||||||||||||||||||||||||||||||||||||
| 112 | CREATE [ UNIQUE ] INDEX index_name ON table | ||||||||||||||||||||||||||||||||||||||
| 113 | [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] ) | ||||||||||||||||||||||||||||||||||||||
| 114 | [ WHERE predicate ] | ||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | sub produce { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
produce: called CC=49 SCAR=46.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my $translator = shift; | |||||||||||||||||||||||||||||||||||||
| 120 | 1 | $DEBUG = $translator->debug; | |||||||||||||||||||||||||||||||||||||
| 121 | 1 | $WARN = $translator->show_warnings; | |||||||||||||||||||||||||||||||||||||
| 122 | 1 | my $no_comments = $translator->no_comments; | |||||||||||||||||||||||||||||||||||||
| 123 | 1 | my $add_drop_table = $translator->add_drop_table; | |||||||||||||||||||||||||||||||||||||
| 124 | 1 | my $schema = $translator->schema; | |||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | 1 | my @output; | |||||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | push @output, header_comment unless ($no_comments); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | 1 | my @foreign_keys; | |||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | 1 | for my $table ($schema->get_tables) { | |||||||||||||||||||||||||||||||||||||
| 132 | 4 | ▶ | my $table_name = $table->name or next; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 133 | 4 | $table_name = mk_name($table_name, '', undef, 1); | |||||||||||||||||||||||||||||||||||||
| 134 | 4 | ▶ | my $table_name_ur = unreserve($table_name) || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
unreserve($table_name) || ''
MC/DC0%0/1
unreserve($table_name) || ''
unreserve($table_name)
Truth table50%1/2
A
A= unreserve($table_name)
| |||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | 4 | my (@comments, @field_defs, @index_defs, @constraint_defs); | |||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | push @comments, "--\n-- Table: $table_name_ur\n--" unless $no_comments; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | 0 | push @comments, map {"-- $_"} $table->comments; | |||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | # | ||||||||||||||||||||||||||||||||||||||
| 143 | # Fields | ||||||||||||||||||||||||||||||||||||||
| 144 | # | ||||||||||||||||||||||||||||||||||||||
| 145 | 4 | my %field_name_scope; | |||||||||||||||||||||||||||||||||||||
| 146 | 4 | for my $field ($table->get_fields) { | |||||||||||||||||||||||||||||||||||||
| 147 | 14 | my $field_name = mk_name($field->name, '', \%field_name_scope, undef, 1); | |||||||||||||||||||||||||||||||||||||
| 148 | 14 | my $field_name_ur = unreserve($field_name, $table_name); | |||||||||||||||||||||||||||||||||||||
| 149 | 14 | my $field_def = qq["$field_name_ur"]; | |||||||||||||||||||||||||||||||||||||
| 150 | 14 | $field_def =~ s/\"//g; | |||||||||||||||||||||||||||||||||||||
| 151 | 14 | ▶ | if ($field_def =~ /identity/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 152 | 0 | $field_def =~ s/identity/pidentity/; | |||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||
| 155 | # | ||||||||||||||||||||||||||||||||||||||
| 156 | # Datatype | ||||||||||||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||||||||||||
| 158 | 14 | my $data_type = lc $field->data_type; | |||||||||||||||||||||||||||||||||||||
| 159 | 14 | my $orig_data_type = $data_type; | |||||||||||||||||||||||||||||||||||||
| 160 | 14 | my %extra = $field->extra; | |||||||||||||||||||||||||||||||||||||
| 161 | 14 | ▶ | my $list = $extra{'list'} || []; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$extra{'list'} || []
MC/DC0%0/1
$extra{'list'} || []
$extra{'list'}
Truth table50%1/2
A
A= $extra{'list'}
| |||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | # \todo deal with embedded quotes | ||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $commalist = join(', ', map {qq['$_']} @$list); | |||||||||||||||||||||||||||||||||||||
| 165 | 14 | my $seq_name; | |||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||
| 167 | 14 | my $identity = ''; | |||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||
| 169 | 14 | ▶ | if ($data_type eq 'enum') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my $check_name = mk_name($table_name . '_' . $field_name, 'chk', undef, 1); | |||||||||||||||||||||||||||||||||||||
| 171 | 0 | push @constraint_defs, "CONSTRAINT $check_name CHECK ($field_name IN ($commalist))"; | |||||||||||||||||||||||||||||||||||||
| 172 | 0 | $data_type .= 'character varying'; | |||||||||||||||||||||||||||||||||||||
| 173 | } elsif ($data_type eq 'set') { | ||||||||||||||||||||||||||||||||||||||
| 174 | 0 | $data_type .= 'character varying'; | |||||||||||||||||||||||||||||||||||||
| 175 | } else { | ||||||||||||||||||||||||||||||||||||||
| 176 | 14 | ▶ | if ($field->is_auto_increment) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 177 | 3 | $identity = 'IDENTITY'; | |||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||
| 179 | 14 | ▶ | if (defined $translate{$data_type}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 180 | 12 | $data_type = $translate{$data_type}; | |||||||||||||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||||||||||||
| 182 | 2 | ▶ | warn "Unknown datatype: $data_type ", "($table_name.$field_name)\n" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 183 | if $WARN; | ||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||
| 187 | 14 | my $size = $field->size; | |||||||||||||||||||||||||||||||||||||
| 188 | 14 | ▶ | unless ($size) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 189 | 3 | ▶ | if ($data_type =~ /numeric/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||
| 190 | 0 | $size = '9,0'; | |||||||||||||||||||||||||||||||||||||
| 191 | } elsif ($orig_data_type eq 'text') { | ||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||
| 193 | #interpret text fields as long varchars | ||||||||||||||||||||||||||||||||||||||
| 194 | 2 | $size = '255'; | |||||||||||||||||||||||||||||||||||||
| 195 | ▶ | } elsif ($data_type eq 'varchar' | |||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$data_type eq "varchar" and $orig_data_type eq "boolean"
MC/DC0%0/2
$data_type eq "varchar" and $orig_data_type eq "boolean"
$data_type eq "varchar"
$orig_data_type eq "boolean"
Truth table33%1/3
A and B
A= $data_type eq "varchar"B= $orig_data_type eq "boolean"
| |||||||||||||||||||||||||||||||||||||||
| 196 | && $orig_data_type eq 'boolean') { | ||||||||||||||||||||||||||||||||||||||
| 197 | 0 | $size = '6'; | |||||||||||||||||||||||||||||||||||||
| 198 | } elsif ($data_type eq 'varchar') { | ||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $size = '255'; | |||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||
| 203 | 14 | $field_def .= " $data_type"; | |||||||||||||||||||||||||||||||||||||
| 204 | 14 | ▶ | $field_def .= "($size)" if $size; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 205 | 14 | ▶ | $field_def .= " $identity" if $identity; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||
| 207 | # | ||||||||||||||||||||||||||||||||||||||
| 208 | # Default value | ||||||||||||||||||||||||||||||||||||||
| 209 | # | ||||||||||||||||||||||||||||||||||||||
| 210 | 14 | my $default = $field->default_value; | |||||||||||||||||||||||||||||||||||||
| 211 | 14 | ▶ | if (defined $default) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 212 | 7 | ▶ | $field_def .= sprintf(' DEFAULT %s', | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$field->is_auto_increment and $seq_name
MC/DC0%0/2
$field->is_auto_increment and $seq_name
$field->is_auto_increment
$seq_name
Truth table33%1/3
A and B
A= $field->is_auto_incrementB= $seq_name
| |||||||||||||||||||||||||||||||||||||||
| 213 | ($field->is_auto_increment && $seq_name) ? qq[nextval('"$seq_name"'::text)] | ||||||||||||||||||||||||||||||||||||||
| 214 | : ($default =~ m/null/i) ? 'NULL' | ||||||||||||||||||||||||||||||||||||||
| 215 | : "'$default'"); | ||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||
| 218 | # | ||||||||||||||||||||||||||||||||||||||
| 219 | # Not null constraint | ||||||||||||||||||||||||||||||||||||||
| 220 | # | ||||||||||||||||||||||||||||||||||||||
| 221 | 14 | ▶ | unless ($field->is_nullable) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 222 | 5 | $field_def .= ' NOT NULL'; | |||||||||||||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||||||||||||
| 224 | 9 | ▶ | $field_def .= ' NULL' if $data_type ne 'bit'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||
| 227 | 14 | push @field_defs, $field_def; | |||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||
| 230 | # | ||||||||||||||||||||||||||||||||||||||
| 231 | # Constraint Declarations | ||||||||||||||||||||||||||||||||||||||
| 232 | # | ||||||||||||||||||||||||||||||||||||||
| 233 | 4 | my @constraint_decs = (); | |||||||||||||||||||||||||||||||||||||
| 234 | 4 | my $c_name_default; | |||||||||||||||||||||||||||||||||||||
| 235 | 4 | for my $constraint ($table->get_constraints) { | |||||||||||||||||||||||||||||||||||||
| 236 | 5 | ▶ | my $name = $constraint->name || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$constraint->name || ''
MC/DC100%1/1
$constraint->name || ''
$constraint->name
Truth table100%2/2
A
A= $constraint->name
| |||||||||||||||||||||||||||||||||||||||
| 237 | 5 | ▶ | my $type = $constraint->type || NORMAL; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$constraint->type || 'NORMAL'
MC/DC0%0/1
$constraint->type || 'NORMAL'
$constraint->type
Truth table50%1/2
A
A= $constraint->type
| |||||||||||||||||||||||||||||||||||||||
| 238 | 5 | my @fields = map { unreserve($_, $table_name) } $constraint->fields; | |||||||||||||||||||||||||||||||||||||
| 239 | 5 | my @rfields = map { unreserve($_, $table_name) } $constraint->reference_fields; | |||||||||||||||||||||||||||||||||||||
| 240 | 5 | ▶ | next unless @fields; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||
| 242 | 5 | ▶ | if ($type eq PRIMARY_KEY) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||
| 243 | 3 | ▶ | $name ||= mk_name($table_name, 'pk', undef, 1); | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$name ||= mk_name($table_name, "pk", undef, 1)
MC/DC0%0/2
$name ||= mk_name($table_name, "pk", undef, 1)
$name
mk_name($table_name, "pk", undef, 1)
Truth table33%1/3
A ||= B
A= $nameB= mk_name($table_name, "pk", undef, 1)
| |||||||||||||||||||||||||||||||||||||||
| 244 | 3 | push @constraint_defs, "CONSTRAINT $name PRIMARY KEY " . '(' . join(', ', @fields) . ')'; | |||||||||||||||||||||||||||||||||||||
| 245 | } elsif ($type eq FOREIGN_KEY) { | ||||||||||||||||||||||||||||||||||||||
| 246 | 1 | ▶ | $name ||= mk_name($table_name, 'fk', undef, 1); | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$name ||= mk_name($table_name, "fk", undef, 1)
MC/DC0%0/2
$name ||= mk_name($table_name, "fk", undef, 1)
$name
mk_name($table_name, "fk", undef, 1)
Truth table33%1/3
A ||= B
A= $nameB= mk_name($table_name, "fk", undef, 1)
| |||||||||||||||||||||||||||||||||||||||
| 247 | 1 | push @foreign_keys, | |||||||||||||||||||||||||||||||||||||
| 248 | "ALTER TABLE $table ADD CONSTRAINT $name FOREIGN KEY" . ' (' | ||||||||||||||||||||||||||||||||||||||
| 249 | . join(', ', @fields) | ||||||||||||||||||||||||||||||||||||||
| 250 | . ') REFERENCES ' | ||||||||||||||||||||||||||||||||||||||
| 251 | . $constraint->reference_table . ' (' | ||||||||||||||||||||||||||||||||||||||
| 252 | . join(', ', @rfields) . ')'; | ||||||||||||||||||||||||||||||||||||||
| 253 | } elsif ($type eq UNIQUE) { | ||||||||||||||||||||||||||||||||||||||
| 254 | 1 | ▶ | $name ||= mk_name($table_name, $name || ++$c_name_default, undef, 1); | ||||||||||||||||||||||||||||||||||||
|
Condition16%1/6
$name ||= mk_name($table_name, $name || ++$c_name_default, undef, 1)
$name || ++$c_name_default
MC/DC0%0/2
$name ||= mk_name($table_name, $name || ++$c_name_default, undef, 1)
$name
mk_name($table_name, $name || ++$c_name_default, undef, 1)
MC/DC0%0/2
$name || ++$c_name_default
$name
++$c_name_default
Truth table33%1/3
A ||= B
A= $nameB= mk_name($table_name, $name || ++$c_name_default, undef, 1)
Truth table0%0/3
A || B
A= $nameB= ++$c_name_default
| |||||||||||||||||||||||||||||||||||||||
| 255 | 1 | push @constraint_defs, "CONSTRAINT $name UNIQUE " . '(' . join(', ', @fields) . ')'; | |||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||
| 259 | # | ||||||||||||||||||||||||||||||||||||||
| 260 | # Indices | ||||||||||||||||||||||||||||||||||||||
| 261 | # | ||||||||||||||||||||||||||||||||||||||
| 262 | 4 | for my $index ($table->get_indices) { | |||||||||||||||||||||||||||||||||||||
| 263 | 1 | push @index_defs, 'CREATE INDEX ' . $index->name . " ON $table_name (" . join(', ', $index->fields) . ")"; | |||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||
| 266 | 4 | ▶ | my $drop_statement = $add_drop_table ? qq[DROP TABLE $table_name_ur] : ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 267 | my $create_statement | ||||||||||||||||||||||||||||||||||||||
| 268 | 4 | = qq[CREATE TABLE $table_name_ur (\n] . join(",\n", map {" $_"} @field_defs, @constraint_defs) . "\n)"; | |||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | 4 | $create_statement = join("\n\n", @comments) . "\n\n" . $create_statement; | |||||||||||||||||||||||||||||||||||||
| 271 | 4 | push @output, $create_statement, @index_defs,; | |||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||
| 274 | 1 | foreach my $view ($schema->get_views) { | |||||||||||||||||||||||||||||||||||||
| 275 | 1 | my (@comments, $view_name); | |||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||
| 277 | 1 | $view_name = $view->name(); | |||||||||||||||||||||||||||||||||||||
| 278 | 1 | ▶ | push @comments, "--\n-- View: $view_name\n--" unless $no_comments; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||
| 280 | # text of view is already a 'create view' statement so no need | ||||||||||||||||||||||||||||||||||||||
| 281 | # to do anything fancy. | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | 1 | push @output, join("\n\n", @comments, $view->sql(),); | |||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||
| 286 | 1 | foreach my $procedure ($schema->get_procedures) { | |||||||||||||||||||||||||||||||||||||
| 287 | 1 | my (@comments, $procedure_name); | |||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||
| 289 | 1 | $procedure_name = $procedure->name(); | |||||||||||||||||||||||||||||||||||||
| 290 | 1 | ▶ | push @comments, "--\n-- Procedure: $procedure_name\n--" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 291 | unless $no_comments; | ||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||
| 293 | # text of procedure already has the 'create procedure' stuff | ||||||||||||||||||||||||||||||||||||||
| 294 | # so there is no need to do anything fancy. However, we should | ||||||||||||||||||||||||||||||||||||||
| 295 | # think about doing fancy stuff with granting permissions and | ||||||||||||||||||||||||||||||||||||||
| 296 | # so on. | ||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||
| 298 | 1 | push @output, join("\n\n", @comments, $procedure->sql(),); | |||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||
| 300 | 1 | push @output, @foreign_keys; | |||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||
| 302 | 1 | ▶ | if ($WARN) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | if (%truncated) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 304 | 0 | warn "Truncated " . keys(%truncated) . " names:\n"; | |||||||||||||||||||||||||||||||||||||
| 305 | 0 | warn "\t" . join("\n\t", sort keys %truncated) . "\n"; | |||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | if (%unreserve) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 309 | 0 | warn "Encounted " . keys(%unreserve) . " unsafe names in schema (reserved or invalid):\n"; | |||||||||||||||||||||||||||||||||||||
| 310 | 0 | warn "\t" . join("\n\t", sort keys %unreserve) . "\n"; | |||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||
| 314 | 1 | ▶ | return wantarray ? @output : join ";\n\n", @output; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||
| 317 | 21 | ▶ | sub mk_name { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
mk_name: called CC=18 SCAR=37.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 318 | 21 | ▶ | my $basename = shift || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 319 | 21 | ▶ | my $type = shift || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 320 | 21 | ▶ | my $scope = shift || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 321 | 21 | ▶ | my $critical = shift || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 322 | 21 | my $basename_orig = $basename; | |||||||||||||||||||||||||||||||||||||
| 323 | 21 | ▶ | my $max_name | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 324 | = $type | ||||||||||||||||||||||||||||||||||||||
| 325 | ? $max_id_length - (length($type) + 1) | ||||||||||||||||||||||||||||||||||||||
| 326 | : $max_id_length; | ||||||||||||||||||||||||||||||||||||||
| 327 | 21 | ▶ | $basename = substr($basename, 0, $max_name) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 328 | if length($basename) > $max_name; | ||||||||||||||||||||||||||||||||||||||
| 329 | 21 | ▶ | my $name = $type ? "${type}_$basename" : $basename; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||
| 331 | 21 | ▶ | if ($basename ne $basename_orig and $critical) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$basename ne $basename_orig and $critical
MC/DC0%0/2
$basename ne $basename_orig and $critical
$basename ne $basename_orig
$critical
Truth table33%1/3
A and B
A= $basename ne $basename_origB= $critical
| |||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | my $show_type = $type ? "+'$type'" : ""; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | warn "Truncating '$basename_orig'$show_type to $max_id_length ", "character limit to make '$name'\n" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 334 | if $WARN; | ||||||||||||||||||||||||||||||||||||||
| 335 | 0 | $truncated{$basename_orig} = $name; | |||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||
| 338 | 21 | ▶ | $scope ||= \%global_names; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope ||= \%global_names
MC/DC100%1/1
$scope ||= \%global_names
$scope
Truth table100%2/2
A
A= $scope
| |||||||||||||||||||||||||||||||||||||||
| 339 | 21 | ▶ | if (my $prev = $scope->{$name}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 340 | 0 | my $name_orig = $name; | |||||||||||||||||||||||||||||||||||||
| 341 | 0 | $name .= sprintf("%02d", ++$prev); | |||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | substr($name, $max_id_length - 3) = "00" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 343 | if length($name) > $max_id_length; | ||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | warn "The name '$name_orig' has been changed to ", "'$name' to make it unique.\n" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 346 | if $WARN; | ||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||
| 348 | 0 | $scope->{$name_orig}++; | |||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||
| 350 | 21 | ▶ | $name = substr($name, 0, $max_id_length) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $name > $max_id_length and $critical
MC/DC0%0/2
length $name > $max_id_length and $critical
length $name > $max_id_length
$critical
Truth table33%1/3
A and B
A= length $name > $max_id_lengthB= $critical
| |||||||||||||||||||||||||||||||||||||||
| 351 | if ((length($name) > $max_id_length) && $critical); | ||||||||||||||||||||||||||||||||||||||
| 352 | 21 | $scope->{$name}++; | |||||||||||||||||||||||||||||||||||||
| 353 | 21 | return $name; | |||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||
| 356 | 24 | ▶ | sub unreserve { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
unreserve: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 357 | 24 | ▶ | my $name = shift || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 358 | 24 | ▶ | my $schema_obj_name = shift || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 359 | 24 | ▶ | my ($suffix) = ($name =~ s/(\W.*)$//) ? $1 : ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||
| 361 | # also trap fields that don't begin with a letter | ||||||||||||||||||||||||||||||||||||||
| 362 | 24 | ▶ | return $name if !$reserved{ uc $name } && $name =~ /^[a-z]/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $reserved{uc $name} and $name =~ /^[a-z]/i
MC/DC50%1/2
not $reserved{uc $name} and $name =~ /^[a-z]/i
not $reserved{uc $name}
$name =~ /^[a-z]/i
Truth table66%2/3
not(A) and B
A= not $reserved{uc $name}B= $name =~ /^[a-z]/i
| |||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||
| 364 | 5 | ▶ | if ($schema_obj_name) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 365 | 3 | ++$unreserve{"$schema_obj_name.$name"}; | |||||||||||||||||||||||||||||||||||||
| 366 | } else { | ||||||||||||||||||||||||||||||||||||||
| 367 | 2 | ++$unreserve{"$name (table name)"}; | |||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||
| 370 | 5 | my $unreserve = sprintf '%s_', $name; | |||||||||||||||||||||||||||||||||||||
| 371 | 5 | return $unreserve . $suffix; | |||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||
| 374 | 1 | 1; | |||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||
| 376 | =pod | ||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||
| 378 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||
| 380 | SQL::Translator. | ||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||
| 382 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||
| 384 | Sam Angiuoli E<lt>angiuoli@users.sourceforge.netE<gt>, | ||||||||||||||||||||||||||||||||||||||
| 385 | Paul Harrington E<lt>harringp@deshaw.comE<gt>, | ||||||||||||||||||||||||||||||||||||||
| 386 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. | ||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||
| 388 | =cut | ||||||||||||||||||||||||||||||||||||||