1 package SQL::Translator::Producer::SQLServer;
7 $DEBUG = 1 unless defined $DEBUG;
9 use SQL::Translator::Schema::Constants;
10 use SQL::Translator::Utils qw(debug header_comment);
11 use SQL::Translator::Generator::DDL::SQLServer;
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,
20 my $no_comments = $translator->no_comments;
21 my $add_drop_table = $translator->add_drop_table;
22 my $schema = $translator->schema;
24 my $output = $future->header_comments;
25 $output .= $future->drop_tables($schema);
27 my @foreign_constraints = ();
29 for my $table ( grep { $_->name } $schema->get_tables ) {
30 my $table_name_ur = $future->quote($table->name);
34 push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
37 push @comments, map { "-- $_" } $table->comments;
39 push @foreign_constraints, map $future->foreign_key_constraint($_),
40 grep { $_->type eq FOREIGN_KEY } $table->get_constraints;
42 $output .= join( "\n\n",
45 $future->table($table),
46 (map $future->unique_constraint_multiple($_),
49 grep { $_->is_nullable } $_->fields
50 } $table->get_constraints),
52 (map $future->index($_), $table->get_indices)
57 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
59 # create view/procedure are NOT prepended to the input $sql, needs
60 # to be filled in with the proper syntax
69 =head1 SQLServer Create Table Syntax
76 SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
82 my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
87 B<WARNING>B This is still fairly early code, basically a hacked version of the
88 Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
90 =head1 Extra Attributes
96 List of values for an enum field.
102 * !! Write some tests !!
103 * Reserved words list needs updating to SQLServer.
104 * Triggers, Procedures and Views DO NOT WORK
107 # Text of view is already a 'create view' statement so no need to
109 foreach ( $schema->get_views ) {
110 my $name = $_->name();
112 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
113 my $text = $_->sql();
115 $output .= "$text\nGO\n";
118 # Text of procedure already has the 'create procedure' stuff
119 # so there is no need to do anything fancy. However, we should
120 # think about doing fancy stuff with granting permissions and
122 foreach ( $schema->get_procedures ) {
123 my $name = $_->name();
125 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
126 my $text = $_->sql();
128 $output .= "$text\nGO\n";
137 Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
138 Sybase producer, I just tweaked it for SQLServer. Thanks.