Subject: | SQLite needs special treatment for auto_increment |
Andrew,
SQLite does support triggers, but somehow they don't work with Class::DBI::SQLite. The attachment is a hack around. It disables precreate_table. The create_table function was overloaded to skip adding the primary key line. Use is now:
use Class::DBI::DDL::SQLite;
__PACKAGE__->column_definitions([
[ primary => 'integer PRIMARY KEY AUTOINCREMENT' ]
]);
The real solution would be making triggers work with Class::DBI::SQLite.
Thank you for the great DDL packages!
Johannes
package Class::DBI::DDL::SQLite;
use 5.008;
use strict;
use warnings;
our $VERSION = '1.01';
use base qw(Class::DBI::DDL);
=head1 NAME
Class::DBI::DDL::SQLite - Perform driver dependent work for SQLite
=head1 DESCRIPTION
Do not use this package directly. Intead, it will automatically be imported and
used by L<Class::DBI::DDL> when the underlying database uses the L<DBD::mysql>
driver.
The only method here that works different from the default is
C<pre_create_table>. This method is defined to do nothing since auto_increment
works normally with the notation we've chosen and C<Class::DBI> is best written
toward MySQL.
=cut
sub pre_create_table { }
sub post_create_table { }
sub pre_drop_table { }
sub post_drop_table { }
sub create_table {
my $class = shift;
my $on_create = shift;
my $dbh = $class->db_Main;
my $table = $class->table;
my @tables = $class->_list_tables;
if (!grep /^$table$/, @tables) {
$class->_load_driver_specifics;
$class->__ddl_helper->pre_create_table($class);
my @decls;
for my $column (@{ $class->column_definitions }) {
push @decls, join(' ', @$column);
}
# my @primary = $class->primary_columns;
# push @decls, sprintf('PRIMARY KEY (%s)', join(',', @primary));
for my $index (@{ $class->index_definitions }) {
my $type = $$index[0];
if ($type =~ /unique/i) {
if (ref $$index[1]) {
push @decls, sprintf('UNIQUE (%s)', join(',', @{$$index[1]}));
} else {
push @decls, sprintf('UNIQUE (%s)', join(',', @$index[1 .. $#$index]));
}
} elsif ($type =~ /foreign/i) {
my @from = ref $$index[1] ? @{$$index[1]} : ($$index[1]);
my $table = $$index[2];
my @to = ref $$index[3] ? @{$$index[3]} : ($$index[3]);
push @decls, sprintf('FOREIGN KEY (%s) REFERENCES %s (%s)',
join(',', @from), $table->table, join(',', @to));
} else {
Class::DBI::_croak "Unknown index type $type.";
}
}
$class->sql_create_table(join(', ', @decls))->execute;
$class->__ddl_helper->post_create_table($class);
if (defined $on_create and ref $on_create eq 'CODE') {
&$on_create;
}
}
}
=head1 SEE ALSO
L<Class::DBI>, L<DBI>, L<Class::DBI::DDL>, L<DBD::SQLite>
=head1 AUTHOR
Johannes Niess
=head1 LICENSE AND COPYRIGHT
Copyright 2005 Johannes Niess. All Rights Reserved.
This module is free software and is distributed under the same license as Perl
itself.
=cut
1