Now parsing and adding views and triggers to schema.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / SQLite.pm
1 package SQL::Translator::Parser::SQLite;
2
3 # -------------------------------------------------------------------
4 # $Id: SQLite.pm,v 1.2 2003-10-04 01:16:39 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 #                    darren chamberlain <darren@cpan.org>,
8 #                    Chris Mungall <cjm@fruitfly.org>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # 02111-1307  USA
23 # -------------------------------------------------------------------
24
25 =head1 NAME
26
27 SQL::Translator::Parser::SQLite - parser for SQLite
28
29 =head1 SYNOPSIS
30
31   use SQL::Translator;
32   use SQL::Translator::Parser::SQLite;
33
34   my $translator = SQL::Translator->new;
35   $translator->parser("SQL::Translator::Parser::SQLite");
36
37 =head1 DESCRIPTION
38
39 This is a grammar for parsing CREATE statements for SQLite as 
40 described here:
41
42     http://www.sqlite.org/lang.html
43
44 CREATE INDEX
45
46 sql-statement ::=
47     CREATE [TEMP | TEMPORARY] [UNIQUE] INDEX index-name 
48      ON [database-name .] table-name ( column-name [, column-name]* )
49      [ ON CONFLICT conflict-algorithm ]
50
51 column-name ::=
52     name [ ASC | DESC ]
53
54 CREATE TABLE
55
56 sql-command ::=
57     CREATE [TEMP | TEMPORARY] TABLE table-name (
58         column-def [, column-def]*
59         [, constraint]*
60      )
61
62 sql-command ::=
63     CREATE [TEMP | TEMPORARY] TABLE table-name AS select-statement
64
65 column-def ::=
66     name [type] [[CONSTRAINT name] column-constraint]*
67
68 type ::=
69     typename |
70      typename ( number ) |
71      typename ( number , number )
72
73 column-constraint ::=
74     NOT NULL [ conflict-clause ] |
75     PRIMARY KEY [sort-order] [ conflict-clause ] |
76     UNIQUE [ conflict-clause ] |
77     CHECK ( expr ) [ conflict-clause ] |
78     DEFAULT value
79
80 constraint ::=
81     PRIMARY KEY ( name [, name]* ) [ conflict-clause ]|
82     UNIQUE ( name [, name]* ) [ conflict-clause ] |
83     CHECK ( expr ) [ conflict-clause ]
84
85 conflict-clause ::=
86     ON CONFLICT conflict-algorithm
87
88 CREATE TRIGGER
89
90 sql-statement ::=
91     CREATE [TEMP | TEMPORARY] TRIGGER trigger-name [ BEFORE | AFTER ]
92     database-event ON [database-name .] table-name
93     trigger-action
94
95 sql-statement ::=
96     CREATE [TEMP | TEMPORARY] TRIGGER trigger-name INSTEAD OF
97     database-event ON [database-name .] view-name
98     trigger-action
99
100 database-event ::=
101     DELETE | 
102     INSERT | 
103     UPDATE | 
104     UPDATE OF column-list
105
106 trigger-action ::=
107     [ FOR EACH ROW | FOR EACH STATEMENT ] [ WHEN expression ] 
108         BEGIN 
109             trigger-step ; [ trigger-step ; ]*
110         END
111
112 trigger-step ::=
113     update-statement | insert-statement | 
114     delete-statement | select-statemen
115
116 CREATE VIEW
117
118 sql-command ::=
119     CREATE [TEMP | TEMPORARY] VIEW view-name AS select-statement
120
121 ON CONFLICT clause
122
123     conflict-clause ::=
124     ON CONFLICT conflict-algorithm
125
126     conflict-algorithm ::=
127     ROLLBACK | ABORT | FAIL | IGNORE | REPLACE
128
129 expression
130
131 expr ::=
132     expr binary-op expr |
133     expr like-op expr |
134     unary-op expr |
135     ( expr ) |
136     column-name |
137     table-name . column-name |
138     database-name . table-name . column-name |
139     literal-value |
140     function-name ( expr-list | * ) |
141     expr (+) |
142     expr ISNULL |
143     expr NOTNULL |
144     expr [NOT] BETWEEN expr AND expr |
145     expr [NOT] IN ( value-list ) |
146     expr [NOT] IN ( select-statement ) |
147     ( select-statement ) |
148     CASE [expr] ( WHEN expr THEN expr )+ [ELSE expr] END
149
150 like-op::=
151     LIKE | GLOB | NOT LIKE | NOT GLOB
152
153 =cut
154
155 use strict;
156 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
157 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
158 $DEBUG   = 0 unless defined $DEBUG;
159
160 use Data::Dumper;
161 use Parse::RecDescent;
162 use Exporter;
163 use base qw(Exporter);
164
165 @EXPORT_OK = qw(parse);
166
167 # Enable warnings within the Parse::RecDescent module.
168 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
169 $::RD_WARN   = 1; # Enable warnings. This will warn on unused rules &c.
170 $::RD_HINT   = 1; # Give out hints to help fix problems.
171
172 $GRAMMAR = q!
173
174
175     my ( %tables, $table_order, @table_comments, @views, @triggers );
176 }
177
178 #
179 # The "eofile" rule makes the parser fail if any "statement" rule
180 # fails.  Otherwise, the first successful match by a "statement" 
181 # won't cause the failure needed to know that the parse, as a whole,
182 # failed. -ky
183 #
184 startrule : statement(s) eofile { 
185     $return      => {
186         tables   => \%tables, 
187         views    => \@views,
188         triggers => \@triggers,
189     }
190 }
191
192 eofile : /^\Z/
193
194 statement : begin_transaction
195     | commit
196     | comment
197     | create
198     | <error>
199
200 begin_transaction : /begin transaction/i SEMICOLON
201
202 commit : /commit/i SEMICOLON
203
204 comment : /^\s*(?:#|-{2}).*\n/
205     {
206         my $comment =  $item[1];
207         $comment    =~ s/^\s*(#|-{2})\s*//;
208         $comment    =~ s/\s*$//;
209         $return     = $comment;
210     }
211
212 comment : /\/\*/ /[^\*]+/ /\*\// 
213     {
214         my $comment = $item[2];
215         $comment    =~ s/^\s*|\s*$//g;
216         $return = $comment;
217     }
218
219 #
220 # Create Index
221 #
222 create : CREATE TEMPORARY(?) UNIQUE(?) INDEX WORD ON table_name parens_field_list conflict_clause(?) SEMICOLON
223     {
224         my $db_name    = $item[7]->{'db_name'} || '';
225         my $table_name = $item[7]->{'name'};
226
227         my $index        =  { 
228             name         => $item[5],
229             fields       => $item[8],
230             on_conflict  => $item[9][0],
231             is_temporary => $item[2][0] ? 1 : 0,
232         };
233
234         my $is_unique = $item[3][0];
235
236         if ( $is_unique ) {
237             $index->{'type'} = 'unique';
238             push @{ $tables{ $table_name }{'constraints'} }, $index;
239         }
240         else {
241             push @{ $tables{ $table_name }{'indices'} }, $index;
242         }
243     }
244
245 #
246 # Create Table
247 #
248 create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLON
249     {
250         my $db_name    = $item[4]->{'db_name'} || '';
251         my $table_name = $item[4]->{'name'};
252
253         $tables{ $table_name }{'name'}         = $table_name;
254         $tables{ $table_name }{'is_temporary'} = $item[2][0] ? 1 : 0;
255         $tables{ $table_name }{'order'}        = ++$table_order;
256
257         for my $def ( @{ $item[6] } ) {
258             if ( $def->{'supertype'} eq 'column' ) {
259                 push @{ $tables{ $table_name }{'fields'} }, $def;
260             }
261             elsif ( $def->{'supertype'} eq 'constraint' ) {
262                 push @{ $tables{ $table_name }{'constraints'} }, $def;
263             }
264         }
265     }
266
267 definition : constraint_def | column_def 
268
269 column_def: NAME type(?) column_constraint(s?)
270     {
271         my $column = {
272             supertype      => 'column',
273             name           => $item[1],  
274             data_type      => $item[2][0]->{'type'},
275             size           => $item[2][0]->{'size'},
276             is_nullable    => 1,
277             is_primary_key => 0,
278             is_unique      => 0,
279             check          => '',
280             default        => undef,
281             constraints    => $item[3],
282         };
283
284         for my $c ( @{ $item[3] } ) {
285             if ( $c->{'type'} eq 'not_null' ) {
286                 $column->{'is_nullable'} = 0;
287             }
288             elsif ( $c->{'type'} eq 'primary_key' ) {
289                 $column->{'is_primary_key'} = 1;
290             }
291             elsif ( $c->{'type'} eq 'unique' ) {
292                 $column->{'is_unique'} = 1;
293             }
294             elsif ( $c->{'type'} eq 'check' ) {
295                 $column->{'check'} = $c->{'expression'};
296             }
297             elsif ( $c->{'type'} eq 'default' ) {
298                 $column->{'default'} = $c->{'value'};
299             }
300         }
301
302         $column;
303     }
304
305 type : WORD parens_value_list(?)
306     {
307         $return = {
308             type => $item[1],
309             size => $item[2][0],
310         }
311     }
312
313 column_constraint : NOT_NULL conflict_clause(?)
314     {
315         $return = {
316             type => 'not_null',
317         }
318     }
319     |
320     PRIMARY_KEY sort_order(?) conflict_clause(?)
321     {
322         $return = {
323             type        => 'primary_key',
324             sort_order  => $item[2][0],
325             on_conflict => $item[2][0], 
326         }
327     }
328     |
329     UNIQUE conflict_clause(?)
330     {
331         $return = {
332             type        => 'unique',
333             on_conflict => $item[2][0], 
334         }
335     }
336     |
337     CHECK_C '(' expr ')' conflict_clause(?)
338     {
339         $return = {
340             type        => 'check',
341             expression  => $item[3],
342             on_conflict => $item[5][0], 
343         }
344     }
345     |
346     DEFAULT VALUE
347     {
348         $return   = {
349             type  => 'default',
350             value => $item[2],
351         }
352     }
353
354 constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?)
355     {
356         $return         = {
357             supertype   => 'constraint',
358             type        => 'primary_key',
359             fields      => $item[2],
360             on_conflict => $item[3][0],
361         }
362     }
363     |
364     UNIQUE parens_field_list conflict_clause(?)
365     {
366         $return         = {
367             supertype   => 'constraint',
368             type        => 'unique',
369             fields      => $item[2],
370             on_conflict => $item[3][0],
371         }
372     }
373     |
374     CHECK_C '(' expr ')' conflict_clause(?)
375     {
376         $return         = {
377             supertype   => 'constraint',
378             type        => 'check',
379             expression  => $item[3],
380             on_conflict => $item[5][0],
381         }
382     }
383
384 table_name : qualified_name
385     
386 qualified_name : NAME 
387     { $return = { name => $item[1] } }
388
389 qualified_name : /(\w+)\.(\w+)/ 
390     { $return = { db_name => $1, name => $2 } }
391
392 field_name : NAME
393
394 conflict_clause : /on conflict/i conflict_algorigthm
395
396 conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
397
398 parens_field_list : '(' column_list ')'
399     { $item[2] }
400
401 column_list : field_name(s /,/)
402
403 parens_value_list : '(' VALUE(s /,/) ')'
404     { $item[2] }
405
406 expr : /[^)]+/
407
408 sort_order : /(ASC|DESC)/i
409
410 #
411 # Create Trigger
412
413 create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action
414     {
415         my $table_name = $item[8]->{'name'};
416         push @triggers, {
417             name         => $item[4],
418             is_temporary => $item[2][0] ? 1 : 0,
419             when         => $item[5][0],
420             instead_of   => 0,
421             db_event     => $item[6],
422             action       => $item[9],
423         }
424     }
425
426 create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
427     {
428         my $table_name = $item[8]->{'name'};
429         push @triggers, {
430             name         => $item[4],
431             is_temporary => $item[2][0] ? 1 : 0,
432             when         => undef,
433             instead_of   => 1,
434             db_event     => $item[6],
435             action       => $item[9],
436         }
437     }
438
439 database_event : /(delete|insert|update)/i
440
441 database_event : /update of/i column_list
442
443 trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C
444     {
445         $return = {
446             for_each => $item[1][0],
447             when     => $item[2][0],
448             steps    => $item[4],
449         }
450     }
451
452 for_each : /FOR EACH ROW/i | /FOR EACH STATEMENT/i
453
454 when : WHEN expr { $item[2] }
455
456 trigger_step : /(select|delete|insert|update)/i /[^;]+/ SEMICOLON
457     {
458         $return = join( ' ', $item[1], $item[2] )
459     }   
460
461 before_or_after : /(before|after)/i { $return = lc $1 }
462
463 instead_of : /instead of/i
464
465 view_name : qualified_name
466
467 #
468 # Create View
469 #
470 create : CREATE TEMPORARY(?) VIEW view_name AS select_statement 
471     {
472         push @views, {
473             name         => $item[4]->{'name'},
474             sql          => $item[6], 
475             is_temporary => $item[2][0] ? 1 : 0,
476         }
477     }
478
479 select_statement : SELECT /[^;]+/ SEMICOLON
480     {
481         $return = join( ' ', $item[1], $item[2] );
482     }
483
484 #
485 # Tokens
486 #
487 BEGIN_C : /begin/i
488
489 END_C : /end/i
490
491 CREATE : /create/i
492
493 TEMPORARY : /temp(orary)?/i { 1 }
494
495 TABLE : /table/i
496
497 INDEX : /index/i
498
499 NOT_NULL : /not null/i
500
501 PRIMARY_KEY : /primary key/i
502
503 CHECK_C : /check/i
504
505 DEFAULT : /default/i
506
507 TRIGGER : /trigger/i
508
509 VIEW : /view/i
510
511 SELECT : /select/i
512
513 ON : /on/i
514
515 AS : /as/i
516
517 WORD : /\w+/
518
519 WHEN : /when/i
520
521 UNIQUE : /unique/i { 1 }
522
523 SEMICOLON : ';'
524
525 NAME : /'?(\w+)'?/ { $return = $1 }
526
527 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
528     { $item[1] }
529     | /'.*?'/   
530     { 
531         # remove leading/trailing quotes 
532         my $val = $item[1];
533         $val    =~ s/^['"]|['"]$//g;
534         $return = $val;
535     }
536     | /NULL/
537     { 'NULL' }
538
539 !;
540
541 # -------------------------------------------------------------------
542 sub parse {
543     my ( $translator, $data ) = @_;
544     my $parser = Parse::RecDescent->new($GRAMMAR);
545
546     local $::RD_TRACE  = $translator->trace ? 1 : undef;
547     local $DEBUG       = $translator->debug;
548
549     unless (defined $parser) {
550         return $translator->error("Error instantiating Parse::RecDescent ".
551             "instance: Bad grammer");
552     }
553
554     my $result = $parser->startrule($data);
555     return $translator->error( "Parse failed." ) unless defined $result;
556     warn Dumper( $result ) if $DEBUG;
557
558     my $schema = $translator->schema;
559     my @tables = sort { 
560         $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
561     } keys %{ $result->{'tables'} };
562
563     for my $table_name ( @tables ) {
564         my $tdata =  $result->{'tables'}{ $table_name };
565         my $table =  $schema->add_table( 
566             name  => $tdata->{'name'},
567         ) or die $schema->error;
568
569         $table->comments( $tdata->{'comments'} );
570
571         for my $fdata ( @{ $tdata->{'fields'} } ) {
572             my $field = $table->add_field(
573                 name              => $fdata->{'name'},
574                 data_type         => $fdata->{'data_type'},
575                 size              => $fdata->{'size'},
576                 default_value     => $fdata->{'default'},
577                 is_auto_increment => $fdata->{'is_auto_inc'},
578                 is_nullable       => $fdata->{'is_nullable'},
579                 comments          => $fdata->{'comments'},
580             ) or die $table->error;
581
582             $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
583
584             for my $cdata ( @{ $fdata->{'constraints'} } ) {
585                 next unless $cdata->{'type'} eq 'foreign_key';
586                 $cdata->{'fields'} ||= [ $field->name ];
587                 push @{ $tdata->{'constraints'} }, $cdata;
588             }
589         }
590
591         for my $idata ( @{ $tdata->{'indices'} || [] } ) {
592             my $index  =  $table->add_index(
593                 name   => $idata->{'name'},
594                 type   => uc $idata->{'type'},
595                 fields => $idata->{'fields'},
596             ) or die $table->error;
597         }
598
599         for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
600             my $constraint       =  $table->add_constraint(
601                 name             => $cdata->{'name'},
602                 type             => $cdata->{'type'},
603                 fields           => $cdata->{'fields'},
604                 reference_table  => $cdata->{'reference_table'},
605                 reference_fields => $cdata->{'reference_fields'},
606                 match_type       => $cdata->{'match_type'} || '',
607                 on_delete        => $cdata->{'on_delete_do'},
608                 on_update        => $cdata->{'on_update_do'},
609             ) or die $table->error;
610         }
611     }
612
613     for my $def ( @{ $result->{'views'} || [] } ) {
614         my $view = $schema->add_view(
615             name => $def->{'name'},
616             sql  => $def->{'sql'},
617         );
618     }
619
620     for my $def ( @{ $result->{'triggers'} || [] } ) {
621         my $view                = $schema->add_trigger(
622             name                => $def->{'name'},
623             perform_action_when => $def->{'when'},
624             database_event      => $def->{'db_event'},
625             action              => $def->{'action'},
626         );
627     }
628
629     return 1;
630 }
631
632 1;
633
634 # -------------------------------------------------------------------
635 # All wholsome food is caught without a net or a trap.
636 # William Blake
637 # -------------------------------------------------------------------
638
639 =pod
640
641 =head1 AUTHOR
642
643 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
644
645 =head1 SEE ALSO
646
647 perl(1), Parse::RecDescent, SQL::Translator::Schema.
648
649 =cut