1 package SQL::Translator::Parser::DBI::Sybase;
5 SQL::Translator::Parser::DBI::Sybase - parser for DBD::Sybase
9 See SQL::Translator::Parser::DBI.
13 Uses DBI Catalog Methods.
20 use SQL::Translator::Schema;
23 our ( $DEBUG, @EXPORT_OK );
24 our $VERSION = '1.59';
25 $DEBUG = 0 unless defined $DEBUG;
30 my ( $tr, $dbh ) = @_;
32 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
33 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
34 $dbh->{FetchHashKeyName} = 'NAME_uc';
37 if ($dbh->{ChopBlanks} != 1) {
38 warn "setting dbh attribute {ChopBlanks} to 1";
39 $dbh->{ChopBlanks} = 1;
42 my $schema = $tr->schema;
44 my ($sth, @tables, $columns);
49 # it is much quicker to slurp back everything all at once rather
50 # than make repeated calls
52 $sth = $dbh->column_info(undef, undef, undef, undef);
55 foreach my $c (@{$sth->fetchall_arrayref({})}) {
61 ->{$c->{COLUMN_NAME}}= $c;
66 # Get a list of the tables and views.
67 $sth = $dbh->table_info();
68 @tables = @{$sth->fetchall_arrayref({})};
70 my $h = $dbh->selectall_arrayref(q{
71 SELECT o.name, colid,colid2,c.text
83 # I had always thought there was something 'hard' about
84 # reconstructing text from syscomments ..
85 # this seems to work fine and is certainly not complicated!
88 $stuff->{view}->{$_->[0]}->{text} .= $_->[3];
91 #### objects with indexes.
93 $stuff->{indexes}->{$_->[0]}++
95 } @{$dbh->selectall_arrayref("SELECT DISTINCT object_name(id) AS name
101 $stuff->{$_->[1]}->{$_->[0]} = $_;
102 } @{$dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects")};
107 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
109 my $n = $_->{PROCEDURE_NAME};
110 $n =~ s/;\d+$//; # Ignore versions for now
112 $stuff->{procedures}->{$n} = $_;
113 } values %{$dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME')};
116 # And this blasts in the text of 'legit' stored procedures. Do
117 # this rather than calling sp_helptext in a loop.
119 $h = $dbh->selectall_arrayref(q{
120 SELECT o.name, colid,colid2,c.text
132 $stuff->{procedures}->{$_->[0]}->{text} .= $_->[3]
133 if (defined($stuff->{procedures}->{$_->[0]}));
142 # Since the 'target' of the trigger is defined in the text, we will
143 # just create them independently for now rather than associating them
146 $h = $dbh->selectall_arrayref(q{
147 SELECT o.name, colid,colid2,c.text
152 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
160 $stuff->{triggers}->{$_->[0]}->{text} .= $_->[3];
167 # Not sure what to do with these?
168 $stuff->{type_info_all} = $dbh->type_info_all;
171 # According to the DBI docs, these can be
176 # "GLOBAL TEMPORARY",
181 foreach my $table_info (@tables) {
183 unless (defined($table_info->{TABLE_TYPE}));
185 if ($table_info->{TABLE_TYPE} =~ /TABLE/) {
186 my $table = $schema->add_table(
188 $table_info->{TABLE_NAME},
190 $table_info->{TABLE_TYPE},
191 ) || die $schema->error;
193 # find the associated columns
196 $columns->{$table_info->{TABLE_QUALIFIER}}
197 ->{$table_info->{TABLE_OWNER}}
198 ->{$table_info->{TABLE_NAME}}
201 foreach my $c (values %{$cols}) {
202 my $f = $table->add_field(
203 name => $c->{COLUMN_NAME},
204 data_type => $c->{TYPE_NAME},
205 order => $c->{ORDINAL_POSITION},
206 size => $c->{COLUMN_SIZE},
207 ) || die $table->error;
210 if ($c->{NULLABLE} == 1);
214 my $h = $dbh->selectall_hashref("sp_pkeys
215 [$table_info->{TABLE_NAME}]", 'COLUMN_NAME');
216 if (scalar keys %{$h} > 1) {
220 $a->{KEY_SEQ} <=> $b->{KEY_SEQ}
223 $table->primary_key(@c)
227 # add in any indexes ... how do we tell if the index has
228 # already been created as part of a primary key or other
231 if (defined($stuff->{indexes}->{$table_info->{TABLE_NAME}})){
232 my $h = $dbh->selectall_hashref("sp_helpindex
233 [$table_info->{TABLE_NAME}]", 'INDEX_NAME');
234 foreach (values %{$h}) {
235 my $fields = $_->{'INDEX_KEYS'};
237 my $i = $table->add_index(
242 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
245 # we could make this a primary key if there
246 # isn't already one defined and if there
247 # aren't any nullable columns in thisindex.
249 if (!defined($table->primary_key())) {
250 $table->primary_key($fields)
252 $table->get_field($_)->is_nullable()
253 } split(/,\s*/, $fields);
258 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
259 my $view = $schema->add_view(
261 $table_info->{TABLE_NAME},
266 $columns->{$table_info->{TABLE_QUALIFIER}}
267 ->{$table_info->{TABLE_OWNER}}
268 ->{$table_info->{TABLE_NAME}}
274 $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION}
278 $view->sql($stuff->{view}->{$table_info->{TABLE_NAME}}->{text})
279 if (defined($stuff->{view}->{$table_info->{TABLE_NAME}}->{text}));
283 foreach my $p (values %{$stuff->{procedures}}) {
284 my $proc = $schema->add_procedure(
286 owner => $p->{PROCEDURE_OWNER},
287 comments => $p->{REMARKS},
307 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
311 DBI, DBD::Sybase, SQL::Translator::Schema.