add remove_table_constraints to future
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLServer.pm
CommitLineData
7a0ceaa1 1package SQL::Translator::Producer::SQLServer;
2
7a0ceaa1 3use strict;
f27f9229 4use warnings;
0c04c5a2 5our ( $DEBUG, $WARN );
6our $VERSION = '1.59';
7a0ceaa1 7$DEBUG = 1 unless defined $DEBUG;
8
9use Data::Dumper;
10use SQL::Translator::Schema::Constants;
11use SQL::Translator::Utils qw(debug header_comment);
c661b77d 12use SQL::Translator::Generator::DDL::SQLServer;
0a6e5a56 13
c661b77d 14my $future = SQL::Translator::Generator::DDL::SQLServer->new();
7a0ceaa1 15
7a0ceaa1 16sub produce {
17 my $translator = shift;
7a0ceaa1 18 my $no_comments = $translator->no_comments;
19 my $add_drop_table = $translator->add_drop_table;
20 my $schema = $translator->schema;
21
22 my $output;
23 $output .= header_comment."\n" unless ($no_comments);
24
d02c3cd2 25 # Generate the DROP statements.
7a0ceaa1 26 if ($add_drop_table) {
d02c3cd2 27 my @tables = sort { $b->order <=> $a->order } $schema->get_tables;
28 $output .= "--\n-- Turn off constraints\n--\n\n" unless $no_comments;
29 foreach my $table (@tables) {
97a16cf4 30 $output .= $future->remove_table_constraints($table)
d02c3cd2 31 }
32 $output .= "\n";
7a0ceaa1 33 $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments;
d02c3cd2 34 foreach my $table (@tables) {
11bfa991 35 $output .= $future->drop_table($table);
7a0ceaa1 36 }
37 }
38
871f55d4 39 # these need to be added separately, as tables may not exist yet
40 my @foreign_constraints = ();
f9a5ee79 41
871f55d4 42 for my $table ( grep { $_->name } $schema->get_tables ) {
43 my $table_name_ur = unreserve($table->name);
f9a5ee79 44
871f55d4 45 my ( @comments );
7a0ceaa1 46
47 push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
871f55d4 48 unless $no_comments;
7a0ceaa1 49
50 push @comments, map { "-- $_" } $table->comments;
51
871f55d4 52 push @foreign_constraints, map $future->foreign_key_constraint($_),
53 grep { $_->type eq FOREIGN_KEY } $table->get_constraints;
7a0ceaa1 54
55 $output .= join( "\n\n",
56 @comments,
871f55d4 57 # index defs
7a16a53e 58 $future->table($table),
871f55d4 59 (map $future->unique_constraint_multiple($_),
60 grep {
61 $_->type eq UNIQUE &&
62 grep { $_->is_nullable } $_->fields
63 } $table->get_constraints),
64
65 (map $future->index($_), $table->get_indices)
7a0ceaa1 66 );
67 }
68
f9a5ee79 69# Add FK constraints
70 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
71
e2fb9ad3 72# create view/procedure are NOT prepended to the input $sql, needs
73# to be filled in with the proper syntax
74
871f55d4 75 return $output;
76}
77
837afeae 78sub unreserve { $future->quote($_[0]) }
056238d8 79
801;
81
6fac033a 82=pod
e2fb9ad3 83
056238d8 84=head1 SQLServer Create Table Syntax
85
86TODO
87
88
89=head1 NAME
90
91SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
92
93=head1 SYNOPSIS
94
95 use SQL::Translator;
96
97 my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
98 $t->translate;
99
100=head1 DESCRIPTION
101
102B<WARNING>B This is still fairly early code, basically a hacked version of the
103Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
104
105=head1 Extra Attributes
106
107=over 4
108
109=item field.list
110
111List of values for an enum field.
112
113=back
114
115=head1 TODO
116
117 * !! Write some tests !!
118 * Reserved words list needs updating to SQLServer.
119 * Triggers, Procedures and Views DO NOT WORK
120
121
7a0ceaa1 122 # Text of view is already a 'create view' statement so no need to
123 # be fancy
124 foreach ( $schema->get_views ) {
125 my $name = $_->name();
126 $output .= "\n\n";
5c5997ef 127 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
3e0bcbfd 128 my $text = $_->sql();
e2fb9ad3 129 $text =~ s/\r//g;
5bb0a4ee 130 $output .= "$text\nGO\n";
7a0ceaa1 131 }
132
133 # Text of procedure already has the 'create procedure' stuff
134 # so there is no need to do anything fancy. However, we should
135 # think about doing fancy stuff with granting permissions and
136 # so on.
137 foreach ( $schema->get_procedures ) {
138 my $name = $_->name();
139 $output .= "\n\n";
5c5997ef 140 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
3e0bcbfd 141 my $text = $_->sql();
028386aa 142 $text =~ s/\r//g;
5bb0a4ee 143 $output .= "$text\nGO\n";
7a0ceaa1 144 }
7a0ceaa1 145
146=head1 SEE ALSO
147
148SQL::Translator.
149
150=head1 AUTHORS
151
152Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
153Sybase producer, I just tweaked it for SQLServer. Thanks.
154
155=cut