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