less accumulators more reduction
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLServer.pm
CommitLineData
7a0ceaa1 1package SQL::Translator::Producer::SQLServer;
2
7a0ceaa1 3=head1 NAME
4
5SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
6
7=head1 SYNOPSIS
8
9 use SQL::Translator;
10
11 my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
12 $t->translate;
13
14=head1 DESCRIPTION
15
16B<WARNING>B This is still fairly early code, basically a hacked version of the
17Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
18
19=head1 Extra Attributes
20
21=over 4
22
23=item field.list
24
25List of values for an enum field.
26
27=back
28
29=head1 TODO
30
31 * !! Write some tests !!
32 * Reserved words list needs updating to SQLServer.
e2fb9ad3 33 * Triggers, Procedures and Views DO NOT WORK
7a0ceaa1 34
35=cut
36
37use strict;
f27f9229 38use warnings;
0c04c5a2 39our ( $DEBUG, $WARN );
40our $VERSION = '1.59';
7a0ceaa1 41$DEBUG = 1 unless defined $DEBUG;
42
43use Data::Dumper;
44use SQL::Translator::Schema::Constants;
45use SQL::Translator::Utils qw(debug header_comment);
fd9f0e09 46use SQL::Translator::Generator::Utils;
c661b77d 47use SQL::Translator::Generator::DDL::SQLServer;
0a6e5a56 48
fd9f0e09 49my $util = SQL::Translator::Generator::Utils->new( quote_chars => ['[', ']'] );
c661b77d 50my $future = SQL::Translator::Generator::DDL::SQLServer->new();
7a0ceaa1 51
52my %translate = (
53 date => 'datetime',
54 'time' => 'datetime',
55 # Sybase types
56 #integer => 'numeric',
57 #int => 'numeric',
58 #number => 'numeric',
59 #money => 'money',
60 #varchar => 'varchar',
61 #varchar2 => 'varchar',
62 #timestamp => 'datetime',
63 #text => 'varchar',
64 #real => 'double precision',
65 #comment => 'text',
66 #bit => 'bit',
67 #tinyint => 'smallint',
68 #float => 'double precision',
028386aa 69 #serial => 'numeric',
7a0ceaa1 70 #boolean => 'varchar',
71 #char => 'char',
72 #long => 'varchar',
73);
74
7a0ceaa1 75# If these datatypes have size appended the sql fails.
3e0bcbfd 76my @no_size = qw/tinyint smallint int integer bigint text bit image datetime/;
7a0ceaa1 77
3e0bcbfd 78my $max_id_length = 128;
7a0ceaa1 79my %global_names;
7a0ceaa1 80
81=pod
82
83=head1 SQLServer Create Table Syntax
84
85TODO
86
87=cut
88
7a0ceaa1 89sub produce {
90 my $translator = shift;
91 $DEBUG = $translator->debug;
92 $WARN = $translator->show_warnings;
93 my $no_comments = $translator->no_comments;
94 my $add_drop_table = $translator->add_drop_table;
95 my $schema = $translator->schema;
96
e2fb9ad3 97 %global_names = (); #reset
e2fb9ad3 98
7a0ceaa1 99 my $output;
100 $output .= header_comment."\n" unless ($no_comments);
101
d02c3cd2 102 # Generate the DROP statements.
7a0ceaa1 103 if ($add_drop_table) {
d02c3cd2 104 my @tables = sort { $b->order <=> $a->order } $schema->get_tables;
105 $output .= "--\n-- Turn off constraints\n--\n\n" unless $no_comments;
106 foreach my $table (@tables) {
107 my $name = $table->name;
108 my $q_name = unreserve($name);
109 $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') ALTER TABLE $q_name NOCHECK CONSTRAINT all;\n"
110 }
111 $output .= "\n";
7a0ceaa1 112 $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments;
d02c3cd2 113 foreach my $table (@tables) {
0a6e5a56 114 my $name = $table->name;
115 my $q_name = unreserve($name);
d02c3cd2 116 $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $q_name;\n"
7a0ceaa1 117 }
118 }
119
871f55d4 120 # these need to be added separately, as tables may not exist yet
121 my @foreign_constraints = ();
f9a5ee79 122
871f55d4 123 for my $table ( grep { $_->name } $schema->get_tables ) {
124 my $table_name_ur = unreserve($table->name);
f9a5ee79 125
871f55d4 126 my ( @comments );
7a0ceaa1 127
128 push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
871f55d4 129 unless $no_comments;
7a0ceaa1 130
131 push @comments, map { "-- $_" } $table->comments;
132
871f55d4 133 push @foreign_constraints, map $future->foreign_key_constraint($_),
134 grep { $_->type eq FOREIGN_KEY } $table->get_constraints;
7a0ceaa1 135
136 $output .= join( "\n\n",
137 @comments,
871f55d4 138 qq[CREATE TABLE $table_name_ur (\n].
139 join( ",\n",
140 map { " $_" }
141 # field defs
142 ( map $future->field($_), $table->get_fields ),
143 # constraint defs
144 (map $future->enum_constraint($_->name, { $_->extra }->{list} || []),
145 grep { 'enum' eq lc $_->data_type } $table->get_fields),
146
147 (map $future->primary_key_constraint($_),
148 grep { $_->type eq PRIMARY_KEY } $table->get_constraints),
149
150 (map $future->unique_constraint_single($_),
151 grep {
152 $_->type eq UNIQUE &&
153 !grep { $_->is_nullable } $_->fields
154 } $table->get_constraints),
155 ).
156 "\n);",
157 # index defs
158 (map $future->unique_constraint_multiple($_),
159 grep {
160 $_->type eq UNIQUE &&
161 grep { $_->is_nullable } $_->fields
162 } $table->get_constraints),
163
164 (map $future->index($_), $table->get_indices)
7a0ceaa1 165 );
166 }
167
f9a5ee79 168# Add FK constraints
169 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
170
e2fb9ad3 171# create view/procedure are NOT prepended to the input $sql, needs
172# to be filled in with the proper syntax
173
871f55d4 174 return $output;
175}
176
6fac033a 177=pod
e2fb9ad3 178
7a0ceaa1 179 # Text of view is already a 'create view' statement so no need to
180 # be fancy
181 foreach ( $schema->get_views ) {
182 my $name = $_->name();
183 $output .= "\n\n";
5c5997ef 184 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
3e0bcbfd 185 my $text = $_->sql();
e2fb9ad3 186 $text =~ s/\r//g;
5bb0a4ee 187 $output .= "$text\nGO\n";
7a0ceaa1 188 }
189
190 # Text of procedure already has the 'create procedure' stuff
191 # so there is no need to do anything fancy. However, we should
192 # think about doing fancy stuff with granting permissions and
193 # so on.
194 foreach ( $schema->get_procedures ) {
195 my $name = $_->name();
196 $output .= "\n\n";
5c5997ef 197 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
3e0bcbfd 198 my $text = $_->sql();
028386aa 199 $text =~ s/\r//g;
5bb0a4ee 200 $output .= "$text\nGO\n";
7a0ceaa1 201 }
e2fb9ad3 202=cut
7a0ceaa1 203
7a0ceaa1 204sub mk_name {
e2fb9ad3 205 my ($name, $scope, $critical) = @_;
7a0ceaa1 206
207 $scope ||= \%global_names;
208 if ( my $prev = $scope->{ $name } ) {
209 my $name_orig = $name;
210 $name .= sprintf( "%02d", ++$prev );
028386aa 211 substr($name, $max_id_length - 3) = "00"
7a0ceaa1 212 if length( $name ) > $max_id_length;
213
214 warn "The name '$name_orig' has been changed to ",
215 "'$name' to make it unique.\n" if $WARN;
216
217 $scope->{ $name_orig }++;
218 }
028386aa 219 $name = substr( $name, 0, $max_id_length )
7a0ceaa1 220 if ((length( $name ) > $max_id_length) && $critical);
221 $scope->{ $name }++;
0a6e5a56 222 return unreserve($name);
7a0ceaa1 223}
224
f3fccb75 225sub unreserve { $util->quote($_[0]) }
7a0ceaa1 226
2271;
228
7a0ceaa1 229=pod
230
231=head1 SEE ALSO
232
233SQL::Translator.
234
235=head1 AUTHORS
236
237Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
238Sybase producer, I just tweaked it for SQLServer. Thanks.
239
240=cut