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 . $future->drop_tables($schema);
27 my @foreign_constraints = ();
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;
33 $output .= join( "\n\n",
34 $future->table_comments($table),
36 $future->table($table),
37 (map $future->unique_constraint_multiple($_),
40 grep { $_->is_nullable } $_->fields
41 } $table->get_constraints),
43 (map $future->index($_), $table->get_indices)
48 $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
50 # create view/procedure are NOT prepended to the input $sql, needs
51 # to be filled in with the proper syntax
60 =head1 SQLServer Create Table Syntax
67 SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
73 my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
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 ;-)
81 =head1 Extra Attributes
87 List of values for an enum field.
93 * !! Write some tests !!
94 * Reserved words list needs updating to SQLServer.
95 * Triggers, Procedures and Views DO NOT WORK
98 # Text of view is already a 'create view' statement so no need to
100 foreach ( $schema->get_views ) {
101 my $name = $_->name();
103 $output .= "--\n-- View: $name\n--\n\n" unless $no_comments;
104 my $text = $_->sql();
106 $output .= "$text\nGO\n";
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
113 foreach ( $schema->get_procedures ) {
114 my $name = $_->name();
116 $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
117 my $text = $_->sql();
119 $output .= "$text\nGO\n";
128 Mark Addison E<lt>grommit@users.sourceforge.netE<gt> - Bulk of code from
129 Sybase producer, I just tweaked it for SQLServer. Thanks.