add unique_constraints_multiple and indices
[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
7a0ceaa1 9use SQL::Translator::Schema::Constants;
10use SQL::Translator::Utils qw(debug header_comment);
c661b77d 11use SQL::Translator::Generator::DDL::SQLServer;
0a6e5a56 12
7a0ceaa1 13sub produce {
14 my $translator = shift;
f9356e0d 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
7a0ceaa1 20 my $schema = $translator->schema;
21
f511a415 22 my $output = $future->header_comments
23 . $future->drop_tables($schema);
7a0ceaa1 24
871f55d4 25 for my $table ( grep { $_->name } $schema->get_tables ) {
7a0ceaa1 26 $output .= join( "\n\n",
7a16a53e 27 $future->table($table),
3f9e80bf 28 $future->unique_constraints_multiple($table),
29 $future->indices($table),
7a0ceaa1 30 );
31 }
32
c7091660 33 my @foreign_constraints = $future->foreign_key_constraints($schema);
f9a5ee79 34 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
35
871f55d4 36 return $output;
37}
38
056238d8 391;
40
6fac033a 41=pod
e2fb9ad3 42
056238d8 43=head1 SQLServer Create Table Syntax
44
45TODO
46
47
48=head1 NAME
49
50SQL::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
61B<WARNING>B This is still fairly early code, basically a hacked version of the
62Sybase 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
70List 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
7a0ceaa1 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";
5c5997ef 86 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
3e0bcbfd 87 my $text = $_->sql();
e2fb9ad3 88 $text =~ s/\r//g;
5bb0a4ee 89 $output .= "$text\nGO\n";
7a0ceaa1 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";
5c5997ef 99 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
3e0bcbfd 100 my $text = $_->sql();
028386aa 101 $text =~ s/\r//g;
5bb0a4ee 102 $output .= "$text\nGO\n";
7a0ceaa1 103 }
7a0ceaa1 104
105=head1 SEE ALSO
106
107SQL::Translator.
108
109=head1 AUTHORS
110
111Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
112Sybase producer, I just tweaked it for SQLServer. Thanks.
113
114=cut