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