Fix index issue in Parser::DBI::PostgreSQL
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / SQLite.pm
index 0de6e12..1024330 100644 (file)
@@ -224,7 +224,7 @@ comment : /\/\*/ /[^\*]+/ /\*\//
 #
 # Create Index
 #
-create : CREATE TEMPORARY(?) UNIQUE(?) INDEX WORD ON table_name parens_field_list conflict_clause(?) SEMICOLON
+create : CREATE TEMPORARY(?) UNIQUE(?) INDEX NAME ON table_name parens_field_list conflict_clause(?) SEMICOLON
     {
         my $db_name    = $item[7]->{'db_name'} || '';
         my $table_name = $item[7]->{'name'};
@@ -271,7 +271,7 @@ create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLO
 
 definition : constraint_def | column_def 
 
-column_def: comment(s?) NAME type(?) column_constraint(s?)
+column_def: comment(s?) NAME type(?) column_constraint_def(s?)
     {
         my $column = {
             supertype      => 'column',
@@ -304,6 +304,9 @@ column_def: comment(s?) NAME type(?) column_constraint(s?)
             elsif ( $c->{'type'} eq 'default' ) {
                 $column->{'default'} = $c->{'value'};
             }
+            elsif ( $c->{'type'} eq 'autoincrement' ) {
+                $column->{'is_auto_inc'} = 1;
+            }
         }
 
         $column;
@@ -317,6 +320,16 @@ type : WORD parens_value_list(?)
         }
     }
 
+column_constraint_def : CONSTRAINT constraint_name column_constraint
+    {
+        $return = {
+            name => $item[2],
+            %{ $item[3] },
+        }
+    }
+    |
+    column_constraint
+
 column_constraint : NOT_NULL conflict_clause(?)
     {
         $return = {
@@ -357,8 +370,41 @@ column_constraint : NOT_NULL conflict_clause(?)
             value => $item[2],
         }
     }
+    |
+    REFERENCES ref_def
+    {
+        $return   = {
+            type             => 'foreign_key',
+            reference_table  => $item[2]{'reference_table'},
+            reference_fields => $item[2]{'reference_fields'},
+        }
+    }
+    |
+    AUTOINCREMENT
+    {
+        $return = {
+            type => 'autoincrement',
+        }
+    }
+
+constraint_def : comment(s?) CONSTRAINT constraint_name table_constraint
+    {
+        $return = {
+            comments => $item[1],
+            name => $item[3],
+            %{ $item[4] },
+        }
+    }
+    |
+    comment(s?) table_constraint
+    {
+        $return = {
+            comments => $item[1],
+            %{ $item[2] },
+        }
+    }
 
-constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?)
+table_constraint : PRIMARY_KEY parens_field_list conflict_clause(?)
     {
         $return         = {
             supertype   => 'constraint',
@@ -387,6 +433,20 @@ constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?)
             on_conflict => $item[5][0],
         }
     }
+    |
+    FOREIGN_KEY parens_field_list REFERENCES ref_def
+    {
+      $return = {
+        supertype        => 'constraint',
+        type             => 'foreign_key',
+        fields           => $item[2],
+        reference_table  => $item[4]{'reference_table'},
+        reference_fields => $item[4]{'reference_fields'},
+      }
+    }
+
+ref_def : /(\w+)\s*\((\w+)\)/
+    { $return = { reference_table => $1, reference_fields => $2 } }
 
 table_name : qualified_name
     
@@ -398,6 +458,8 @@ qualified_name : /(\w+)\.(\w+)/
 
 field_name : NAME
 
+constraint_name : NAME
+
 conflict_clause : /on conflict/i conflict_algorigthm
 
 conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
@@ -522,6 +584,8 @@ NOT_NULL : /not null/i
 
 PRIMARY_KEY : /primary key/i
 
+FOREIGN_KEY : /foreign key/i
+
 CHECK_C : /check/i
 
 DEFAULT : /default/i
@@ -540,11 +604,17 @@ WORD : /\w+/
 
 WHEN : /when/i
 
+REFERENCES : /references/i
+
+CONSTRAINT : /constraint/i
+
+AUTOINCREMENT : /autoincrement/i
+
 UNIQUE : /unique/i { 1 }
 
 SEMICOLON : ';'
 
-NAME : /'?(\w+)'?/ { $return = $1 }
+NAME : /["']?(\w+)["']?/ { $return = $1 }
 
 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
     { $item[1] }
@@ -617,7 +687,7 @@ sub parse {
         for my $idata ( @{ $tdata->{'indices'} || [] } ) {
             my $index  =  $table->add_index(
                 name   => $idata->{'name'},
-                type   => uc $idata->{'type'},
+                type   => uc ($idata->{'type'}||''),
                 fields => $idata->{'fields'},
             ) or die $table->error;
         }
@@ -630,8 +700,10 @@ sub parse {
                 reference_table  => $cdata->{'reference_table'},
                 reference_fields => $cdata->{'reference_fields'},
                 match_type       => $cdata->{'match_type'} || '',
-                on_delete        => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
-                on_update        => $cdata->{'on_update'} || $cdata->{'on_update_do'},
+                on_delete        => $cdata->{'on_delete'} 
+                                 || $cdata->{'on_delete_do'},
+                on_update        => $cdata->{'on_update'} 
+                                 || $cdata->{'on_update_do'},
             ) or die $table->error;
         }
     }
@@ -667,7 +739,7 @@ sub parse {
 
 =head1 AUTHOR
 
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
+Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
 
 =head1 SEE ALSO