Class::DBI::SQLite - Extension to Class::DBI for sqlite


Class-DBI-SQLite documentation Contained in the Class-DBI-SQLite distribution.

Index


Code Index:

NAME

Top

Class::DBI::SQLite - Extension to Class::DBI for sqlite

SYNOPSIS

Top

  package Film;
  use base qw(Class::DBI::SQLite);
  __PACKAGE__->set_db('Main', 'dbi:SQLite:dbname=dbfile', '', '');
  __PACKAGE__->set_up_table('Movies');

  package main;
  my $film = Film->create({
     name  => 'Bad Taste',
     title => 'Peter Jackson',
  });
  my $id = $film->id;		# auto-incremented

DESCRIPTION

Top

Class::DBI::SQLite is an extension to Class::DBI for DBD::SQLite. It allows you to populate an auto-incremented row id after insert.

The set_up_table method automates the setup of columns and primary key(s) via the SQLite PRAGMA statement.

AUTHOR

Top

Tatsuhiko Miyagawa <miyagawa@bulknews.net>

set_up_table implementation by Tomohiro Ikebe <ikebe@cpan.org>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

Class::DBI, DBD::SQLite


Class-DBI-SQLite documentation Contained in the Class-DBI-SQLite distribution.

package Class::DBI::SQLite;

use strict;
use vars qw($VERSION);
$VERSION = "0.11";

require Class::DBI;
use base qw(Class::DBI);

sub _auto_increment_value {
    my $self = shift;
    return $self->db_Main->func("last_insert_rowid");
}

sub set_up_table {
    my($class, $table) = @_;

    # find all columns.
    my $sth = $class->db_Main->prepare("PRAGMA table_info('$table')");
    $sth->execute();
    my @columns;
    while (my $row = $sth->fetchrow_hashref) {
	push @columns, $row->{name};
    }
    $sth->finish;

    # find primary key. so complex ;-(
    $sth = $class->db_Main->prepare(<<'SQL');
SELECT sql FROM sqlite_master WHERE tbl_name = ?
SQL
    $sth->execute($table);
    my($sql) = $sth->fetchrow_array;
    $sth->finish;
    my ($primary) = $sql =~ m/
        (?:\(|\,) # either a ( to start the definition or a , for next
        \s*       # maybe some whitespace
        (\w+)     # the col name
        [^,]*     # anything but the end or a ',' for next column
        PRIMARY\sKEY/sxi;
    my @pks;
    if ($primary) {
        @pks = ($primary);
    } else {
        my ($pks)= $sql =~ m/PRIMARY\s+KEY\s*\(\s*([^)]+)\s*\)/;
        @pks = split(m/\s*\,\s*/, $pks) if $pks;
    }
    $class->table($table);
    $class->columns(Primary => @pks);
    $class->columns(All => @columns);
}

1;

__END__