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); |
0a6e5a56 |
63 | use SQL::Translator::Shim; |
64 | |
65 | my $shim = SQL::Translator::Shim->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 | |
104 | # ------------------------------------------------------------------- |
105 | sub produce { |
106 | my $translator = shift; |
107 | $DEBUG = $translator->debug; |
108 | $WARN = $translator->show_warnings; |
109 | my $no_comments = $translator->no_comments; |
110 | my $add_drop_table = $translator->add_drop_table; |
111 | my $schema = $translator->schema; |
112 | |
e2fb9ad3 |
113 | %global_names = (); #reset |
e2fb9ad3 |
114 | |
7a0ceaa1 |
115 | my $output; |
116 | $output .= header_comment."\n" unless ($no_comments); |
117 | |
118 | # Generate the DROP statements. We do this in one block here as if we |
119 | # have fkeys we need to drop in the correct order otherwise they will fail |
120 | # due to the dependancies the fkeys setup. (There is no way to turn off |
121 | # fkey checking while we sort the schema like MySQL's set |
122 | # foreign_key_checks=0) |
123 | # We assume the tables are in the correct order to set them up as you need |
124 | # to have created a table to fkey to it. So the reverse order should drop |
125 | # them properly, fingers crossed... |
126 | if ($add_drop_table) { |
127 | $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments; |
128 | foreach my $table ( |
129 | sort { $b->order <=> $a->order } $schema->get_tables |
130 | ) { |
0a6e5a56 |
131 | my $name = $table->name; |
132 | my $q_name = unreserve($name); |
133 | $output .= qq{IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $q_name;\n\n} |
7a0ceaa1 |
134 | } |
135 | } |
136 | |
137 | # Generate the CREATE sql |
f9a5ee79 |
138 | |
139 | my @foreign_constraints = (); # these need to be added separately, as tables may not exist yet |
140 | |
7a0ceaa1 |
141 | for my $table ( $schema->get_tables ) { |
142 | my $table_name = $table->name or next; |
7a0ceaa1 |
143 | my $table_name_ur = unreserve($table_name) || ''; |
144 | |
145 | my ( @comments, @field_defs, @index_defs, @constraint_defs ); |
146 | |
147 | push @comments, "\n\n--\n-- Table: $table_name_ur\n--" |
148 | unless $no_comments; |
149 | |
150 | push @comments, map { "-- $_" } $table->comments; |
151 | |
152 | # |
153 | # Fields |
154 | # |
155 | my %field_name_scope; |
156 | for my $field ( $table->get_fields ) { |
e2fb9ad3 |
157 | my $field_name = $field->name; |
0a6e5a56 |
158 | my $field_name_ur = unreserve( $field_name ); |
7a0ceaa1 |
159 | my $field_def = qq["$field_name_ur"]; |
160 | $field_def =~ s/\"//g; |
161 | if ( $field_def =~ /identity/ ){ |
162 | $field_def =~ s/identity/pidentity/; |
163 | } |
164 | |
165 | # |
166 | # Datatype |
167 | # |
168 | my $data_type = lc $field->data_type; |
169 | my $orig_data_type = $data_type; |
170 | my %extra = $field->extra; |
171 | my $list = $extra{'list'} || []; |
172 | # \todo deal with embedded quotes |
173 | my $commalist = join( ', ', map { qq['$_'] } @$list ); |
7a0ceaa1 |
174 | |
175 | if ( $data_type eq 'enum' ) { |
e2fb9ad3 |
176 | my $check_name = mk_name( $field_name . '_chk' ); |
7a0ceaa1 |
177 | push @constraint_defs, |
e2fb9ad3 |
178 | "CONSTRAINT $check_name CHECK ($field_name IN ($commalist))"; |
7a0ceaa1 |
179 | $data_type .= 'character varying'; |
180 | } |
181 | elsif ( $data_type eq 'set' ) { |
182 | $data_type .= 'character varying'; |
183 | } |
f9a5ee79 |
184 | elsif ( grep { $data_type eq $_ } qw/bytea blob clob/ ) { |
185 | $data_type = 'varbinary'; |
186 | } |
7a0ceaa1 |
187 | else { |
188 | if ( defined $translate{ $data_type } ) { |
189 | $data_type = $translate{ $data_type }; |
190 | } |
191 | else { |
192 | warn "Unknown datatype: $data_type ", |
193 | "($table_name.$field_name)\n" if $WARN; |
194 | } |
195 | } |
196 | |
197 | my $size = $field->size; |
198 | if ( grep $_ eq $data_type, @no_size) { |
199 | # SQLServer doesn't seem to like sizes on some datatypes |
200 | $size = undef; |
201 | } |
202 | elsif ( !$size ) { |
203 | if ( $data_type =~ /numeric/ ) { |
204 | $size = '9,0'; |
205 | } |
206 | elsif ( $orig_data_type eq 'text' ) { |
207 | #interpret text fields as long varchars |
208 | $size = '255'; |
209 | } |
210 | elsif ( |
211 | $data_type eq 'varchar' && |
212 | $orig_data_type eq 'boolean' |
213 | ) { |
214 | $size = '6'; |
215 | } |
216 | elsif ( $data_type eq 'varchar' ) { |
217 | $size = '255'; |
218 | } |
219 | } |
220 | |
221 | $field_def .= " $data_type"; |
222 | $field_def .= "($size)" if $size; |
223 | |
224 | $field_def .= ' IDENTITY' if $field->is_auto_increment; |
225 | |
226 | # |
1426be03 |
227 | # Not null constraint |
228 | # |
229 | unless ( $field->is_nullable ) { |
230 | $field_def .= ' NOT NULL'; |
231 | } |
232 | else { |
233 | $field_def .= ' NULL' if $data_type ne 'bit'; |
234 | } |
1426be03 |
235 | |
236 | # |
7a0ceaa1 |
237 | # Default value |
238 | # |
06baeb21 |
239 | SQL::Translator::Producer->_apply_default_value( |
240 | $field, |
241 | \$field_def, |
242 | [ |
243 | 'NULL' => \'NULL', |
244 | ], |
245 | ); |
bc8e2aa1 |
246 | |
028386aa |
247 | push @field_defs, $field_def; |
7a0ceaa1 |
248 | } |
249 | |
250 | # |
251 | # Constraint Declarations |
252 | # |
253 | my @constraint_decs = (); |
7a0ceaa1 |
254 | for my $constraint ( $table->get_constraints ) { |
255 | my $name = $constraint->name || ''; |
0a6e5a56 |
256 | my $name_ur = unreserve($name); |
7a0ceaa1 |
257 | # Make sure we get a unique name |
7a0ceaa1 |
258 | my $type = $constraint->type || NORMAL; |
0a6e5a56 |
259 | my @fields = map { unreserve( $_ ) } |
7a0ceaa1 |
260 | $constraint->fields; |
0a6e5a56 |
261 | my @rfields = map { unreserve( $_ ) } |
7a0ceaa1 |
262 | $constraint->reference_fields; |
263 | next unless @fields; |
264 | |
5bac76bc |
265 | my $c_def; |
f9a5ee79 |
266 | if ( $type eq FOREIGN_KEY ) { |
e2fb9ad3 |
267 | $name ||= mk_name( $table_name . '_fk' ); |
5bac76bc |
268 | my $on_delete = uc ($constraint->on_delete || ''); |
269 | my $on_update = uc ($constraint->on_update || ''); |
270 | |
271 | # The default implicit constraint action in MSSQL is RESTRICT |
272 | # but you can not specify it explicitly. Go figure :) |
273 | for ($on_delete, $on_update) { |
274 | undef $_ if $_ eq 'RESTRICT' |
275 | } |
276 | |
028386aa |
277 | $c_def = |
0a6e5a56 |
278 | "ALTER TABLE $table_name_ur ADD CONSTRAINT $name_ur FOREIGN KEY". |
7a0ceaa1 |
279 | ' (' . join( ', ', @fields ) . ') REFERENCES '. |
0a6e5a56 |
280 | unreserve($constraint->reference_table). |
f9a5ee79 |
281 | ' (' . join( ', ', @rfields ) . ')' |
282 | ; |
283 | |
5bac76bc |
284 | if ( $on_delete && $on_delete ne "NO ACTION") { |
285 | $c_def .= " ON DELETE $on_delete"; |
286 | } |
287 | if ( $on_update && $on_update ne "NO ACTION") { |
288 | $c_def .= " ON UPDATE $on_update"; |
289 | } |
f9a5ee79 |
290 | |
291 | $c_def .= ";"; |
292 | |
293 | push @foreign_constraints, $c_def; |
294 | next; |
295 | } |
296 | |
297 | |
298 | if ( $type eq PRIMARY_KEY ) { |
028386aa |
299 | $name = ($name ? unreserve($name) : mk_name( $table_name . '_pk' )); |
300 | $c_def = |
f9a5ee79 |
301 | "CONSTRAINT $name PRIMARY KEY ". |
302 | '(' . join( ', ', @fields ) . ')'; |
7a0ceaa1 |
303 | } |
304 | elsif ( $type eq UNIQUE ) { |
0a6e5a56 |
305 | $name = $name_ur || mk_name( $table_name . '_uc' ); |
028386aa |
306 | $c_def = |
7a0ceaa1 |
307 | "CONSTRAINT $name UNIQUE " . |
308 | '(' . join( ', ', @fields ) . ')'; |
309 | } |
04a180d6 |
310 | push @constraint_defs, $c_def; |
7a0ceaa1 |
311 | } |
312 | |
313 | # |
314 | # Indices |
315 | # |
316 | for my $index ( $table->get_indices ) { |
e2fb9ad3 |
317 | my $idx_name = $index->name || mk_name($table_name . '_idx'); |
0a6e5a56 |
318 | my $idx_name_ur = unreserve($idx_name); |
7a0ceaa1 |
319 | push @index_defs, |
0a6e5a56 |
320 | "CREATE INDEX $idx_name_ur ON $table_name_ur (". |
321 | join( ', ', map unreserve($_), $index->fields ) . ");"; |
7a0ceaa1 |
322 | } |
323 | |
324 | my $create_statement = ""; |
325 | $create_statement .= qq[CREATE TABLE $table_name_ur (\n]. |
028386aa |
326 | join( ",\n", |
7a0ceaa1 |
327 | map { " $_" } @field_defs, @constraint_defs |
328 | ). |
329 | "\n);" |
330 | ; |
331 | |
332 | $output .= join( "\n\n", |
333 | @comments, |
334 | $create_statement, |
335 | @index_defs, |
7a0ceaa1 |
336 | ); |
337 | } |
338 | |
f9a5ee79 |
339 | # Add FK constraints |
340 | $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints; |
341 | |
e2fb9ad3 |
342 | # create view/procedure are NOT prepended to the input $sql, needs |
343 | # to be filled in with the proper syntax |
344 | |
6fac033a |
345 | =pod |
e2fb9ad3 |
346 | |
7a0ceaa1 |
347 | # Text of view is already a 'create view' statement so no need to |
348 | # be fancy |
349 | foreach ( $schema->get_views ) { |
350 | my $name = $_->name(); |
351 | $output .= "\n\n"; |
5c5997ef |
352 | $output .= "--\n-- View: $name\n--\n\n" unless $no_comments; |
3e0bcbfd |
353 | my $text = $_->sql(); |
e2fb9ad3 |
354 | $text =~ s/\r//g; |
5bb0a4ee |
355 | $output .= "$text\nGO\n"; |
7a0ceaa1 |
356 | } |
357 | |
358 | # Text of procedure already has the 'create procedure' stuff |
359 | # so there is no need to do anything fancy. However, we should |
360 | # think about doing fancy stuff with granting permissions and |
361 | # so on. |
362 | foreach ( $schema->get_procedures ) { |
363 | my $name = $_->name(); |
364 | $output .= "\n\n"; |
5c5997ef |
365 | $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments; |
3e0bcbfd |
366 | my $text = $_->sql(); |
028386aa |
367 | $text =~ s/\r//g; |
5bb0a4ee |
368 | $output .= "$text\nGO\n"; |
7a0ceaa1 |
369 | } |
e2fb9ad3 |
370 | =cut |
7a0ceaa1 |
371 | |
372 | return $output; |
373 | } |
374 | |
375 | # ------------------------------------------------------------------- |
376 | sub mk_name { |
e2fb9ad3 |
377 | my ($name, $scope, $critical) = @_; |
7a0ceaa1 |
378 | |
379 | $scope ||= \%global_names; |
380 | if ( my $prev = $scope->{ $name } ) { |
381 | my $name_orig = $name; |
382 | $name .= sprintf( "%02d", ++$prev ); |
028386aa |
383 | substr($name, $max_id_length - 3) = "00" |
7a0ceaa1 |
384 | if length( $name ) > $max_id_length; |
385 | |
386 | warn "The name '$name_orig' has been changed to ", |
387 | "'$name' to make it unique.\n" if $WARN; |
388 | |
389 | $scope->{ $name_orig }++; |
390 | } |
028386aa |
391 | $name = substr( $name, 0, $max_id_length ) |
7a0ceaa1 |
392 | if ((length( $name ) > $max_id_length) && $critical); |
393 | $scope->{ $name }++; |
0a6e5a56 |
394 | return unreserve($name); |
7a0ceaa1 |
395 | } |
396 | |
397 | # ------------------------------------------------------------------- |
0a6e5a56 |
398 | sub unreserve { $shim->quote($_[0]) } |
7a0ceaa1 |
399 | |
400 | 1; |
401 | |
402 | # ------------------------------------------------------------------- |
403 | |
404 | =pod |
405 | |
406 | =head1 SEE ALSO |
407 | |
408 | SQL::Translator. |
409 | |
410 | =head1 AUTHORS |
411 | |
412 | Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from |
413 | Sybase producer, I just tweaked it for SQLServer. Thanks. |
414 | |
415 | =cut |