1 package SQL::Translator::Parser::DBI::SQLServer;
5 SQL::Translator::Parser::DBI::SQLServer - parser for SQL Server through DBD::ODBC
9 See SQL::Translator::Parser::DBI.
13 Uses DBI Catalog Methods.
19 use SQL::Translator::Schema;
22 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
24 $DEBUG = 0 unless defined $DEBUG;
29 my ( $tr, $dbh ) = @_;
31 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
32 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
33 $dbh->{FetchHashKeyName} = 'NAME_uc';
36 if ($dbh->{ChopBlanks} != 1) {
37 warn "setting dbh attribute {ChopBlanks} to 1";
38 $dbh->{ChopBlanks} = 1;
41 my $schema = $tr->schema;
43 my ($sth, @tables, $columns);
48 # it is much quicker to slurp back everything all at once rather
49 # than make repeated calls
51 $sth = $dbh->column_info(undef, undef, undef, undef);
54 foreach my $c (@{$sth->fetchall_arrayref({})}) {
60 ->{$c->{COLUMN_NAME}}= $c;
65 # Get a list of the tables and views.
66 $sth = $dbh->table_info();
67 @tables = @{$sth->fetchall_arrayref({})};
69 my $h = $dbh->selectall_arrayref(q{
70 SELECT o.name, colid,c.text
81 # I had always thought there was something 'hard' about
82 # reconstructing text from syscomments ..
83 # this seems to work fine and is certainly not complicated!
86 $stuff->{view}->{$_->[0]}->{text} .= $_->[2];
89 #### objects with indexes.
91 $stuff->{indexes}->{$_->[0]}++
93 } @{$dbh->selectall_arrayref("SELECT DISTINCT object_name(id)
95 WHERE indid > 0 and indid < 255 and
96 name not like '_WA_Sys%'")};
100 $stuff->{$_->[1]}->{$_->[0]} = $_;
101 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects")};
106 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
108 my $n = $_->{PROCEDURE_NAME};
109 $n =~ s/;\d+$//; # Ignore versions for now
111 $stuff->{procedures}->{$n} = $_;
112 } values %{$dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME')};
115 # And this blasts in the text of 'legit' stored procedures. Do
116 # this rather than calling sp_helptext in a loop.
118 $h = $dbh->selectall_arrayref(q{
119 SELECT o.name, colid,c.text
123 WHERE o.type in ('P', 'FN', 'TF', 'IF')
128 $stuff->{procedures}->{$_->[0]}->{text} .= $_->[2]
129 if (defined($stuff->{procedures}->{$_->[0]}));
138 # Since the 'target' of the trigger is defined in the text, we will
139 # just create them independently for now rather than associating them
142 $h = $dbh->selectall_arrayref(q{
143 SELECT o.name, colid,c.text
148 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
155 $stuff->{triggers}->{$_->[0]}->{text} .= $_->[2];
162 # Not sure what to do with these?
163 $stuff->{type_info_all} = $dbh->type_info_all;
166 # According to the DBI docs, these can be
171 # "GLOBAL TEMPORARY",
176 foreach my $table_info (@tables) {
178 unless (defined($table_info->{TABLE_TYPE}));
180 if ($table_info->{TABLE_TYPE} eq "TABLE") {
181 my $table = $schema->add_table(
183 $table_info->{TABLE_NAME},
185 $table_info->{TABLE_TYPE},
186 ) || die $schema->error;
188 # find the associated columns
191 $columns->{$table_info->{TABLE_CAT}}
192 ->{$table_info->{TABLE_SCHEM}}
193 ->{$table_info->{TABLE_NAME}}
196 foreach my $c (values %{$cols}) {
197 my $is_auto_increment = $c->{TYPE_NAME} =~ s#(\(\))? identity##i;
198 my $f = $table->add_field(
199 name => $c->{COLUMN_NAME},
200 data_type => $c->{TYPE_NAME},
201 order => $c->{ORDINAL_POSITION},
202 size => $c->{COLUMN_SIZE},
203 ) || die $table->error;
204 $f->is_nullable($c->{NULLABLE} == 1);
205 $f->is_auto_increment($is_auto_increment);
206 if ( defined $c->{COLUMN_DEF}) {
207 $c->{COLUMN_DEF} =~ s#\('?(.*?)'?\)#$1#;
208 $f->default_value($c->{COLUMN_DEF});
213 my $h = $dbh->selectall_hashref("sp_pkeys
214 $table_info->{TABLE_NAME}", 'COLUMN_NAME');
215 if (scalar keys %{$h} >= 1) {
219 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
222 $table->primary_key(@c)
226 # add in foreign keys
227 $h = $dbh->selectall_hashref("sp_fkeys NULL,
228 \@fktable_name = '$table_info->{TABLE_NAME}'", 'FK_NAME');
229 foreach my $fk ( values %{$h} ) {
230 my $constraint = $table->add_constraint( name => $fk->{FK_NAME},
231 fields => [$fk->{FKCOLUMN_NAME}],
233 $constraint->type("FOREIGN_KEY");
234 $constraint->on_delete(
235 $fk->{DELETE_RULE} == 0 ? "CASCADE" :
236 $fk->{DELETE_RULE} == 1 ? "NO ACTION" : "SET_NULL"
238 $constraint->on_update(
239 $fk->{UPDATE_RULE} == 0 ? "CASCADE" :
240 $fk->{UPDATE_RULE} == 1 ? "NO ACTION" : "SET_NULL"
242 $constraint->reference_table($fk->{PKTABLE_NAME});
245 # add in any indexes ... how do we tell if the index has
246 # already been created as part of a primary key or other
249 if (defined($stuff->{indexes}->{$table_info->{TABLE_NAME}})){
250 my $h = $dbh->selectall_hashref("sp_helpindex
251 $table_info->{TABLE_NAME}", 'INDEX_NAME');
252 foreach (values %{$h}) {
253 my $fields = $_->{'INDEX_KEYS'};
255 my $i = $table->add_index(
260 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
263 # we could make this a primary key if there
264 # isn't already one defined and if there
265 # aren't any nullable columns in thisindex.
267 if (!defined($table->primary_key())) {
268 $table->primary_key($fields)
270 $table->get_field($_)->is_nullable()
271 } split(/,\s*/, $fields);
276 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
277 next if $table_info->{TABLE_NAME} eq 'sysconstraints'
278 || $table_info->{TABLE_NAME} eq 'syssegments';
279 next if !$stuff->{view}->{$table_info->{TABLE_NAME}}->{text};
280 my $view = $schema->add_view(
282 $table_info->{TABLE_NAME},
287 $columns->{$table_info->{TABLE_CAT}}
288 ->{$table_info->{TABLE_SCHEM}}
289 ->{$table_info->{TABLE_NAME}}
295 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
299 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
300 if (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
304 foreach my $p (values %{$stuff->{procedures}}) {
306 my $proc = $schema->add_procedure(
308 owner => $p->{PROCEDURE_OWNER},
309 comments => $p->{REMARKS},
329 Chris Hilton E<lt>chris@dctank.comE<gt> - Bulk of code from
330 DBI-Sybase parser, I just tweaked it for SQLServer. Thanks.
334 DBI, DBD::ODBC, SQL::Translator::Schema.