1 package SQL::Translator::Parser::DBI::Sybase;
3 # $Id: Sybase.pm,v 1.3 2003-10-04 00:10:00 phrrngtn 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.3 $ =~ /(\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 name
103 $stuff->{$_->[1]}->{$_->[0]} = $_;
104 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects")};
109 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
111 my $n = $_->{PROCEDURE_NAME};
112 $n =~ s/;\d+$//; # Ignore versions for now
114 $stuff->{procedures}->{$n} = $_;
115 } values %{$dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME')};
118 # And this blasts in the text of 'legit' stored procedures. Do
119 # this rather than calling sp_helptext in a loop.
121 $h = $dbh->selectall_arrayref(q{
122 SELECT o.name, colid,colid2,c.text
134 $stuff->{procedures}->{$_->[0]}->{text} .= $_->[3]
135 if (defined($stuff->{procedures}->{$_->[0]}));
144 # Since the 'target' of the trigger is defined in the text, we will
145 # just create them independently for now rather than associating them
148 $h = $dbh->selectall_arrayref(q{
149 SELECT o.name, colid,colid2,c.text
154 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
162 $stuff->{triggers}->{$_->[0]}->{text} .= $_->[3];
169 # Not sure what to do with these?
170 $stuff->{type_info_all} = $dbh->type_info_all;
173 # According to the DBI docs, these can be
178 # "GLOBAL TEMPORARY",
183 foreach my $table_info (@tables) {
185 unless (defined($table_info->{TABLE_TYPE}));
187 if ($table_info->{TABLE_TYPE} =~ /TABLE/) {
188 my $table = $schema->add_table(
190 $table_info->{TABLE_NAME},
192 $table_info->{TABLE_TYPE},
193 ) || die $schema->error;
195 # find the associated columns
198 $columns->{$table_info->{TABLE_QUALIFIER}}
199 ->{$table_info->{TABLE_OWNER}}
200 ->{$table_info->{TABLE_NAME}}
203 foreach my $c (values %{$cols}) {
204 my $f = $table->add_field(
205 name => $c->{COLUMN_NAME},
206 data_type => $c->{TYPE_NAME},
207 order => $c->{ORDINAL_POSITION},
208 size => $c->{COLUMN_SIZE},
209 ) || die $table->error;
212 if ($c->{NULLABLE} == 1);
216 my $h = $dbh->selectall_hashref("sp_pkeys
217 $table_info->{TABLE_NAME}", 'COLUMN_NAME');
218 if (scalar keys %{$h} > 1) {
222 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
225 $table->primary_key(@c)
229 # add in any indexes ... how do we tell if the index has
230 # already been created as part of a primary key or other
233 if (defined($stuff->{indexes}->{$table_info->{TABLE_NAME}})){
234 my $h = $dbh->selectall_hashref("sp_helpindex
235 $table_info->{TABLE_NAME}", 'INDEX_NAME');
236 foreach (values %{$h}) {
237 my $fields = $_->{'INDEX_KEYS'};
239 my $i = $table->add_index(
244 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
247 # we could make this a primary key if there
248 # isn't already one defined and if there
249 # aren't any nullable columns in thisindex.
251 if (!defined($table->primary_key())) {
252 $table->primary_key($fields)
254 $table->get_field($_)->is_nullable()
255 } split(/,\s*/, $fields);
260 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
261 my $view = $schema->add_view(
263 $table_info->{TABLE_NAME},
268 $columns->{$table_info->{TABLE_QUALIFIER}}
269 ->{$table_info->{TABLE_OWNER}}
270 ->{$table_info->{TABLE_NAME}}
276 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
280 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
281 if (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
285 foreach my $p (values %{$stuff->{procedures}}) {
286 my $proc = $schema->add_procedure(
288 owner => $p->{PROCEDURE_OWNER},
289 comments => $p->{REMARKS},
308 Paul Harrington E<lt>harringp@deshaw.comE<gt>,
312 DBI, DBD::Sybase, SQL::Translator::Schema.