1 package SQL::Translator::Parser::DBI::Sybase;
3 # $Id: Sybase.pm,v 1.2 2003-10-03 20:56:40 kycl4rk Exp $
7 SQL::Translator::Parser::DBI::Sybase - parser for DBD::Sybase
11 See SQL::Translator::Parser::DBI.
15 Uses DBI Catalog Methods.
21 use SQL::Translator::Schema;
24 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
25 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
26 $DEBUG = 0 unless defined $DEBUG;
30 # -------------------------------------------------------------------
32 my ( $tr, $dbh ) = @_;
34 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
35 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
36 $dbh->{FetchHashKeyName} = 'NAME_uc';
39 if ($dbh->{ChopBlanks} != 1) {
40 warn "setting dbh attribute {ChopBlanks} to 1";
41 $dbh->{ChopBlanks} = 1;
44 my $schema = $tr->schema;
46 my ($sth, @tables, $columns);
51 # it is much quicker to slurp back everything all at once rather
52 # than make repeated calls
54 $sth = $dbh->column_info();
57 foreach my $c (@{$sth->fetchall_arrayref({})}) {
63 ->{$c->{COLUMN_NAME}}= $c;
68 # Get a list of the tables and views.
69 $sth = $dbh->table_info();
70 @tables = @{$sth->fetchall_arrayref({})};
72 my $h = $dbh->selectall_arrayref(q{
73 SELECT o.name, colid,colid2,c.text
85 # I had always thought there was something 'hard' about
86 # reconstructing text from syscomments ..
87 # this seems to work fine and is certainly not complicated!
90 $stuff->{view}->{$_->[0]}->{text} .= $_->[3];
93 #### objects with indexes.
95 $stuff->{indexes}->{$_->[0]}++
97 } @{$dbh->selectall_arrayref("SELECT DISTINCT object_name(id) AS
105 $stuff->{$_->[1]}->{$_->[0]} = $_;
106 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM
123 $stuff->{type_info_all} = $dbh->type_info_all;
126 # According to the DBI docs, these can be
131 # "GLOBAL TEMPORARY",
136 foreach my $table_info (@tables) {
138 unless (defined($table_info->{TABLE_TYPE}));
140 if ($table_info->{TABLE_TYPE} =~ /TABLE/) {
141 my $table = $schema->add_table(
143 $table_info->{TABLE_NAME},
145 $table_info->{TABLE_TYPE},
146 ) || die $schema->error;
148 # find the associated columns
151 $columns->{$table_info->{TABLE_QUALIFIER}}
152 ->{$table_info->{TABLE_OWNER}}
153 ->{$table_info->{TABLE_NAME}}
156 foreach my $c (values %{$cols}) {
157 my $f = $table->add_field(name =>
162 $c->{ORDINAL_POSITION},
165 ) || die $table->error;
167 if ($c->{NULLABLE} == 1);
171 my $h = $dbh->selectall_hashref("sp_pkeys
172 $table_info->{TABLE_NAME}", 'COLUMN_NAME');
173 if (scalar keys %{$h} > 1) {
177 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
180 $table->primary_key(@c)
184 # add in any indexes ... how do we tell if the index has
185 # already been created as part of a primary key or other
188 if (defined($stuff->{indexes}->{$table_info->{TABLE_NAME}}))
190 my $h = $dbh->selectall_hashref("sp_helpindex
191 $table_info->{TABLE_NAME}", 'INDEX_NAME');
192 foreach (values %{$h}) {
193 my $fields = $_->{'INDEX_KEYS'};
195 my $i = $table->add_index(
200 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
203 # we could make this a primary key if there
204 # isn't already one defined and if there
205 # aren't any nullable columns in thisindex.
207 if (!defined($table->primary_key())) {
208 $table->primary_key($fields)
210 $table->get_field($_)->is_nullable()
211 } split(/,\s*/, $fields);
216 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
217 my $view = $schema->add_view(
219 $table_info->{TABLE_NAME},
223 $columns->{$table_info->{TABLE_QUALIFIER}}
224 ->{$table_info->{TABLE_OWNER}}
225 ->{$table_info->{TABLE_NAME}}
231 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
236 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
238 (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
255 Paul Harrington E<lt>harringp@deshaw.comE<gt>,
259 DBI, DBD::Sybase, SQL::Translator::Schema.