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