1 package SQL::Translator::Parser::Sybase;
3 # -------------------------------------------------------------------
4 # $Id: Sybase.pm 1440 2009-01-17 16:31:57Z jawnsy $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 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 $GRAMMAR @EXPORT_OK ];
43 $DEBUG = 0 unless defined $DEBUG;
46 use Parse::RecDescent;
48 use base qw(Exporter);
50 @EXPORT_OK = qw(parse);
59 my ( %tables, @table_comments, $table_order );
62 startrule : statement(s) eofile { \%tables }
66 statement : create_table
80 { @table_comments = () }
82 setuser : /setuser/i NAME GO
84 if : /if/i object_not_null begin if_command end GO
90 object_not_null : /object_id/i '(' ident ')' /is not null/i
92 print : /\s*/ /print/i /.*/
100 grant : /grant/i /[^\n]*/
102 exec : exec_statement(s) GO
104 exec_statement : /exec/i /[^\n]+/
106 comment : comment_start comment_middle comment_end
108 my $comment = $item[2];
109 $comment =~ s/^\s*|\s*$//mg;
110 $comment =~ s/^\**\s*//mg;
111 push @table_comments, $comment;
114 comment_start : /^\s*\/\*/
116 comment_end : /\s*\*\//
118 comment_middle : m{([^*]+|\*(?!/))*}
123 create_table : /create/i /table/i ident '(' create_def(s /,/) ')' lock(?) on_system(?) GO
125 my $table_owner = $item[3]{'owner'};
126 my $table_name = $item[3]{'name'};
128 if ( @table_comments ) {
129 $tables{ $table_name }{'comments'} = [ @table_comments ];
130 @table_comments = ();
133 $tables{ $table_name }{'order'} = ++$table_order;
134 $tables{ $table_name }{'name'} = $table_name;
135 $tables{ $table_name }{'owner'} = $table_owner;
136 $tables{ $table_name }{'system'} = $item[7];
139 for my $def ( @{ $item[5] } ) {
140 if ( $def->{'supertype'} eq 'field' ) {
141 my $field_name = $def->{'name'};
142 $tables{ $table_name }{'fields'}{ $field_name } =
143 { %$def, order => $i };
146 if ( $def->{'is_primary_key'} ) {
147 push @{ $tables{ $table_name }{'constraints'} }, {
148 type => 'primary_key',
149 fields => [ $field_name ],
153 elsif ( $def->{'supertype'} eq 'constraint' ) {
154 push @{ $tables{ $table_name }{'constraints'} }, $def;
157 push @{ $tables{ $table_name }{'indices'} }, $def;
162 create_constraint : /create/i constraint
164 @table_comments = ();
165 push @{ $tables{ $item[2]{'table'} }{'constraints'} }, $item[2];
168 create_index : /create/i index
170 @table_comments = ();
171 push @{ $tables{ $item[2]{'table'} }{'indices'} }, $item[2];
174 create_procedure : /create/i /procedure/i procedure_body GO
176 @table_comments = ();
179 procedure_body : not_go(s)
181 not_go : /((?!go).)*/
189 field : field_name data_type nullable(?)
192 supertype => 'field',
193 name => $item{'field_name'},
194 data_type => $item{'data_type'}{'type'},
195 size => $item{'data_type'}{'size'},
196 nullable => $item[3][0],
197 # default => $item{'default_val'}[0],
198 # is_auto_inc => $item{'auto_inc'}[0],
199 # is_primary_key => $item{'primary_key'}[0],
203 constraint : primary_key_constraint
212 data_type : WORD field_size(?)
220 lock : /lock/i /datarows/i
224 field_size : '(' num_range ')' { $item{'num_range'} }
226 num_range : DIGITS ',' DIGITS
227 { $return = $item[1].','.$item[3] }
229 { $return = $item[1] }
232 nullable : /not/i /null/i
237 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
238 { $item[2]=~s/'//g; $return=$item[2] }
240 auto_inc : /auto_increment/i { 1 }
242 primary_key_constraint : /primary/i /key/i index_name(?) parens_field_list
245 supertype => 'constraint',
246 name => $item{'index_name'}[0],
247 type => 'primary_key',
252 unique_constraint : /unique/i clustered(?) INDEX(?) index_name(?) on_table(?) parens_field_list
255 supertype => 'constraint',
257 clustered => $item[2][0],
259 table => $item[5][0],
264 clustered : /clustered/i
271 on_table : /on/i table_name
272 { $return = $item[2] }
274 on_system : /on/i /system/i
277 index : clustered(?) INDEX index_name(?) on_table(?) parens_field_list
280 supertype => 'index',
282 clustered => $item[1][0],
284 table => $item[4][0],
289 parens_field_list : '(' field_name(s /,/) ')'
292 ident : QUOTE(?) WORD '.' WORD QUOTE(?)
293 { $return = { owner => $item[2], name => $item[4] } }
295 { $return = { name => $item[2] } }
299 NAME : QUOTE(?) /\w+/ QUOTE(?)
312 # -------------------------------------------------------------------
314 my ( $translator, $data ) = @_;
315 my $parser = Parse::RecDescent->new($GRAMMAR);
317 local $::RD_TRACE = $translator->trace ? 1 : undef;
318 local $DEBUG = $translator->debug;
320 unless (defined $parser) {
321 return $translator->error("Error instantiating Parse::RecDescent ".
322 "instance: Bad grammer");
325 my $result = $parser->startrule($data);
326 return $translator->error( "Parse failed." ) unless defined $result;
327 warn Dumper( $result ) if $DEBUG;
329 my $schema = $translator->schema;
331 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
334 for my $table_name ( @tables ) {
335 my $tdata = $result->{ $table_name };
336 my $table = $schema->add_table( name => $tdata->{'name'} )
337 or die "Can't create table '$table_name': ", $schema->error;
339 $table->comments( $tdata->{'comments'} );
342 $tdata->{'fields'}->{$a}->{'order'}
344 $tdata->{'fields'}->{$b}->{'order'}
345 } keys %{ $tdata->{'fields'} };
347 for my $fname ( @fields ) {
348 my $fdata = $tdata->{'fields'}{ $fname };
349 my $field = $table->add_field(
350 name => $fdata->{'name'},
351 data_type => $fdata->{'data_type'},
352 size => $fdata->{'size'},
353 default_value => $fdata->{'default'},
354 is_auto_increment => $fdata->{'is_auto_inc'},
355 is_nullable => $fdata->{'nullable'},
356 comments => $fdata->{'comments'},
357 ) or die $table->error;
359 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
361 for my $qual ( qw[ binary unsigned zerofill list ] ) {
362 if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
363 next if ref $val eq 'ARRAY' && !@$val;
364 $field->extra( $qual, $val );
368 if ( $field->data_type =~ /(set|enum)/i && !$field->size ) {
369 my %extra = $field->extra;
371 for my $len ( map { length } @{ $extra{'list'} || [] } ) {
372 $longest = $len if $len > $longest;
374 $field->size( $longest ) if $longest;
377 for my $cdata ( @{ $fdata->{'constraints'} } ) {
378 next unless $cdata->{'type'} eq 'foreign_key';
379 $cdata->{'fields'} ||= [ $field->name ];
380 push @{ $tdata->{'constraints'} }, $cdata;
384 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
385 my $index = $table->add_index(
386 name => $idata->{'name'},
387 type => uc $idata->{'type'},
388 fields => $idata->{'fields'},
389 ) or die $table->error;
392 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
393 my $constraint = $table->add_constraint(
394 name => $cdata->{'name'},
395 type => $cdata->{'type'},
396 fields => $cdata->{'fields'},
397 reference_table => $cdata->{'reference_table'},
398 reference_fields => $cdata->{'reference_fields'},
399 match_type => $cdata->{'match_type'} || '',
400 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
401 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
402 ) or die $table->error;
411 # -------------------------------------------------------------------
412 # Every hero becomes a bore at last.
413 # Ralph Waldo Emerson
414 # -------------------------------------------------------------------
420 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
424 SQL::Translator, SQL::Translator::Parser::DBI, L<http://www.midsomer.org/>.