Commit | Line | Data |
7a0ceaa1 |
1 | package SQL::Translator::Producer::SQLServer; |
2 | |
44659089 |
3 | # ------------------------------------------------------------------- |
4 | # Copyright (C) 2002-2009 SQLFairy Authors |
5 | # |
6 | # This program is free software; you can redistribute it and/or |
7 | # modify it under the terms of the GNU General Public License as |
8 | # published by the Free Software Foundation; version 2. |
9 | # |
10 | # This program is distributed in the hope that it will be useful, but |
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | # General Public License for more details. |
14 | # |
15 | # You should have received a copy of the GNU General Public License |
16 | # along with this program; if not, write to the Free Software |
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
18 | # 02111-1307 USA |
19 | # ------------------------------------------------------------------- |
20 | |
7a0ceaa1 |
21 | =head1 NAME |
22 | |
23 | SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator |
24 | |
25 | =head1 SYNOPSIS |
26 | |
27 | use SQL::Translator; |
28 | |
29 | my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' ); |
30 | $t->translate; |
31 | |
32 | =head1 DESCRIPTION |
33 | |
34 | B<WARNING>B This is still fairly early code, basically a hacked version of the |
35 | Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-) |
36 | |
37 | =head1 Extra Attributes |
38 | |
39 | =over 4 |
40 | |
41 | =item field.list |
42 | |
43 | List of values for an enum field. |
44 | |
45 | =back |
46 | |
47 | =head1 TODO |
48 | |
49 | * !! Write some tests !! |
50 | * Reserved words list needs updating to SQLServer. |
e2fb9ad3 |
51 | * Triggers, Procedures and Views DO NOT WORK |
7a0ceaa1 |
52 | |
53 | =cut |
54 | |
55 | use strict; |
da06ac74 |
56 | use vars qw[ $DEBUG $WARN $VERSION ]; |
11ad2df9 |
57 | $VERSION = '1.59'; |
7a0ceaa1 |
58 | $DEBUG = 1 unless defined $DEBUG; |
59 | |
60 | use Data::Dumper; |
61 | use SQL::Translator::Schema::Constants; |
62 | use SQL::Translator::Utils qw(debug header_comment); |
f3fccb75 |
63 | use SQL::Translator::ProducerUtils; |
0a6e5a56 |
64 | |
f3fccb75 |
65 | my $util = SQL::Translator::ProducerUtils->new( quote_chars => ['[', ']'] ); |
7a0ceaa1 |
66 | |
67 | my %translate = ( |
68 | date => 'datetime', |
69 | 'time' => 'datetime', |
70 | # Sybase types |
71 | #integer => 'numeric', |
72 | #int => 'numeric', |
73 | #number => 'numeric', |
74 | #money => 'money', |
75 | #varchar => 'varchar', |
76 | #varchar2 => 'varchar', |
77 | #timestamp => 'datetime', |
78 | #text => 'varchar', |
79 | #real => 'double precision', |
80 | #comment => 'text', |
81 | #bit => 'bit', |
82 | #tinyint => 'smallint', |
83 | #float => 'double precision', |
028386aa |
84 | #serial => 'numeric', |
7a0ceaa1 |
85 | #boolean => 'varchar', |
86 | #char => 'char', |
87 | #long => 'varchar', |
88 | ); |
89 | |
7a0ceaa1 |
90 | # If these datatypes have size appended the sql fails. |
3e0bcbfd |
91 | my @no_size = qw/tinyint smallint int integer bigint text bit image datetime/; |
7a0ceaa1 |
92 | |
3e0bcbfd |
93 | my $max_id_length = 128; |
7a0ceaa1 |
94 | my %global_names; |
7a0ceaa1 |
95 | |
96 | =pod |
97 | |
98 | =head1 SQLServer Create Table Syntax |
99 | |
100 | TODO |
101 | |
102 | =cut |
103 | |
7a0ceaa1 |
104 | sub produce { |
105 | my $translator = shift; |
106 | $DEBUG = $translator->debug; |
107 | $WARN = $translator->show_warnings; |
108 | my $no_comments = $translator->no_comments; |
109 | my $add_drop_table = $translator->add_drop_table; |
110 | my $schema = $translator->schema; |
111 | |
e2fb9ad3 |
112 | %global_names = (); #reset |
e2fb9ad3 |
113 | |
7a0ceaa1 |
114 | my $output; |
115 | $output .= header_comment."\n" unless ($no_comments); |
116 | |
d02c3cd2 |
117 | # Generate the DROP statements. |
7a0ceaa1 |
118 | if ($add_drop_table) { |
d02c3cd2 |
119 | my @tables = sort { $b->order <=> $a->order } $schema->get_tables; |
120 | $output .= "--\n-- Turn off constraints\n--\n\n" unless $no_comments; |
121 | foreach my $table (@tables) { |
122 | my $name = $table->name; |
123 | my $q_name = unreserve($name); |
124 | $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') ALTER TABLE $q_name NOCHECK CONSTRAINT all;\n" |
125 | } |
126 | $output .= "\n"; |
7a0ceaa1 |
127 | $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments; |
d02c3cd2 |
128 | foreach my $table (@tables) { |
0a6e5a56 |
129 | my $name = $table->name; |
130 | my $q_name = unreserve($name); |
d02c3cd2 |
131 | $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $q_name;\n" |
7a0ceaa1 |
132 | } |
133 | } |
134 | |
135 | # Generate the CREATE sql |
f9a5ee79 |
136 | |
137 | my @foreign_constraints = (); # these need to be added separately, as tables may not exist yet |
138 | |
7a0ceaa1 |
139 | for my $table ( $schema->get_tables ) { |
140 | my $table_name = $table->name or next; |
7a0ceaa1 |
141 | my $table_name_ur = unreserve($table_name) || ''; |
142 | |
143 | my ( @comments, @field_defs, @index_defs, @constraint_defs ); |
144 | |
145 | push @comments, "\n\n--\n-- Table: $table_name_ur\n--" |
146 | unless $no_comments; |
147 | |
148 | push @comments, map { "-- $_" } $table->comments; |
149 | |
150 | # |
151 | # Fields |
152 | # |
153 | my %field_name_scope; |
154 | for my $field ( $table->get_fields ) { |
e2fb9ad3 |
155 | my $field_name = $field->name; |
0a6e5a56 |
156 | my $field_name_ur = unreserve( $field_name ); |
7a0ceaa1 |
157 | my $field_def = qq["$field_name_ur"]; |
158 | $field_def =~ s/\"//g; |
159 | if ( $field_def =~ /identity/ ){ |
160 | $field_def =~ s/identity/pidentity/; |
161 | } |
162 | |
163 | # |
164 | # Datatype |
165 | # |
166 | my $data_type = lc $field->data_type; |
167 | my $orig_data_type = $data_type; |
168 | my %extra = $field->extra; |
169 | my $list = $extra{'list'} || []; |
170 | # \todo deal with embedded quotes |
171 | my $commalist = join( ', ', map { qq['$_'] } @$list ); |
7a0ceaa1 |
172 | |
173 | if ( $data_type eq 'enum' ) { |
e2fb9ad3 |
174 | my $check_name = mk_name( $field_name . '_chk' ); |
7a0ceaa1 |
175 | push @constraint_defs, |
e2fb9ad3 |
176 | "CONSTRAINT $check_name CHECK ($field_name IN ($commalist))"; |
7a0ceaa1 |
177 | $data_type .= 'character varying'; |
178 | } |
179 | elsif ( $data_type eq 'set' ) { |
180 | $data_type .= 'character varying'; |
181 | } |
f9a5ee79 |
182 | elsif ( grep { $data_type eq $_ } qw/bytea blob clob/ ) { |
183 | $data_type = 'varbinary'; |
184 | } |
7a0ceaa1 |
185 | else { |
186 | if ( defined $translate{ $data_type } ) { |
187 | $data_type = $translate{ $data_type }; |
188 | } |
189 | else { |
190 | warn "Unknown datatype: $data_type ", |
191 | "($table_name.$field_name)\n" if $WARN; |
192 | } |
193 | } |
194 | |
195 | my $size = $field->size; |
196 | if ( grep $_ eq $data_type, @no_size) { |
197 | # SQLServer doesn't seem to like sizes on some datatypes |
198 | $size = undef; |
199 | } |
200 | elsif ( !$size ) { |
201 | if ( $data_type =~ /numeric/ ) { |
202 | $size = '9,0'; |
203 | } |
204 | elsif ( $orig_data_type eq 'text' ) { |
205 | #interpret text fields as long varchars |
206 | $size = '255'; |
207 | } |
208 | elsif ( |
209 | $data_type eq 'varchar' && |
210 | $orig_data_type eq 'boolean' |
211 | ) { |
212 | $size = '6'; |
213 | } |
214 | elsif ( $data_type eq 'varchar' ) { |
215 | $size = '255'; |
216 | } |
217 | } |
218 | |
219 | $field_def .= " $data_type"; |
220 | $field_def .= "($size)" if $size; |
221 | |
222 | $field_def .= ' IDENTITY' if $field->is_auto_increment; |
223 | |
224 | # |
1426be03 |
225 | # Not null constraint |
226 | # |
227 | unless ( $field->is_nullable ) { |
228 | $field_def .= ' NOT NULL'; |
229 | } |
230 | else { |
231 | $field_def .= ' NULL' if $data_type ne 'bit'; |
232 | } |
1426be03 |
233 | |
234 | # |
7a0ceaa1 |
235 | # Default value |
236 | # |
06baeb21 |
237 | SQL::Translator::Producer->_apply_default_value( |
238 | $field, |
239 | \$field_def, |
240 | [ |
241 | 'NULL' => \'NULL', |
242 | ], |
243 | ); |
bc8e2aa1 |
244 | |
028386aa |
245 | push @field_defs, $field_def; |
7a0ceaa1 |
246 | } |
247 | |
248 | # |
249 | # Constraint Declarations |
250 | # |
251 | my @constraint_decs = (); |
7a0ceaa1 |
252 | for my $constraint ( $table->get_constraints ) { |
253 | my $name = $constraint->name || ''; |
0a6e5a56 |
254 | my $name_ur = unreserve($name); |
7a0ceaa1 |
255 | # Make sure we get a unique name |
7a0ceaa1 |
256 | my $type = $constraint->type || NORMAL; |
0a6e5a56 |
257 | my @fields = map { unreserve( $_ ) } |
7a0ceaa1 |
258 | $constraint->fields; |
0a6e5a56 |
259 | my @rfields = map { unreserve( $_ ) } |
7a0ceaa1 |
260 | $constraint->reference_fields; |
261 | next unless @fields; |
262 | |
5bac76bc |
263 | my $c_def; |
f9a5ee79 |
264 | if ( $type eq FOREIGN_KEY ) { |
e2fb9ad3 |
265 | $name ||= mk_name( $table_name . '_fk' ); |
5bac76bc |
266 | my $on_delete = uc ($constraint->on_delete || ''); |
267 | my $on_update = uc ($constraint->on_update || ''); |
268 | |
269 | # The default implicit constraint action in MSSQL is RESTRICT |
270 | # but you can not specify it explicitly. Go figure :) |
271 | for ($on_delete, $on_update) { |
272 | undef $_ if $_ eq 'RESTRICT' |
273 | } |
274 | |
028386aa |
275 | $c_def = |
0a6e5a56 |
276 | "ALTER TABLE $table_name_ur ADD CONSTRAINT $name_ur FOREIGN KEY". |
7a0ceaa1 |
277 | ' (' . join( ', ', @fields ) . ') REFERENCES '. |
0a6e5a56 |
278 | unreserve($constraint->reference_table). |
f9a5ee79 |
279 | ' (' . join( ', ', @rfields ) . ')' |
280 | ; |
281 | |
5bac76bc |
282 | if ( $on_delete && $on_delete ne "NO ACTION") { |
283 | $c_def .= " ON DELETE $on_delete"; |
284 | } |
285 | if ( $on_update && $on_update ne "NO ACTION") { |
286 | $c_def .= " ON UPDATE $on_update"; |
287 | } |
f9a5ee79 |
288 | |
289 | $c_def .= ";"; |
290 | |
291 | push @foreign_constraints, $c_def; |
292 | next; |
293 | } |
294 | |
295 | |
296 | if ( $type eq PRIMARY_KEY ) { |
028386aa |
297 | $name = ($name ? unreserve($name) : mk_name( $table_name . '_pk' )); |
298 | $c_def = |
f9a5ee79 |
299 | "CONSTRAINT $name PRIMARY KEY ". |
300 | '(' . join( ', ', @fields ) . ')'; |
7a0ceaa1 |
301 | } |
302 | elsif ( $type eq UNIQUE ) { |
0a6e5a56 |
303 | $name = $name_ur || mk_name( $table_name . '_uc' ); |
66444b41 |
304 | my @nullable = grep { $_->is_nullable } $constraint->fields; |
305 | if (!@nullable) { |
306 | $c_def = |
307 | "CONSTRAINT $name UNIQUE " . |
308 | '(' . join( ', ', @fields ) . ')'; |
309 | } else { |
310 | push @index_defs, |
311 | "CREATE UNIQUE NONCLUSTERED INDEX $name_ur ON $table_name_ur (" . |
312 | join( ', ', @fields ) . ')' . |
313 | ' WHERE ' . join( ' AND ', map unreserve($_->name) . ' IS NOT NULL', @nullable ) . ';'; |
314 | next; |
315 | } |
7a0ceaa1 |
316 | } |
04a180d6 |
317 | push @constraint_defs, $c_def; |
7a0ceaa1 |
318 | } |
319 | |
320 | # |
321 | # Indices |
322 | # |
323 | for my $index ( $table->get_indices ) { |
e2fb9ad3 |
324 | my $idx_name = $index->name || mk_name($table_name . '_idx'); |
0a6e5a56 |
325 | my $idx_name_ur = unreserve($idx_name); |
7a0ceaa1 |
326 | push @index_defs, |
0a6e5a56 |
327 | "CREATE INDEX $idx_name_ur ON $table_name_ur (". |
328 | join( ', ', map unreserve($_), $index->fields ) . ");"; |
7a0ceaa1 |
329 | } |
330 | |
331 | my $create_statement = ""; |
332 | $create_statement .= qq[CREATE TABLE $table_name_ur (\n]. |
028386aa |
333 | join( ",\n", |
7a0ceaa1 |
334 | map { " $_" } @field_defs, @constraint_defs |
335 | ). |
336 | "\n);" |
337 | ; |
338 | |
339 | $output .= join( "\n\n", |
340 | @comments, |
341 | $create_statement, |
342 | @index_defs, |
7a0ceaa1 |
343 | ); |
344 | } |
345 | |
f9a5ee79 |
346 | # Add FK constraints |
347 | $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints; |
348 | |
e2fb9ad3 |
349 | # create view/procedure are NOT prepended to the input $sql, needs |
350 | # to be filled in with the proper syntax |
351 | |
6fac033a |
352 | =pod |
e2fb9ad3 |
353 | |
7a0ceaa1 |
354 | # Text of view is already a 'create view' statement so no need to |
355 | # be fancy |
356 | foreach ( $schema->get_views ) { |
357 | my $name = $_->name(); |
358 | $output .= "\n\n"; |
5c5997ef |
359 | $output .= "--\n-- View: $name\n--\n\n" unless $no_comments; |
3e0bcbfd |
360 | my $text = $_->sql(); |
e2fb9ad3 |
361 | $text =~ s/\r//g; |
5bb0a4ee |
362 | $output .= "$text\nGO\n"; |
7a0ceaa1 |
363 | } |
364 | |
365 | # Text of procedure already has the 'create procedure' stuff |
366 | # so there is no need to do anything fancy. However, we should |
367 | # think about doing fancy stuff with granting permissions and |
368 | # so on. |
369 | foreach ( $schema->get_procedures ) { |
370 | my $name = $_->name(); |
371 | $output .= "\n\n"; |
5c5997ef |
372 | $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments; |
3e0bcbfd |
373 | my $text = $_->sql(); |
028386aa |
374 | $text =~ s/\r//g; |
5bb0a4ee |
375 | $output .= "$text\nGO\n"; |
7a0ceaa1 |
376 | } |
e2fb9ad3 |
377 | =cut |
7a0ceaa1 |
378 | |
379 | return $output; |
380 | } |
381 | |
7a0ceaa1 |
382 | sub mk_name { |
e2fb9ad3 |
383 | my ($name, $scope, $critical) = @_; |
7a0ceaa1 |
384 | |
385 | $scope ||= \%global_names; |
386 | if ( my $prev = $scope->{ $name } ) { |
387 | my $name_orig = $name; |
388 | $name .= sprintf( "%02d", ++$prev ); |
028386aa |
389 | substr($name, $max_id_length - 3) = "00" |
7a0ceaa1 |
390 | if length( $name ) > $max_id_length; |
391 | |
392 | warn "The name '$name_orig' has been changed to ", |
393 | "'$name' to make it unique.\n" if $WARN; |
394 | |
395 | $scope->{ $name_orig }++; |
396 | } |
028386aa |
397 | $name = substr( $name, 0, $max_id_length ) |
7a0ceaa1 |
398 | if ((length( $name ) > $max_id_length) && $critical); |
399 | $scope->{ $name }++; |
0a6e5a56 |
400 | return unreserve($name); |
7a0ceaa1 |
401 | } |
402 | |
f3fccb75 |
403 | sub unreserve { $util->quote($_[0]) } |
7a0ceaa1 |
404 | |
405 | 1; |
406 | |
7a0ceaa1 |
407 | =pod |
408 | |
409 | =head1 SEE ALSO |
410 | |
411 | SQL::Translator. |
412 | |
413 | =head1 AUTHORS |
414 | |
415 | Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from |
416 | Sybase producer, I just tweaked it for SQLServer. Thanks. |
417 | |
418 | =cut |