add drop_tables method
[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
7a0ceaa1 14sub produce {
15 my $translator = shift;
f9356e0d 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
7a0ceaa1 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
d02c3cd2 28 # Generate the DROP statements.
f9356e0d 29 $output .= $future->drop_tables;
7a0ceaa1 30
871f55d4 31 # these need to be added separately, as tables may not exist yet
32 my @foreign_constraints = ();
f9a5ee79 33
871f55d4 34 for my $table ( grep { $_->name } $schema->get_tables ) {
f9356e0d 35 my $table_name_ur = $future->quote($table->name);
f9a5ee79 36
871f55d4 37 my ( @comments );
7a0ceaa1 38
39 push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
871f55d4 40 unless $no_comments;
7a0ceaa1 41
42 push @comments, map { "-- $_" } $table->comments;
43
871f55d4 44 push @foreign_constraints, map $future->foreign_key_constraint($_),
45 grep { $_->type eq FOREIGN_KEY } $table->get_constraints;
7a0ceaa1 46
47 $output .= join( "\n\n",
48 @comments,
871f55d4 49 # index defs
7a16a53e 50 $future->table($table),
871f55d4 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)
7a0ceaa1 58 );
59 }
60
f9a5ee79 61# Add FK constraints
62 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
63
e2fb9ad3 64# create view/procedure are NOT prepended to the input $sql, needs
65# to be filled in with the proper syntax
66
871f55d4 67 return $output;
68}
69
056238d8 701;
71
6fac033a 72=pod
e2fb9ad3 73
056238d8 74=head1 SQLServer Create Table Syntax
75
76TODO
77
78
79=head1 NAME
80
81SQL::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
92B<WARNING>B This is still fairly early code, basically a hacked version of the
93Sybase 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
101List 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
7a0ceaa1 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";
5c5997ef 117 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
3e0bcbfd 118 my $text = $_->sql();
e2fb9ad3 119 $text =~ s/\r//g;
5bb0a4ee 120 $output .= "$text\nGO\n";
7a0ceaa1 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";
5c5997ef 130 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
3e0bcbfd 131 my $text = $_->sql();
028386aa 132 $text =~ s/\r//g;
5bb0a4ee 133 $output .= "$text\nGO\n";
7a0ceaa1 134 }
7a0ceaa1 135
136=head1 SEE ALSO
137
138SQL::Translator.
139
140=head1 AUTHORS
141
142Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
143Sybase producer, I just tweaked it for SQLServer. Thanks.
144
145=cut