1 package SQL::Translator::Parser::DBI::Sybase;
3 # -------------------------------------------------------------------
4 # $Id: Sybase.pm,v 1.6 2004-07-30 16:13:52 phrrngtn Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 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::Sybase - parser for DBD::Sybase
29 See SQL::Translator::Parser::DBI.
33 Uses DBI Catalog Methods.
39 use SQL::Translator::Schema;
42 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
43 $VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/;
44 $DEBUG = 0 unless defined $DEBUG;
48 # -------------------------------------------------------------------
50 my ( $tr, $dbh ) = @_;
52 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
53 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
54 $dbh->{FetchHashKeyName} = 'NAME_uc';
57 if ($dbh->{ChopBlanks} != 1) {
58 warn "setting dbh attribute {ChopBlanks} to 1";
59 $dbh->{ChopBlanks} = 1;
62 my $schema = $tr->schema;
64 my ($sth, @tables, $columns);
69 # it is much quicker to slurp back everything all at once rather
70 # than make repeated calls
72 $sth = $dbh->column_info(undef, undef, undef, undef);
75 foreach my $c (@{$sth->fetchall_arrayref({})}) {
81 ->{$c->{COLUMN_NAME}}= $c;
86 # Get a list of the tables and views.
87 $sth = $dbh->table_info();
88 @tables = @{$sth->fetchall_arrayref({})};
90 my $h = $dbh->selectall_arrayref(q{
91 SELECT o.name, colid,colid2,c.text
103 # I had always thought there was something 'hard' about
104 # reconstructing text from syscomments ..
105 # this seems to work fine and is certainly not complicated!
108 $stuff->{view}->{$_->[0]}->{text} .= $_->[3];
111 #### objects with indexes.
113 $stuff->{indexes}->{$_->[0]}++
115 } @{$dbh->selectall_arrayref("SELECT DISTINCT object_name(id) AS name
121 $stuff->{$_->[1]}->{$_->[0]} = $_;
122 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects")};
127 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
129 my $n = $_->{PROCEDURE_NAME};
130 $n =~ s/;\d+$//; # Ignore versions for now
132 $stuff->{procedures}->{$n} = $_;
133 } values %{$dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME')};
136 # And this blasts in the text of 'legit' stored procedures. Do
137 # this rather than calling sp_helptext in a loop.
139 $h = $dbh->selectall_arrayref(q{
140 SELECT o.name, colid,colid2,c.text
152 $stuff->{procedures}->{$_->[0]}->{text} .= $_->[3]
153 if (defined($stuff->{procedures}->{$_->[0]}));
162 # Since the 'target' of the trigger is defined in the text, we will
163 # just create them independently for now rather than associating them
166 $h = $dbh->selectall_arrayref(q{
167 SELECT o.name, colid,colid2,c.text
172 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
180 $stuff->{triggers}->{$_->[0]}->{text} .= $_->[3];
187 # Not sure what to do with these?
188 $stuff->{type_info_all} = $dbh->type_info_all;
191 # According to the DBI docs, these can be
196 # "GLOBAL TEMPORARY",
201 foreach my $table_info (@tables) {
203 unless (defined($table_info->{TABLE_TYPE}));
205 if ($table_info->{TABLE_TYPE} =~ /TABLE/) {
206 my $table = $schema->add_table(
208 $table_info->{TABLE_NAME},
210 $table_info->{TABLE_TYPE},
211 ) || die $schema->error;
213 # find the associated columns
216 $columns->{$table_info->{TABLE_QUALIFIER}}
217 ->{$table_info->{TABLE_OWNER}}
218 ->{$table_info->{TABLE_NAME}}
221 foreach my $c (values %{$cols}) {
222 my $f = $table->add_field(
223 name => $c->{COLUMN_NAME},
224 data_type => $c->{TYPE_NAME},
225 order => $c->{ORDINAL_POSITION},
226 size => $c->{COLUMN_SIZE},
227 ) || die $table->error;
230 if ($c->{NULLABLE} == 1);
234 my $h = $dbh->selectall_hashref("sp_pkeys
235 $table_info->{TABLE_NAME}", 'COLUMN_NAME');
236 if (scalar keys %{$h} > 1) {
240 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
243 $table->primary_key(@c)
247 # add in any indexes ... how do we tell if the index has
248 # already been created as part of a primary key or other
251 if (defined($stuff->{indexes}->{$table_info->{TABLE_NAME}})){
252 my $h = $dbh->selectall_hashref("sp_helpindex
253 $table_info->{TABLE_NAME}", 'INDEX_NAME');
254 foreach (values %{$h}) {
255 my $fields = $_->{'INDEX_KEYS'};
257 my $i = $table->add_index(
262 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
265 # we could make this a primary key if there
266 # isn't already one defined and if there
267 # aren't any nullable columns in thisindex.
269 if (!defined($table->primary_key())) {
270 $table->primary_key($fields)
272 $table->get_field($_)->is_nullable()
273 } split(/,\s*/, $fields);
278 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
279 my $view = $schema->add_view(
281 $table_info->{TABLE_NAME},
286 $columns->{$table_info->{TABLE_QUALIFIER}}
287 ->{$table_info->{TABLE_OWNER}}
288 ->{$table_info->{TABLE_NAME}}
294 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
298 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
299 if (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
303 foreach my $p (values %{$stuff->{procedures}}) {
304 my $proc = $schema->add_procedure(
306 owner => $p->{PROCEDURE_OWNER},
307 comments => $p->{REMARKS},
323 # -------------------------------------------------------------------
329 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
333 DBI, DBD::Sybase, SQL::Translator::Schema.