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