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