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;
15 my $translator = shift;
16 my $future = SQL::Translator::Generator::DDL::SQLServer->new(
17 add_comments => !$translator->no_comments,
18 add_drop_tables => $translator->add_drop_table,
21 my $no_comments = $translator->no_comments;
22 my $add_drop_table = $translator->add_drop_table;
23 my $schema = $translator->schema;
26 $output .= header_comment."\n" unless ($no_comments);
28 # Generate the DROP statements.
29 $output .= $future->drop_tables;
31 # these need to be added separately, as tables may not exist yet
32 my @foreign_constraints = ();
34 for my $table ( grep { $_->name } $schema->get_tables ) {
35 my $table_name_ur = $future->quote($table->name);
39 push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
42 push @comments, map { "-- $_" } $table->comments;
44 push @foreign_constraints, map $future->foreign_key_constraint($_),
45 grep { $_->type eq FOREIGN_KEY } $table->get_constraints;
47 $output .= join( "\n\n",
50 $future->table($table),
51 (map $future->unique_constraint_multiple($_),
54 grep { $_->is_nullable } $_->fields
55 } $table->get_constraints),
57 (map $future->index($_), $table->get_indices)
62 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
64 # create view/procedure are NOT prepended to the input $sql, needs
65 # to be filled in with the proper syntax
74 =head1 SQLServer Create Table Syntax
81 SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
87 my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
92 B<WARNING>B This is still fairly early code, basically a hacked version of the
93 Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
95 =head1 Extra Attributes
101 List of values for an enum field.
107 * !! Write some tests !!
108 * Reserved words list needs updating to SQLServer.
109 * Triggers, Procedures and Views DO NOT WORK
112 # Text of view is already a 'create view' statement so no need to
114 foreach ( $schema->get_views ) {
115 my $name = $_->name();
117 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
118 my $text = $_->sql();
120 $output .= "$text\nGO\n";
123 # Text of procedure already has the 'create procedure' stuff
124 # so there is no need to do anything fancy. However, we should
125 # think about doing fancy stuff with granting permissions and
127 foreach ( $schema->get_procedures ) {
128 my $name = $_->name();
130 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
131 my $text = $_->sql();
133 $output .= "$text\nGO\n";
142 Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
143 Sybase producer, I just tweaked it for SQLServer. Thanks.