1 package SQL::Translator::Producer::SQLServer;
5 SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
11 my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
16 B<WARNING>B This is still fairly early code, basically a hacked version of the
17 Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
19 =head1 Extra Attributes
25 List of values for an enum field.
31 * !! Write some tests !!
32 * Reserved words list needs updating to SQLServer.
33 * Triggers, Procedures and Views DO NOT WORK
39 our ( $DEBUG, $WARN );
40 our $VERSION = '1.59';
41 $DEBUG = 1 unless defined $DEBUG;
44 use SQL::Translator::Schema::Constants;
45 use SQL::Translator::Utils qw(debug header_comment);
46 use SQL::Translator::Generator::Utils;
47 use SQL::Translator::Generator::DDL::SQLServer;
49 my $util = SQL::Translator::Generator::Utils->new( quote_chars => ['[', ']'] );
50 my $future = SQL::Translator::Generator::DDL::SQLServer->new();
56 #integer => 'numeric',
60 #varchar => 'varchar',
61 #varchar2 => 'varchar',
62 #timestamp => 'datetime',
64 #real => 'double precision',
67 #tinyint => 'smallint',
68 #float => 'double precision',
70 #boolean => 'varchar',
75 # If these datatypes have size appended the sql fails.
76 my @no_size = qw/tinyint smallint int integer bigint text bit image datetime/;
78 my $max_id_length = 128;
83 =head1 SQLServer Create Table Syntax
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;
97 %global_names = (); #reset
100 $output .= header_comment."\n" unless ($no_comments);
102 # Generate the DROP statements.
103 if ($add_drop_table) {
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"
112 $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments;
113 foreach my $table (@tables) {
114 my $name = $table->name;
115 my $q_name = unreserve($name);
116 $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $q_name;\n"
120 # Generate the CREATE sql
122 my @foreign_constraints = (); # these need to be added separately, as tables may not exist yet
124 for my $table ( $schema->get_tables ) {
125 my $table_name = $table->name or next;
126 my $table_name_ur = unreserve($table_name) || '';
128 my ( @comments, @field_defs, @index_defs, @constraint_defs );
130 push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
133 push @comments, map { "-- $_" } $table->comments;
138 my %field_name_scope;
139 for my $field ( $table->get_fields ) {
140 my $field_name = $field->name;
145 my $data_type = lc $field->data_type;
146 my %extra = $field->extra;
148 if ( $data_type eq 'enum' ) {
149 push @constraint_defs, $future->enum_constraint($field_name, $extra{'list'} || [])
152 push @field_defs, $future->field($field);
156 # Constraint Declarations
158 my @constraint_decs = ();
159 for my $constraint ( $table->get_constraints ) {
160 my $type = $constraint->type || NORMAL;
161 next unless $constraint->fields;
164 if ( $type eq FOREIGN_KEY ) {
165 push @foreign_constraints, $future->foreign_key_constraint($constraint);
170 if ( $type eq PRIMARY_KEY ) {
171 $c_def = $future->primary_key_constraint($constraint)
173 elsif ( $type eq UNIQUE ) {
174 if (!grep { $_->is_nullable } $constraint->fields) {
175 $c_def = $future->unique_constraint_single($constraint)
177 push @index_defs, $future->unique_constraint_multiple($constraint);
181 push @constraint_defs, $c_def;
187 for my $index ( $table->get_indices ) {
188 push @index_defs, $future->index($index)
191 my $create_statement = "";
192 $create_statement .= qq[CREATE TABLE $table_name_ur (\n].
194 map { " $_" } @field_defs, @constraint_defs
199 $output .= join( "\n\n",
207 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
209 # create view/procedure are NOT prepended to the input $sql, needs
210 # to be filled in with the proper syntax
214 # Text of view is already a 'create view' statement so no need to
216 foreach ( $schema->get_views ) {
217 my $name = $_->name();
219 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
220 my $text = $_->sql();
222 $output .= "$text\nGO\n";
225 # Text of procedure already has the 'create procedure' stuff
226 # so there is no need to do anything fancy. However, we should
227 # think about doing fancy stuff with granting permissions and
229 foreach ( $schema->get_procedures ) {
230 my $name = $_->name();
232 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
233 my $text = $_->sql();
235 $output .= "$text\nGO\n";
243 my ($name, $scope, $critical) = @_;
245 $scope ||= \%global_names;
246 if ( my $prev = $scope->{ $name } ) {
247 my $name_orig = $name;
248 $name .= sprintf( "%02d", ++$prev );
249 substr($name, $max_id_length - 3) = "00"
250 if length( $name ) > $max_id_length;
252 warn "The name '$name_orig' has been changed to ",
253 "'$name' to make it unique.\n" if $WARN;
255 $scope->{ $name_orig }++;
257 $name = substr( $name, 0, $max_id_length )
258 if ((length( $name ) > $max_id_length) && $critical);
260 return unreserve($name);
263 sub unreserve { $util->quote($_[0]) }
275 Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
276 Sybase producer, I just tweaked it for SQLServer. Thanks.