Commit | Line | Data |
16dc9970 |
1 | package SQL::Translator::Parser::Sybase; |
2 | |
d529894e |
3 | # ------------------------------------------------------------------- |
821a0fde |
4 | # $Id$ |
d529894e |
5 | # ------------------------------------------------------------------- |
478f608d |
6 | # Copyright (C) 2002-2009 SQLFairy Authors |
16dc9970 |
7 | # |
d529894e |
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. |
11 | # |
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. |
16 | # |
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 |
20 | # 02111-1307 USA |
21 | # ------------------------------------------------------------------- |
22 | |
23 | =head1 NAME |
24 | |
25 | SQL::Translator::Parser::Sybase - parser for Sybase |
26 | |
27 | =head1 SYNOPSIS |
28 | |
29 | use SQL::Translator::Parser::Sybase; |
30 | |
31 | =head1 DESCRIPTION |
32 | |
0839c8a4 |
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. |
d529894e |
37 | |
38 | =cut |
16dc9970 |
39 | |
d9656bd5 |
40 | use strict; |
16dc9970 |
41 | |
478f608d |
42 | use vars qw[ $DEBUG $GRAMMAR @EXPORT_OK ]; |
d9656bd5 |
43 | $DEBUG = 0 unless defined $DEBUG; |
16dc9970 |
44 | |
d9656bd5 |
45 | use Data::Dumper; |
46 | use Parse::RecDescent; |
47 | use Exporter; |
48 | use base qw(Exporter); |
16dc9970 |
49 | |
d9656bd5 |
50 | @EXPORT_OK = qw(parse); |
16dc9970 |
51 | |
0839c8a4 |
52 | $::RD_ERRORS = 1; |
53 | $::RD_WARN = 1; |
54 | $::RD_HINT = 1; |
16dc9970 |
55 | |
d9656bd5 |
56 | $GRAMMAR = q{ |
16dc9970 |
57 | |
d9656bd5 |
58 | { |
56746e2a |
59 | my ( %tables, @table_comments, $table_order ); |
d9656bd5 |
60 | } |
16dc9970 |
61 | |
d9656bd5 |
62 | startrule : statement(s) eofile { \%tables } |
16dc9970 |
63 | |
d9656bd5 |
64 | eofile : /^\Z/ |
16dc9970 |
65 | |
d9656bd5 |
66 | statement : create_table |
67 | | create_procedure |
68 | | create_index |
69 | | create_constraint |
70 | | comment |
71 | | use |
72 | | setuser |
73 | | if |
74 | | print |
75 | | grant |
76 | | exec |
77 | | <error> |
16dc9970 |
78 | |
d9656bd5 |
79 | use : /use/i WORD GO |
80 | { @table_comments = () } |
16dc9970 |
81 | |
d9656bd5 |
82 | setuser : /setuser/i NAME GO |
83 | |
84 | if : /if/i object_not_null begin if_command end GO |
85 | |
86 | if_command : grant |
87 | | create_index |
88 | | create_constraint |
89 | |
90 | object_not_null : /object_id/i '(' ident ')' /is not null/i |
91 | |
92 | print : /\s*/ /print/i /.*/ |
93 | |
94 | else : /else/i /.*/ |
95 | |
96 | begin : /begin/i |
97 | |
98 | end : /end/i |
99 | |
100 | grant : /grant/i /[^\n]*/ |
101 | |
102 | exec : exec_statement(s) GO |
103 | |
104 | exec_statement : /exec/i /[^\n]+/ |
105 | |
106 | comment : comment_start comment_middle comment_end |
107 | { |
108 | my $comment = $item[2]; |
109 | $comment =~ s/^\s*|\s*$//mg; |
110 | $comment =~ s/^\**\s*//mg; |
111 | push @table_comments, $comment; |
112 | } |
16dc9970 |
113 | |
d9656bd5 |
114 | comment_start : /^\s*\/\*/ |
115 | |
116 | comment_end : /\s*\*\// |
117 | |
118 | comment_middle : m{([^*]+|\*(?!/))*} |
119 | |
120 | # |
121 | # Create table. |
122 | # |
123 | create_table : /create/i /table/i ident '(' create_def(s /,/) ')' lock(?) on_system(?) GO |
124 | { |
125 | my $table_owner = $item[3]{'owner'}; |
126 | my $table_name = $item[3]{'name'}; |
127 | |
128 | if ( @table_comments ) { |
129 | $tables{ $table_name }{'comments'} = [ @table_comments ]; |
130 | @table_comments = (); |
16dc9970 |
131 | } |
132 | |
56746e2a |
133 | $tables{ $table_name }{'order'} = ++$table_order; |
d9656bd5 |
134 | $tables{ $table_name }{'name'} = $table_name; |
135 | $tables{ $table_name }{'owner'} = $table_owner; |
136 | $tables{ $table_name }{'system'} = $item[7]; |
137 | |
138 | my $i = 0; |
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 }; |
144 | $i++; |
145 | |
146 | if ( $def->{'is_primary_key'} ) { |
147 | push @{ $tables{ $table_name }{'constraints'} }, { |
148 | type => 'primary_key', |
149 | fields => [ $field_name ], |
150 | }; |
151 | } |
152 | } |
153 | elsif ( $def->{'supertype'} eq 'constraint' ) { |
154 | push @{ $tables{ $table_name }{'constraints'} }, $def; |
155 | } |
156 | else { |
157 | push @{ $tables{ $table_name }{'indices'} }, $def; |
158 | } |
16dc9970 |
159 | } |
d9656bd5 |
160 | } |
161 | |
162 | create_constraint : /create/i constraint |
163 | { |
164 | @table_comments = (); |
165 | push @{ $tables{ $item[2]{'table'} }{'constraints'} }, $item[2]; |
166 | } |
167 | |
168 | create_index : /create/i index |
169 | { |
170 | @table_comments = (); |
171 | push @{ $tables{ $item[2]{'table'} }{'indices'} }, $item[2]; |
172 | } |
173 | |
174 | create_procedure : /create/i /procedure/i procedure_body GO |
175 | { |
176 | @table_comments = (); |
177 | } |
178 | |
179 | procedure_body : not_go(s) |
180 | |
181 | not_go : /((?!go).)*/ |
182 | |
183 | create_def : field |
184 | | index |
185 | | constraint |
186 | |
187 | blank : /\s*/ |
188 | |
189 | field : field_name data_type nullable(?) |
190 | { |
191 | $return = { |
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], |
200 | } |
201 | } |
202 | |
203 | constraint : primary_key_constraint |
204 | | unique_constraint |
205 | |
206 | field_name : WORD |
207 | |
208 | index_name : WORD |
209 | |
210 | table_name : WORD |
211 | |
212 | data_type : WORD field_size(?) |
213 | { |
214 | $return = { |
215 | type => $item[1], |
216 | size => $item[2][0] |
217 | } |
218 | } |
219 | |
220 | lock : /lock/i /datarows/i |
221 | |
222 | field_type : WORD |
223 | |
224 | field_size : '(' num_range ')' { $item{'num_range'} } |
225 | |
226 | num_range : DIGITS ',' DIGITS |
227 | { $return = $item[1].','.$item[3] } |
228 | | DIGITS |
229 | { $return = $item[1] } |
230 | |
16dc9970 |
231 | |
d9656bd5 |
232 | nullable : /not/i /null/i |
233 | { $return = 0 } |
234 | | /null/i |
235 | { $return = 1 } |
16dc9970 |
236 | |
d9656bd5 |
237 | default_val : /default/i /(?:')?[\w\d.-]*(?:')?/ |
238 | { $item[2]=~s/'//g; $return=$item[2] } |
16dc9970 |
239 | |
d9656bd5 |
240 | auto_inc : /auto_increment/i { 1 } |
16dc9970 |
241 | |
d9656bd5 |
242 | primary_key_constraint : /primary/i /key/i index_name(?) parens_field_list |
243 | { |
244 | $return = { |
245 | supertype => 'constraint', |
246 | name => $item{'index_name'}[0], |
247 | type => 'primary_key', |
248 | fields => $item[4], |
249 | } |
250 | } |
16dc9970 |
251 | |
d9656bd5 |
252 | unique_constraint : /unique/i clustered(?) INDEX(?) index_name(?) on_table(?) parens_field_list |
253 | { |
254 | $return = { |
255 | supertype => 'constraint', |
256 | type => 'unique', |
257 | clustered => $item[2][0], |
258 | name => $item[4][0], |
259 | table => $item[5][0], |
260 | fields => $item[6], |
261 | } |
262 | } |
263 | |
264 | clustered : /clustered/i |
265 | { $return = 1 } |
266 | | /nonclustered/i |
267 | { $return = 0 } |
268 | |
269 | INDEX : /index/i |
270 | |
271 | on_table : /on/i table_name |
272 | { $return = $item[2] } |
273 | |
274 | on_system : /on/i /system/i |
275 | { $return = 1 } |
276 | |
277 | index : clustered(?) INDEX index_name(?) on_table(?) parens_field_list |
278 | { |
279 | $return = { |
280 | supertype => 'index', |
281 | type => 'normal', |
282 | clustered => $item[1][0], |
283 | name => $item[3][0], |
284 | table => $item[4][0], |
285 | fields => $item[5], |
286 | } |
287 | } |
288 | |
289 | parens_field_list : '(' field_name(s /,/) ')' |
290 | { $item[2] } |
291 | |
292 | ident : QUOTE(?) WORD '.' WORD QUOTE(?) |
293 | { $return = { owner => $item[2], name => $item[4] } } |
294 | | WORD |
295 | { $return = { name => $item[2] } } |
296 | |
297 | GO : /^go/i |
298 | |
299 | NAME : QUOTE(?) /\w+/ QUOTE(?) |
300 | { $item[2] } |
301 | |
302 | WORD : /[\w#]+/ |
303 | |
304 | DIGITS : /\d+/ |
305 | |
306 | COMMA : ',' |
307 | |
308 | QUOTE : /'/ |
16dc9970 |
309 | |
310 | }; |
311 | |
d9656bd5 |
312 | # ------------------------------------------------------------------- |
313 | sub parse { |
314 | my ( $translator, $data ) = @_; |
315 | my $parser = Parse::RecDescent->new($GRAMMAR); |
316 | |
317 | local $::RD_TRACE = $translator->trace ? 1 : undef; |
318 | local $DEBUG = $translator->debug; |
319 | |
320 | unless (defined $parser) { |
321 | return $translator->error("Error instantiating Parse::RecDescent ". |
322 | "instance: Bad grammer"); |
323 | } |
324 | |
325 | my $result = $parser->startrule($data); |
326 | return $translator->error( "Parse failed." ) unless defined $result; |
327 | warn Dumper( $result ) if $DEBUG; |
328 | |
329 | my $schema = $translator->schema; |
330 | my @tables = sort { |
331 | $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'} |
332 | } keys %{ $result }; |
333 | |
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; |
338 | |
339 | $table->comments( $tdata->{'comments'} ); |
340 | |
341 | my @fields = sort { |
342 | $tdata->{'fields'}->{$a}->{'order'} |
343 | <=> |
344 | $tdata->{'fields'}->{$b}->{'order'} |
345 | } keys %{ $tdata->{'fields'} }; |
346 | |
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; |
358 | |
359 | $table->primary_key( $field->name ) if $fdata->{'is_primary_key'}; |
360 | |
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 ); |
365 | } |
366 | } |
367 | |
368 | if ( $field->data_type =~ /(set|enum)/i && !$field->size ) { |
369 | my %extra = $field->extra; |
370 | my $longest = 0; |
371 | for my $len ( map { length } @{ $extra{'list'} || [] } ) { |
372 | $longest = $len if $len > $longest; |
373 | } |
374 | $field->size( $longest ) if $longest; |
375 | } |
376 | |
377 | for my $cdata ( @{ $fdata->{'constraints'} } ) { |
378 | next unless $cdata->{'type'} eq 'foreign_key'; |
379 | $cdata->{'fields'} ||= [ $field->name ]; |
380 | push @{ $tdata->{'constraints'} }, $cdata; |
381 | } |
382 | } |
383 | |
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; |
390 | } |
391 | |
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'} || '', |
100684f3 |
400 | on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'}, |
401 | on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'}, |
d9656bd5 |
402 | ) or die $table->error; |
403 | } |
404 | } |
405 | |
406 | return 1; |
407 | } |
408 | |
16dc9970 |
409 | 1; |
410 | |
d9656bd5 |
411 | # ------------------------------------------------------------------- |
16dc9970 |
412 | # Every hero becomes a bore at last. |
413 | # Ralph Waldo Emerson |
d9656bd5 |
414 | # ------------------------------------------------------------------- |
16dc9970 |
415 | |
d529894e |
416 | =pod |
16dc9970 |
417 | |
418 | =head1 AUTHOR |
419 | |
d9656bd5 |
420 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
16dc9970 |
421 | |
422 | =head1 SEE ALSO |
423 | |
0839c8a4 |
424 | SQL::Translator, SQL::Translator::Parser::DBI, L<http://www.midsomer.org/>. |
16dc9970 |
425 | |
426 | =cut |