1 package SQL::Translator::Parser::DBI::SQLServer;
3 # -------------------------------------------------------------------
4 # $Id: SQLServer.pm 1440 2009-01-17 16:31:57Z jawnsy $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::DBI::SQLServer - parser for SQL Server through DBD::ODBC
29 See SQL::Translator::Parser::DBI.
33 Uses DBI Catalog Methods.
39 use SQL::Translator::Schema;
42 use vars qw[ $DEBUG @EXPORT_OK ];
43 $DEBUG = 0 unless defined $DEBUG;
47 # -------------------------------------------------------------------
49 my ( $tr, $dbh ) = @_;
51 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
52 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
53 $dbh->{FetchHashKeyName} = 'NAME_uc';
56 if ($dbh->{ChopBlanks} != 1) {
57 warn "setting dbh attribute {ChopBlanks} to 1";
58 $dbh->{ChopBlanks} = 1;
61 my $schema = $tr->schema;
63 my ($sth, @tables, $columns);
68 # it is much quicker to slurp back everything all at once rather
69 # than make repeated calls
71 $sth = $dbh->column_info(undef, undef, undef, undef);
74 foreach my $c (@{$sth->fetchall_arrayref({})}) {
80 ->{$c->{COLUMN_NAME}}= $c;
85 # Get a list of the tables and views.
86 $sth = $dbh->table_info();
87 @tables = @{$sth->fetchall_arrayref({})};
89 my $h = $dbh->selectall_arrayref(q{
90 SELECT o.name, colid,c.text
101 # I had always thought there was something 'hard' about
102 # reconstructing text from syscomments ..
103 # this seems to work fine and is certainly not complicated!
106 $stuff->{view}->{$_->[0]}->{text} .= $_->[2];
109 #### objects with indexes.
111 $stuff->{indexes}->{$_->[0]}++
113 } @{$dbh->selectall_arrayref("SELECT DISTINCT object_name(id)
115 WHERE indid > 0 and indid < 255 and
116 name not like '_WA_Sys%'")};
120 $stuff->{$_->[1]}->{$_->[0]} = $_;
121 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects")};
126 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
128 my $n = $_->{PROCEDURE_NAME};
129 $n =~ s/;\d+$//; # Ignore versions for now
131 $stuff->{procedures}->{$n} = $_;
132 } values %{$dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME')};
135 # And this blasts in the text of 'legit' stored procedures. Do
136 # this rather than calling sp_helptext in a loop.
138 $h = $dbh->selectall_arrayref(q{
139 SELECT o.name, colid,c.text
143 WHERE o.type in ('P', 'FN', 'TF', 'IF')
148 $stuff->{procedures}->{$_->[0]}->{text} .= $_->[2]
149 if (defined($stuff->{procedures}->{$_->[0]}));
158 # Since the 'target' of the trigger is defined in the text, we will
159 # just create them independently for now rather than associating them
162 $h = $dbh->selectall_arrayref(q{
163 SELECT o.name, colid,c.text
168 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
175 $stuff->{triggers}->{$_->[0]}->{text} .= $_->[2];
182 # Not sure what to do with these?
183 $stuff->{type_info_all} = $dbh->type_info_all;
186 # According to the DBI docs, these can be
191 # "GLOBAL TEMPORARY",
196 foreach my $table_info (@tables) {
198 unless (defined($table_info->{TABLE_TYPE}));
200 if ($table_info->{TABLE_TYPE} eq "TABLE") {
201 my $table = $schema->add_table(
203 $table_info->{TABLE_NAME},
205 $table_info->{TABLE_TYPE},
206 ) || die $schema->error;
208 # find the associated columns
211 $columns->{$table_info->{TABLE_CAT}}
212 ->{$table_info->{TABLE_SCHEM}}
213 ->{$table_info->{TABLE_NAME}}
216 foreach my $c (values %{$cols}) {
217 my $is_auto_increment = $c->{TYPE_NAME} =~ s#(\(\))? identity##i;
218 my $f = $table->add_field(
219 name => $c->{COLUMN_NAME},
220 data_type => $c->{TYPE_NAME},
221 order => $c->{ORDINAL_POSITION},
222 size => $c->{COLUMN_SIZE},
223 ) || die $table->error;
224 $f->is_nullable($c->{NULLABLE} == 1);
225 $f->is_auto_increment($is_auto_increment);
226 if ( defined $c->{COLUMN_DEF}) {
227 $c->{COLUMN_DEF} =~ s#\('?(.*?)'?\)#$1#;
228 $f->default_value($c->{COLUMN_DEF});
233 my $h = $dbh->selectall_hashref("sp_pkeys
234 $table_info->{TABLE_NAME}", 'COLUMN_NAME');
235 if (scalar keys %{$h} >= 1) {
239 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
242 $table->primary_key(@c)
246 # add in foreign keys
247 $h = $dbh->selectall_hashref("sp_fkeys NULL,
248 \@fktable_name = '$table_info->{TABLE_NAME}'", 'FK_NAME');
249 foreach my $fk ( values %{$h} ) {
250 my $constraint = $table->add_constraint( name => $fk->{FK_NAME},
251 fields => [$fk->{FKCOLUMN_NAME}],
253 $constraint->type("FOREIGN_KEY");
254 $constraint->on_delete(
255 $fk->{DELETE_RULE} == 0 ? "CASCADE" :
256 $fk->{DELETE_RULE} == 1 ? "NO ACTION" : "SET_NULL"
258 $constraint->on_update(
259 $fk->{UPDATE_RULE} == 0 ? "CASCADE" :
260 $fk->{UPDATE_RULE} == 1 ? "NO ACTION" : "SET_NULL"
262 $constraint->reference_table($fk->{PKTABLE_NAME});
265 # add in any indexes ... how do we tell if the index has
266 # already been created as part of a primary key or other
269 if (defined($stuff->{indexes}->{$table_info->{TABLE_NAME}})){
270 my $h = $dbh->selectall_hashref("sp_helpindex
271 $table_info->{TABLE_NAME}", 'INDEX_NAME');
272 foreach (values %{$h}) {
273 my $fields = $_->{'INDEX_KEYS'};
275 my $i = $table->add_index(
280 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
283 # we could make this a primary key if there
284 # isn't already one defined and if there
285 # aren't any nullable columns in thisindex.
287 if (!defined($table->primary_key())) {
288 $table->primary_key($fields)
290 $table->get_field($_)->is_nullable()
291 } split(/,\s*/, $fields);
296 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
297 next if $table_info->{TABLE_NAME} eq 'sysconstraints'
298 || $table_info->{TABLE_NAME} eq 'syssegments';
299 next if !$stuff->{view}->{$table_info->{TABLE_NAME}}->{text};
300 my $view = $schema->add_view(
302 $table_info->{TABLE_NAME},
307 $columns->{$table_info->{TABLE_CAT}}
308 ->{$table_info->{TABLE_SCHEM}}
309 ->{$table_info->{TABLE_NAME}}
315 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
319 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
320 if (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
324 foreach my $p (values %{$stuff->{procedures}}) {
326 my $proc = $schema->add_procedure(
328 owner => $p->{PROCEDURE_OWNER},
329 comments => $p->{REMARKS},
345 # -------------------------------------------------------------------
351 Chris Hilton E<lt>chris@dctank.comE<gt> - Bulk of code from
352 DBI-Sybase parser, I just tweaked it for SQLServer. Thanks.
356 DBI, DBD::ODBC, SQL::Translator::Schema.