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;
47 my ( $tr, $dbh ) = @_;
49 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
50 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
51 $dbh->{FetchHashKeyName} = 'NAME_uc';
54 if ($dbh->{ChopBlanks} != 1) {
55 warn "setting dbh attribute {ChopBlanks} to 1";
56 $dbh->{ChopBlanks} = 1;
59 my $schema = $tr->schema;
61 my ($sth, @tables, $columns);
66 # it is much quicker to slurp back everything all at once rather
67 # than make repeated calls
69 $sth = $dbh->column_info(undef, undef, undef, undef);
72 foreach my $c (@{$sth->fetchall_arrayref({})}) {
78 ->{$c->{COLUMN_NAME}}= $c;
83 # Get a list of the tables and views.
84 $sth = $dbh->table_info();
85 @tables = @{$sth->fetchall_arrayref({})};
87 my $h = $dbh->selectall_arrayref(q{
88 SELECT o.name, colid,colid2,c.text
100 # I had always thought there was something 'hard' about
101 # reconstructing text from syscomments ..
102 # this seems to work fine and is certainly not complicated!
105 $stuff->{view}->{$_->[0]}->{text} .= $_->[3];
108 #### objects with indexes.
110 $stuff->{indexes}->{$_->[0]}++
112 } @{$dbh->selectall_arrayref("SELECT DISTINCT object_name(id) AS name
118 $stuff->{$_->[1]}->{$_->[0]} = $_;
119 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects")};
124 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
126 my $n = $_->{PROCEDURE_NAME};
127 $n =~ s/;\d+$//; # Ignore versions for now
129 $stuff->{procedures}->{$n} = $_;
130 } values %{$dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME')};
133 # And this blasts in the text of 'legit' stored procedures. Do
134 # this rather than calling sp_helptext in a loop.
136 $h = $dbh->selectall_arrayref(q{
137 SELECT o.name, colid,colid2,c.text
149 $stuff->{procedures}->{$_->[0]}->{text} .= $_->[3]
150 if (defined($stuff->{procedures}->{$_->[0]}));
159 # Since the 'target' of the trigger is defined in the text, we will
160 # just create them independently for now rather than associating them
163 $h = $dbh->selectall_arrayref(q{
164 SELECT o.name, colid,colid2,c.text
169 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
177 $stuff->{triggers}->{$_->[0]}->{text} .= $_->[3];
184 # Not sure what to do with these?
185 $stuff->{type_info_all} = $dbh->type_info_all;
188 # According to the DBI docs, these can be
193 # "GLOBAL TEMPORARY",
198 foreach my $table_info (@tables) {
200 unless (defined($table_info->{TABLE_TYPE}));
202 if ($table_info->{TABLE_TYPE} =~ /TABLE/) {
203 my $table = $schema->add_table(
205 $table_info->{TABLE_NAME},
207 $table_info->{TABLE_TYPE},
208 ) || die $schema->error;
210 # find the associated columns
213 $columns->{$table_info->{TABLE_QUALIFIER}}
214 ->{$table_info->{TABLE_OWNER}}
215 ->{$table_info->{TABLE_NAME}}
218 foreach my $c (values %{$cols}) {
219 my $f = $table->add_field(
220 name => $c->{COLUMN_NAME},
221 data_type => $c->{TYPE_NAME},
222 order => $c->{ORDINAL_POSITION},
223 size => $c->{COLUMN_SIZE},
224 ) || die $table->error;
227 if ($c->{NULLABLE} == 1);
231 my $h = $dbh->selectall_hashref("sp_pkeys
232 $table_info->{TABLE_NAME}", 'COLUMN_NAME');
233 if (scalar keys %{$h} > 1) {
237 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
240 $table->primary_key(@c)
244 # add in any indexes ... how do we tell if the index has
245 # already been created as part of a primary key or other
248 if (defined($stuff->{indexes}->{$table_info->{TABLE_NAME}})){
249 my $h = $dbh->selectall_hashref("sp_helpindex
250 $table_info->{TABLE_NAME}", 'INDEX_NAME');
251 foreach (values %{$h}) {
252 my $fields = $_->{'INDEX_KEYS'};
254 my $i = $table->add_index(
259 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
262 # we could make this a primary key if there
263 # isn't already one defined and if there
264 # aren't any nullable columns in thisindex.
266 if (!defined($table->primary_key())) {
267 $table->primary_key($fields)
269 $table->get_field($_)->is_nullable()
270 } split(/,\s*/, $fields);
275 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
276 my $view = $schema->add_view(
278 $table_info->{TABLE_NAME},
283 $columns->{$table_info->{TABLE_QUALIFIER}}
284 ->{$table_info->{TABLE_OWNER}}
285 ->{$table_info->{TABLE_NAME}}
291 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
295 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
296 if (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
300 foreach my $p (values %{$stuff->{procedures}}) {
301 my $proc = $schema->add_procedure(
303 owner => $p->{PROCEDURE_OWNER},
304 comments => $p->{REMARKS},
324 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
328 DBI, DBD::Sybase, SQL::Translator::Schema.