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