Parse-Dia-SQL
view release on metacpan or search on metacpan
lib/Parse/Dia/SQL/Output/SQLite3fk.pm view on Meta::CPAN
=head1 SYNOPSIS
use Parse::Dia::SQL;
my $dia = Parse::Dia::SQL->new(file => 'foo.dia', db => 'sqlite3fk');
print $dia->get_sql();
=head1 DESCRIPTION
This sub-class creates SQL for the SQLite database version 3.
=cut
use warnings;
use strict;
use Data::Dumper;
use File::Spec::Functions qw(catfile);
use lib q{lib};
use base q{Parse::Dia::SQL::Output}; # extends
require Parse::Dia::SQL::Logger;
require Parse::Dia::SQL::Const;
=head2 new
The constructor.
Object names in SQLite have no inherent limit. 60 has been arbitrarily chosen.
=cut
sub new {
my ( $class, %param ) = @_;
my $self = {};
# Set defaults for sqlite
$param{db} = q{sqlite3fk};
$param{object_name_max_length} = $param{object_name_max_length} || 60;
$self = $class->SUPER::new( %param );
bless( $self, $class );
return $self;
}
=head2 _get_create_table_sql
Generate create table statement for a single table using SQLite
syntax:
Includes class comments before the table definition.
Includes autoupdate triggers based on the class comment.
Includes foreign key support of the form
foreign key(thisColumn) references thatTable(thatColumn) {action}
Where {action} is the optional contraint condition, such as 'on delete cascade' exactly as entered in the diagram.
=head3 autoupdate triggers
If the class comment includes a line like:
<autoupdate:I<foo>/>
Then an 'after update' trigger is generated for this table which
executes the statement I<foo> for the updated row.
Examples of use include tracking record modification dates
(C<<autoupdate:dtModified=datetime('now')/>>) or deriving a value from
another field (C<<autoupdate:sSoundex=soundex(sName)/>>)
=cut
sub _get_create_table_sql {
my ( $self, $table ) = @_;
my $sqlstr = '';
my $temp;
my $comment;
my $tablename;
my $trigger = '';
my $update;
my $primary_keys = '';
my @columns = ();
my @primary_keys = ();
my @comments = ();
# Sanity checks on table ref
return unless $self->_check_attlist($table);
# include the comments before the table creation
$comment = $table->{comment};
if ( !defined( $comment ) ) { $comment = ''; }
$tablename = $table->{name};
$sqlstr .= $self->{newline};
if ( $comment ne "" ) {
$temp = "-- $comment";
$temp =~ s/\n/\n-- /g;
$temp =~ s/^-- <autoupdate(\s*)(.*):(.*)\/>$//mgi;
if ( $temp ne "" ) {
if ( $temp !~ /\n$/m ) { $temp .= $self->{newline}; }
$sqlstr .= $temp;
}
}
# Call the base class to generate the main create table statements
$sqlstr .= $self->SUPER::_get_create_table_sql( $table );
# Generate update triggers if required
if ( $comment =~ /<autoupdate(\s*)(.*):(.*)\/>/mi ) {
$update = $3; # what we will set it to
$trigger = $2; # the trigger suffix to use (optional)
$trigger = $tablename . "_autoupdate" . $trigger;
# Check that the column exists
( run in 1.148 second using v1.01-cache-2.11-cpan-39bf76dae61 )