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