1 package SQL::Translator::Parser::Sybase;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::Sybase - parser for Sybase
27 use SQL::Translator::Parser::Sybase;
31 Mostly parses the output of "dbschema.pl," a Perl script freely
32 available from http://www.midsomer.org. The parsing is not complete,
33 however, and you would probably have much better luck using the
34 DBI-Sybase parser included with SQL::Translator.
40 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
42 $DEBUG = 0 unless defined $DEBUG;
45 use Parse::RecDescent;
47 use base qw(Exporter);
49 @EXPORT_OK = qw(parse);
58 my ( %tables, @table_comments, $table_order );
61 startrule : statement(s) eofile { \%tables }
65 statement : create_table
79 { @table_comments = () }
81 setuser : /setuser/i NAME GO
83 if : /if/i object_not_null begin if_command end GO
89 object_not_null : /object_id/i '(' ident ')' /is not null/i
91 print : /\s*/ /print/i /.*/
99 grant : /grant/i /[^\n]*/
101 exec : exec_statement(s) GO
103 exec_statement : /exec/i /[^\n]+/
105 comment : comment_start comment_middle comment_end
107 my $comment = $item[2];
108 $comment =~ s/^\s*|\s*$//mg;
109 $comment =~ s/^\**\s*//mg;
110 push @table_comments, $comment;
113 comment_start : /^\s*\/\*/
115 comment_end : /\s*\*\//
117 comment_middle : m{([^*]+|\*(?!/))*}
122 create_table : /create/i /table/i ident '(' create_def(s /,/) ')' lock(?) on_system(?) GO
124 my $table_owner = $item[3]{'owner'};
125 my $table_name = $item[3]{'name'};
127 if ( @table_comments ) {
128 $tables{ $table_name }{'comments'} = [ @table_comments ];
129 @table_comments = ();
132 $tables{ $table_name }{'order'} = ++$table_order;
133 $tables{ $table_name }{'name'} = $table_name;
134 $tables{ $table_name }{'owner'} = $table_owner;
135 $tables{ $table_name }{'system'} = $item[7];
138 for my $def ( @{ $item[5] } ) {
139 if ( $def->{'supertype'} eq 'field' ) {
140 my $field_name = $def->{'name'};
141 $tables{ $table_name }{'fields'}{ $field_name } =
142 { %$def, order => $i };
145 if ( $def->{'is_primary_key'} ) {
146 push @{ $tables{ $table_name }{'constraints'} }, {
147 type => 'primary_key',
148 fields => [ $field_name ],
152 elsif ( $def->{'supertype'} eq 'constraint' ) {
153 push @{ $tables{ $table_name }{'constraints'} }, $def;
156 push @{ $tables{ $table_name }{'indices'} }, $def;
161 create_constraint : /create/i constraint
163 @table_comments = ();
164 push @{ $tables{ $item[2]{'table'} }{'constraints'} }, $item[2];
167 create_index : /create/i index
169 @table_comments = ();
170 push @{ $tables{ $item[2]{'table'} }{'indices'} }, $item[2];
173 create_procedure : /create/i /procedure/i procedure_body GO
175 @table_comments = ();
178 procedure_body : not_go(s)
180 not_go : /((?!go).)*/
188 field : field_name data_type nullable(?)
191 supertype => 'field',
192 name => $item{'field_name'},
193 data_type => $item{'data_type'}{'type'},
194 size => $item{'data_type'}{'size'},
195 nullable => $item[3][0],
196 # default => $item{'default_val'}[0],
197 # is_auto_inc => $item{'auto_inc'}[0],
198 # is_primary_key => $item{'primary_key'}[0],
202 constraint : primary_key_constraint
211 data_type : WORD field_size(?)
219 lock : /lock/i /datarows/i
223 field_size : '(' num_range ')' { $item{'num_range'} }
225 num_range : DIGITS ',' DIGITS
226 { $return = $item[1].','.$item[3] }
228 { $return = $item[1] }
231 nullable : /not/i /null/i
236 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
237 { $item[2]=~s/'//g; $return=$item[2] }
239 auto_inc : /auto_increment/i { 1 }
241 primary_key_constraint : /primary/i /key/i index_name(?) parens_field_list
244 supertype => 'constraint',
245 name => $item{'index_name'}[0],
246 type => 'primary_key',
251 unique_constraint : /unique/i clustered(?) INDEX(?) index_name(?) on_table(?) parens_field_list
254 supertype => 'constraint',
256 clustered => $item[2][0],
258 table => $item[5][0],
263 clustered : /clustered/i
270 on_table : /on/i table_name
271 { $return = $item[2] }
273 on_system : /on/i /system/i
276 index : clustered(?) INDEX index_name(?) on_table(?) parens_field_list
279 supertype => 'index',
281 clustered => $item[1][0],
283 table => $item[4][0],
288 parens_field_list : '(' field_name(s /,/) ')'
291 ident : QUOTE(?) WORD '.' WORD QUOTE(?)
292 { $return = { owner => $item[2], name => $item[4] } }
294 { $return = { name => $item[2] } }
298 NAME : QUOTE(?) /\w+/ QUOTE(?)
312 my ( $translator, $data ) = @_;
313 my $parser = Parse::RecDescent->new($GRAMMAR);
315 local $::RD_TRACE = $translator->trace ? 1 : undef;
316 local $DEBUG = $translator->debug;
318 unless (defined $parser) {
319 return $translator->error("Error instantiating Parse::RecDescent ".
320 "instance: Bad grammer");
323 my $result = $parser->startrule($data);
324 return $translator->error( "Parse failed." ) unless defined $result;
325 warn Dumper( $result ) if $DEBUG;
327 my $schema = $translator->schema;
329 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
332 for my $table_name ( @tables ) {
333 my $tdata = $result->{ $table_name };
334 my $table = $schema->add_table( name => $tdata->{'name'} )
335 or die "Can't create table '$table_name': ", $schema->error;
337 $table->comments( $tdata->{'comments'} );
340 $tdata->{'fields'}->{$a}->{'order'}
342 $tdata->{'fields'}->{$b}->{'order'}
343 } keys %{ $tdata->{'fields'} };
345 for my $fname ( @fields ) {
346 my $fdata = $tdata->{'fields'}{ $fname };
347 my $field = $table->add_field(
348 name => $fdata->{'name'},
349 data_type => $fdata->{'data_type'},
350 size => $fdata->{'size'},
351 default_value => $fdata->{'default'},
352 is_auto_increment => $fdata->{'is_auto_inc'},
353 is_nullable => $fdata->{'nullable'},
354 comments => $fdata->{'comments'},
355 ) or die $table->error;
357 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
359 for my $qual ( qw[ binary unsigned zerofill list ] ) {
360 if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
361 next if ref $val eq 'ARRAY' && !@$val;
362 $field->extra( $qual, $val );
366 if ( $field->data_type =~ /(set|enum)/i && !$field->size ) {
367 my %extra = $field->extra;
369 for my $len ( map { length } @{ $extra{'list'} || [] } ) {
370 $longest = $len if $len > $longest;
372 $field->size( $longest ) if $longest;
375 for my $cdata ( @{ $fdata->{'constraints'} } ) {
376 next unless $cdata->{'type'} eq 'foreign_key';
377 $cdata->{'fields'} ||= [ $field->name ];
378 push @{ $tdata->{'constraints'} }, $cdata;
382 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
383 my $index = $table->add_index(
384 name => $idata->{'name'},
385 type => uc $idata->{'type'},
386 fields => $idata->{'fields'},
387 ) or die $table->error;
390 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
391 my $constraint = $table->add_constraint(
392 name => $cdata->{'name'},
393 type => $cdata->{'type'},
394 fields => $cdata->{'fields'},
395 reference_table => $cdata->{'reference_table'},
396 reference_fields => $cdata->{'reference_fields'},
397 match_type => $cdata->{'match_type'} || '',
398 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
399 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
400 ) or die $table->error;
409 # -------------------------------------------------------------------
410 # Every hero becomes a bore at last.
411 # Ralph Waldo Emerson
412 # -------------------------------------------------------------------
418 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
422 SQL::Translator, SQL::Translator::Parser::DBI, L<http://www.midsomer.org/>.