1 package SQL::Translator::Parser::DBI::Sybase;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::DBI::Sybase - parser for DBD::Sybase
27 See SQL::Translator::Parser::DBI.
31 Uses DBI Catalog Methods.
37 use SQL::Translator::Schema;
40 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
42 $DEBUG = 0 unless defined $DEBUG;
46 # -------------------------------------------------------------------
48 my ( $tr, $dbh ) = @_;
50 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
51 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
52 $dbh->{FetchHashKeyName} = 'NAME_uc';
55 if ($dbh->{ChopBlanks} != 1) {
56 warn "setting dbh attribute {ChopBlanks} to 1";
57 $dbh->{ChopBlanks} = 1;
60 my $schema = $tr->schema;
62 my ($sth, @tables, $columns);
67 # it is much quicker to slurp back everything all at once rather
68 # than make repeated calls
70 $sth = $dbh->column_info(undef, undef, undef, undef);
73 foreach my $c (@{$sth->fetchall_arrayref({})}) {
79 ->{$c->{COLUMN_NAME}}= $c;
84 # Get a list of the tables and views.
85 $sth = $dbh->table_info();
86 @tables = @{$sth->fetchall_arrayref({})};
88 my $h = $dbh->selectall_arrayref(q{
89 SELECT o.name, colid,colid2,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} .= $_->[3];
109 #### objects with indexes.
111 $stuff->{indexes}->{$_->[0]}++
113 } @{$dbh->selectall_arrayref("SELECT DISTINCT object_name(id) AS name
119 $stuff->{$_->[1]}->{$_->[0]} = $_;
120 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects")};
125 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
127 my $n = $_->{PROCEDURE_NAME};
128 $n =~ s/;\d+$//; # Ignore versions for now
130 $stuff->{procedures}->{$n} = $_;
131 } values %{$dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME')};
134 # And this blasts in the text of 'legit' stored procedures. Do
135 # this rather than calling sp_helptext in a loop.
137 $h = $dbh->selectall_arrayref(q{
138 SELECT o.name, colid,colid2,c.text
150 $stuff->{procedures}->{$_->[0]}->{text} .= $_->[3]
151 if (defined($stuff->{procedures}->{$_->[0]}));
160 # Since the 'target' of the trigger is defined in the text, we will
161 # just create them independently for now rather than associating them
164 $h = $dbh->selectall_arrayref(q{
165 SELECT o.name, colid,colid2,c.text
170 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
178 $stuff->{triggers}->{$_->[0]}->{text} .= $_->[3];
185 # Not sure what to do with these?
186 $stuff->{type_info_all} = $dbh->type_info_all;
189 # According to the DBI docs, these can be
194 # "GLOBAL TEMPORARY",
199 foreach my $table_info (@tables) {
201 unless (defined($table_info->{TABLE_TYPE}));
203 if ($table_info->{TABLE_TYPE} =~ /TABLE/) {
204 my $table = $schema->add_table(
206 $table_info->{TABLE_NAME},
208 $table_info->{TABLE_TYPE},
209 ) || die $schema->error;
211 # find the associated columns
214 $columns->{$table_info->{TABLE_QUALIFIER}}
215 ->{$table_info->{TABLE_OWNER}}
216 ->{$table_info->{TABLE_NAME}}
219 foreach my $c (values %{$cols}) {
220 my $f = $table->add_field(
221 name => $c->{COLUMN_NAME},
222 data_type => $c->{TYPE_NAME},
223 order => $c->{ORDINAL_POSITION},
224 size => $c->{COLUMN_SIZE},
225 ) || die $table->error;
228 if ($c->{NULLABLE} == 1);
232 my $h = $dbh->selectall_hashref("sp_pkeys
233 $table_info->{TABLE_NAME}", 'COLUMN_NAME');
234 if (scalar keys %{$h} > 1) {
238 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
241 $table->primary_key(@c)
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 my $view = $schema->add_view(
279 $table_info->{TABLE_NAME},
284 $columns->{$table_info->{TABLE_QUALIFIER}}
285 ->{$table_info->{TABLE_OWNER}}
286 ->{$table_info->{TABLE_NAME}}
292 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
296 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
297 if (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
301 foreach my $p (values %{$stuff->{procedures}}) {
302 my $proc = $schema->add_procedure(
304 owner => $p->{PROCEDURE_OWNER},
305 comments => $p->{REMARKS},
321 # -------------------------------------------------------------------
327 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
331 DBI, DBD::Sybase, SQL::Translator::Schema.