Changeset 715

Show
Ignore:
Timestamp:
08/09/02 16:03:12
Author:
miyagawa
Message:

0.02

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Class-DBI-SQLite/trunk/Changes

    r560 r715  
    11Revision history for Perl extension Class::DBI::SQLite 
     2 
     30.02  Fri Aug  9 15:59:44 JST 2002 
     4        - Added tests to see commit() really works (needs Ima::DBI >= 0.27) 
     5        * Added set_up_table() method 
     6          (Thanks to Tomohiro Ikebe <ikebe@cpan.org>) 
    27 
    380.01  Sat Feb 23 22:49:15 2002 
  • Class-DBI-SQLite/trunk/Makefile.PL

    r560 r715  
    66        'Test::More' => 0.32, 
    77        'Class::DBI' => 0.85, 
     8        'Ima::DBI'   => 0.27, 
    89        'DBD::SQLite' => 0.07, 
     10        'SQL::Parser' => 1.004, 
    911    }, 
    1012); 
  • Class-DBI-SQLite/trunk/lib/Class/DBI/SQLite.pm

    r560 r715  
    33use strict; 
    44use vars qw($VERSION); 
    5 $VERSION = 0.01
     5$VERSION = 0.02
    66 
    77require Class::DBI; 
    88use base qw(Class::DBI); 
     9use SQL::Statement; 
    910 
    1011sub _auto_increment_value { 
     
    1314} 
    1415 
     16sub set_up_table { 
     17    my($class, $table) = @_; 
     18 
     19    # find all columns. 
     20    my $sth = $class->db_Main->prepare(<<'SQL'); 
     21PRAGMA table_info(?) 
     22SQL 
     23    $sth->execute($table); 
     24    my @columns; 
     25    while (my $row = $sth->fetchrow_hashref) { 
     26        push @columns,$row->{name}; 
     27    } 
     28    $sth->finish; 
     29 
     30    # find primary key. so complex ;-( 
     31    $sth = $class->db_Main->prepare(<<'SQL'); 
     32SELECT sql FROM sqlite_master WHERE tbl_name = ? 
     33SQL 
     34    $sth->execute($table); 
     35    my($sql) = $sth->fetchrow_array; 
     36    $sth->finish; 
     37 
     38    my $parser = SQL::Parser->new('AnyData', { RaiseError => 1}); 
     39    $parser->parse($sql); 
     40    my $structure = $parser->structure; 
     41    my $primary; 
     42    foreach my $key (keys %{$structure->{column_defs}}) { 
     43        my $def = $structure->{column_defs}->{$key}; 
     44        next unless $def->{constraints}; 
     45        foreach my $constraint(@{$def->{constraints}}) { 
     46            if (uc($constraint) eq 'PRIMARY KEY') { 
     47                $primary = $key; 
     48                last; 
     49            } 
     50        } 
     51    } 
     52    $class->table($table); 
     53    $class->columns(All => @columns); 
     54    $class->columns(Primary => $primary); 
     55} 
     56 
    15571; 
     58 
    1659__END__ 
    1760 
     
    2568  use base qw(Class::DBI::SQLite); 
    2669  __PACKAGE__->set_db('Main', 'dbi:SQLite:dbname=dbfile', '', ''); 
    27   __PACKAGE__->table('Movies'); 
    28   __PACKAGE__->columns(Primary => qw(id)); 
    29   __PACKAGE__->columns(All => qw(id title director)); 
     70  __PACKAGE__->set_up_table('Movies'); 
    3071 
    3172  package main; 
     
    4182which allows you to populate auto incremented row id after insert. 
    4283 
     84C<set_up_table> method allows you to automate the setup of columns and 
     85primary key by using of SQLite PRAGMA statement (with SQL::Statement 
     86module) 
     87 
    4388=head1 AUTHOR 
    4489 
    4590Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt> 
     91 
     92C<set_up_table> implementation by Tomohiro Ikebe E<lt>ikebe@cpan.orgE<gt> 
    4693 
    4794This library is free software; you can redistribute it and/or modify 
     
    5097=head1 SEE ALSO 
    5198 
    52 L<Class::DBI>, L<DBD::SQLite> 
     99L<Class::DBI>, L<DBD::SQLite> L<SQL::Statement> 
    53100 
    54101=cut 
  • Class-DBI-SQLite/trunk/t/01_sqlite.t

    r560 r715  
    1919} 
    2020 
     21Film->dbi_commit; 
     22Film->db_Main->disconnect; 
     23 
    2124my @movies = Film->retrieve_all; 
    2225is @movies, 10, '10 movies out there'; 
  • Class-DBI-SQLite/trunk/t/lib/Film.pm

    r560 r715  
    33 
    44use File::Temp qw(tempdir tempfile); 
    5 my $dir = tempdir( CLEANUP => 1 ); 
    6 my($fh, $filename) = tempfile( DIR => $dir ); 
    75 
    86use base qw(Class::DBI::SQLite); 
    9 __PACKAGE__->set_db('Main', "dbi:SQLite:dbname=$filename", '', ''); 
     7BEGIN { 
     8    my $dir = tempdir( CLEANUP => 1 ); 
     9    my($fh, $filename) = tempfile( DIR => $dir ); 
     10    __PACKAGE__->set_db('Main', "dbi:SQLite:dbname=$filename", '', ''); 
     11
     12 
    1013__PACKAGE__->table('Movies'); 
    1114__PACKAGE__->columns(Primary => qw(id)); 
     
    2326    ; 
    2427} 
    25  
    26 1;