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