1 package SQL::Translator::Parser::Sybase;
3 # -------------------------------------------------------------------
4 # $Id: Sybase.pm,v 1.10 2005-06-28 16:39:41 mwz444 Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::Sybase - parser for Sybase
29 use SQL::Translator::Parser::Sybase;
33 Mostly parses the output of "dbschema.pl," a Perl script freely
34 available from http://www.midsomer.org. The parsing is not complete,
35 however, and you would probably have much better luck using the
36 DBI-Sybase parser included with SQL::Translator.
42 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
43 $VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
44 $DEBUG = 0 unless defined $DEBUG;
47 use Parse::RecDescent;
49 use base qw(Exporter);
51 @EXPORT_OK = qw(parse);
60 my ( %tables, @table_comments, $table_order );
63 startrule : statement(s) eofile { \%tables }
67 statement : create_table
81 { @table_comments = () }
83 setuser : /setuser/i NAME GO
85 if : /if/i object_not_null begin if_command end GO
91 object_not_null : /object_id/i '(' ident ')' /is not null/i
93 print : /\s*/ /print/i /.*/
101 grant : /grant/i /[^\n]*/
103 exec : exec_statement(s) GO
105 exec_statement : /exec/i /[^\n]+/
107 comment : comment_start comment_middle comment_end
109 my $comment = $item[2];
110 $comment =~ s/^\s*|\s*$//mg;
111 $comment =~ s/^\**\s*//mg;
112 push @table_comments, $comment;
115 comment_start : /^\s*\/\*/
117 comment_end : /\s*\*\//
119 comment_middle : m{([^*]+|\*(?!/))*}
124 create_table : /create/i /table/i ident '(' create_def(s /,/) ')' lock(?) on_system(?) GO
126 my $table_owner = $item[3]{'owner'};
127 my $table_name = $item[3]{'name'};
129 if ( @table_comments ) {
130 $tables{ $table_name }{'comments'} = [ @table_comments ];
131 @table_comments = ();
134 $tables{ $table_name }{'order'} = ++$table_order;
135 $tables{ $table_name }{'name'} = $table_name;
136 $tables{ $table_name }{'owner'} = $table_owner;
137 $tables{ $table_name }{'system'} = $item[7];
140 for my $def ( @{ $item[5] } ) {
141 if ( $def->{'supertype'} eq 'field' ) {
142 my $field_name = $def->{'name'};
143 $tables{ $table_name }{'fields'}{ $field_name } =
144 { %$def, order => $i };
147 if ( $def->{'is_primary_key'} ) {
148 push @{ $tables{ $table_name }{'constraints'} }, {
149 type => 'primary_key',
150 fields => [ $field_name ],
154 elsif ( $def->{'supertype'} eq 'constraint' ) {
155 push @{ $tables{ $table_name }{'constraints'} }, $def;
158 push @{ $tables{ $table_name }{'indices'} }, $def;
163 create_constraint : /create/i constraint
165 @table_comments = ();
166 push @{ $tables{ $item[2]{'table'} }{'constraints'} }, $item[2];
169 create_index : /create/i index
171 @table_comments = ();
172 push @{ $tables{ $item[2]{'table'} }{'indices'} }, $item[2];
175 create_procedure : /create/i /procedure/i procedure_body GO
177 @table_comments = ();
180 procedure_body : not_go(s)
182 not_go : /((?!go).)*/
190 field : field_name data_type nullable(?)
193 supertype => 'field',
194 name => $item{'field_name'},
195 data_type => $item{'data_type'}{'type'},
196 size => $item{'data_type'}{'size'},
197 nullable => $item[3][0],
198 # default => $item{'default_val'}[0],
199 # is_auto_inc => $item{'auto_inc'}[0],
200 # is_primary_key => $item{'primary_key'}[0],
204 constraint : primary_key_constraint
213 data_type : WORD field_size(?)
221 lock : /lock/i /datarows/i
225 field_size : '(' num_range ')' { $item{'num_range'} }
227 num_range : DIGITS ',' DIGITS
228 { $return = $item[1].','.$item[3] }
230 { $return = $item[1] }
233 nullable : /not/i /null/i
238 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
239 { $item[2]=~s/'//g; $return=$item[2] }
241 auto_inc : /auto_increment/i { 1 }
243 primary_key_constraint : /primary/i /key/i index_name(?) parens_field_list
246 supertype => 'constraint',
247 name => $item{'index_name'}[0],
248 type => 'primary_key',
253 unique_constraint : /unique/i clustered(?) INDEX(?) index_name(?) on_table(?) parens_field_list
256 supertype => 'constraint',
258 clustered => $item[2][0],
260 table => $item[5][0],
265 clustered : /clustered/i
272 on_table : /on/i table_name
273 { $return = $item[2] }
275 on_system : /on/i /system/i
278 index : clustered(?) INDEX index_name(?) on_table(?) parens_field_list
281 supertype => 'index',
283 clustered => $item[1][0],
285 table => $item[4][0],
290 parens_field_list : '(' field_name(s /,/) ')'
293 ident : QUOTE(?) WORD '.' WORD QUOTE(?)
294 { $return = { owner => $item[2], name => $item[4] } }
296 { $return = { name => $item[2] } }
300 NAME : QUOTE(?) /\w+/ QUOTE(?)
313 # -------------------------------------------------------------------
315 my ( $translator, $data ) = @_;
316 my $parser = Parse::RecDescent->new($GRAMMAR);
318 local $::RD_TRACE = $translator->trace ? 1 : undef;
319 local $DEBUG = $translator->debug;
321 unless (defined $parser) {
322 return $translator->error("Error instantiating Parse::RecDescent ".
323 "instance: Bad grammer");
326 my $result = $parser->startrule($data);
327 return $translator->error( "Parse failed." ) unless defined $result;
328 warn Dumper( $result ) if $DEBUG;
330 my $schema = $translator->schema;
332 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
335 for my $table_name ( @tables ) {
336 my $tdata = $result->{ $table_name };
337 my $table = $schema->add_table( name => $tdata->{'name'} )
338 or die "Can't create table '$table_name': ", $schema->error;
340 $table->comments( $tdata->{'comments'} );
343 $tdata->{'fields'}->{$a}->{'order'}
345 $tdata->{'fields'}->{$b}->{'order'}
346 } keys %{ $tdata->{'fields'} };
348 for my $fname ( @fields ) {
349 my $fdata = $tdata->{'fields'}{ $fname };
350 my $field = $table->add_field(
351 name => $fdata->{'name'},
352 data_type => $fdata->{'data_type'},
353 size => $fdata->{'size'},
354 default_value => $fdata->{'default'},
355 is_auto_increment => $fdata->{'is_auto_inc'},
356 is_nullable => $fdata->{'nullable'},
357 comments => $fdata->{'comments'},
358 ) or die $table->error;
360 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
362 for my $qual ( qw[ binary unsigned zerofill list ] ) {
363 if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
364 next if ref $val eq 'ARRAY' && !@$val;
365 $field->extra( $qual, $val );
369 if ( $field->data_type =~ /(set|enum)/i && !$field->size ) {
370 my %extra = $field->extra;
372 for my $len ( map { length } @{ $extra{'list'} || [] } ) {
373 $longest = $len if $len > $longest;
375 $field->size( $longest ) if $longest;
378 for my $cdata ( @{ $fdata->{'constraints'} } ) {
379 next unless $cdata->{'type'} eq 'foreign_key';
380 $cdata->{'fields'} ||= [ $field->name ];
381 push @{ $tdata->{'constraints'} }, $cdata;
385 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
386 my $index = $table->add_index(
387 name => $idata->{'name'},
388 type => uc $idata->{'type'},
389 fields => $idata->{'fields'},
390 ) or die $table->error;
393 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
394 my $constraint = $table->add_constraint(
395 name => $cdata->{'name'},
396 type => $cdata->{'type'},
397 fields => $cdata->{'fields'},
398 reference_table => $cdata->{'reference_table'},
399 reference_fields => $cdata->{'reference_fields'},
400 match_type => $cdata->{'match_type'} || '',
401 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
402 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
403 ) or die $table->error;
412 # -------------------------------------------------------------------
413 # Every hero becomes a bore at last.
414 # Ralph Waldo Emerson
415 # -------------------------------------------------------------------
421 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
425 SQL::Translator, SQL::Translator::Parser::DBI, L<http://www.midsomer.org/>.