package SQL::Translator::Parser::SQLite;
# -------------------------------------------------------------------
-# $Id: SQLite.pm,v 1.1 2003-10-03 00:20:51 kycl4rk Exp $
+# $Id: SQLite.pm,v 1.2 2003-10-04 01:16:39 kycl4rk Exp $
# -------------------------------------------------------------------
# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
# darren chamberlain <darren@cpan.org>,
use strict;
use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
use Data::Dumper;
$GRAMMAR = q!
{
- my ( %tables, $table_order, @table_comments );
+ my ( %tables, $table_order, @table_comments, @views, @triggers );
}
#
# won't cause the failure needed to know that the parse, as a whole,
# failed. -ky
#
-startrule : statement(s) eofile { \%tables }
+startrule : statement(s) eofile {
+ $return => {
+ tables => \%tables,
+ views => \@views,
+ triggers => \@triggers,
+ }
+}
eofile : /^\Z/
create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action
{
my $table_name = $item[8]->{'name'};
- push @{ $tables{ $table_name }{'triggers'} }, {
+ push @triggers, {
name => $item[4],
is_temporary => $item[2][0] ? 1 : 0,
when => $item[5][0],
create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
{
my $table_name = $item[8]->{'name'};
- push @{ $tables{ $table_name }{'triggers'} }, {
+ push @triggers, {
name => $item[4],
is_temporary => $item[2][0] ? 1 : 0,
when => undef,
view_name : qualified_name
#
+# Create View
+#
+create : CREATE TEMPORARY(?) VIEW view_name AS select_statement
+ {
+ push @views, {
+ name => $item[4]->{'name'},
+ sql => $item[6],
+ is_temporary => $item[2][0] ? 1 : 0,
+ }
+ }
+
+select_statement : SELECT /[^;]+/ SEMICOLON
+ {
+ $return = join( ' ', $item[1], $item[2] );
+ }
+
+#
# Tokens
#
BEGIN_C : /begin/i
TRIGGER : /trigger/i
+VIEW : /view/i
+
+SELECT : /select/i
+
ON : /on/i
+AS : /as/i
+
WORD : /\w+/
WHEN : /when/i
SEMICOLON : ';'
-NAME : /'?(\w+)'?/ { $return = $1 }
+NAME : /'?(\w+)'?/ { $return = $1 }
-VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
+VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
{ $item[1] }
| /'.*?'/
{
my $schema = $translator->schema;
my @tables = sort {
$result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
- } keys %{ $result };
+ } keys %{ $result->{'tables'} };
for my $table_name ( @tables ) {
- my $tdata = $result->{ $table_name };
+ my $tdata = $result->{'tables'}{ $table_name };
my $table = $schema->add_table(
name => $tdata->{'name'},
) or die $schema->error;
}
}
+ for my $def ( @{ $result->{'views'} || [] } ) {
+ my $view = $schema->add_view(
+ name => $def->{'name'},
+ sql => $def->{'sql'},
+ );
+ }
+
+ for my $def ( @{ $result->{'triggers'} || [] } ) {
+ my $view = $schema->add_trigger(
+ name => $def->{'name'},
+ perform_action_when => $def->{'when'},
+ database_event => $def->{'db_event'},
+ action => $def->{'action'},
+ );
+ }
+
return 1;
}