1 package SQL::Translator::Producer::SQLServer;
7 $DEBUG = 1 unless defined $DEBUG;
10 use SQL::Translator::Schema::Constants;
11 use SQL::Translator::Utils qw(debug header_comment);
12 use SQL::Translator::Generator::DDL::SQLServer;
14 my $future = SQL::Translator::Generator::DDL::SQLServer->new();
17 my $translator = shift;
18 my $no_comments = $translator->no_comments;
19 my $add_drop_table = $translator->add_drop_table;
20 my $schema = $translator->schema;
23 $output .= header_comment."\n" unless ($no_comments);
25 # Generate the DROP statements.
26 if ($add_drop_table) {
27 my @tables = sort { $b->order <=> $a->order } $schema->get_tables;
28 $output .= "--\n-- Turn off constraints\n--\n\n" unless $no_comments;
29 foreach my $table (@tables) {
30 $output .= $future->remove_table_constraints($table)
33 $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments;
34 foreach my $table (@tables) {
35 $output .= $future->drop_table($table);
39 # these need to be added separately, as tables may not exist yet
40 my @foreign_constraints = ();
42 for my $table ( grep { $_->name } $schema->get_tables ) {
43 my $table_name_ur = unreserve($table->name);
47 push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
50 push @comments, map { "-- $_" } $table->comments;
52 push @foreign_constraints, map $future->foreign_key_constraint($_),
53 grep { $_->type eq FOREIGN_KEY } $table->get_constraints;
55 $output .= join( "\n\n",
58 $future->table($table),
59 (map $future->unique_constraint_multiple($_),
62 grep { $_->is_nullable } $_->fields
63 } $table->get_constraints),
65 (map $future->index($_), $table->get_indices)
70 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
72 # create view/procedure are NOT prepended to the input $sql, needs
73 # to be filled in with the proper syntax
78 sub unreserve { $future->quote($_[0]) }
84 =head1 SQLServer Create Table Syntax
91 SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
97 my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
102 B<WARNING>B This is still fairly early code, basically a hacked version of the
103 Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
105 =head1 Extra Attributes
111 List of values for an enum field.
117 * !! Write some tests !!
118 * Reserved words list needs updating to SQLServer.
119 * Triggers, Procedures and Views DO NOT WORK
122 # Text of view is already a 'create view' statement so no need to
124 foreach ( $schema->get_views ) {
125 my $name = $_->name();
127 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
128 my $text = $_->sql();
130 $output .= "$text\nGO\n";
133 # Text of procedure already has the 'create procedure' stuff
134 # so there is no need to do anything fancy. However, we should
135 # think about doing fancy stuff with granting permissions and
137 foreach ( $schema->get_procedures ) {
138 my $name = $_->name();
140 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
141 my $text = $_->sql();
143 $output .= "$text\nGO\n";
152 Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
153 Sybase producer, I just tweaked it for SQLServer. Thanks.