The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::DBI::AutoIncrement::Simple - Add autoincrementing to a Class::DBI subclass

VERSION

Version 0.01

SYNOPSIS

Provides an alternative Class::DBI base class that automatically uses an autoincremented value for the (single column) primary key when creating new rows.

    package My::DB::Base;
    use base 'Class::DBI::AutoIncrement::Simple';
    __PACKAGE__->connection("DBI:CSV:f_dir=data/");

    package My::DB::Table1;
    use base 'My::DB::Base';
    __PACKAGE__->table('table1');
    __PACKAGE__->columns(Primary => qw/ my_id /);
    __PACKAGE__->columns(Essential => qw/ first_name last_name / );

For newer versions of Class::DBI

    my $foo = My::DB::Table1->insert({first_name=>'foo', last_name=>'bar'});
    warn $foo->my_id;    # will be the autoincremented value

    my $bar = My::DB::Table1->insert({my_id => 1234, first_name=>'foo', last_name=>'bar'});
    warn $foo->my_id;    # will be 1234

For older versions of Class::DBI

    my $foo = My::DB::Table1->create({first_name=>'foo', last_name=>'bar'});
    warn $foo->my_id;    # will be the autoincremented value

    my $bar = My::DB::Table1->create({my_id => 1234, first_name=>'foo', last_name=>'bar'});
    warn $foo->my_id;    # will be 1234

METHODS

insert

Overloads the Class::DBI->insert() method to first (if not provided) give the primary key an autoincremented value, then calls insert() in the base class.

create

Same as insert -- provided for backwards-compatibility of Class::DBI

NOTES

This requires/assumes that the class has a single-column primary key.

This could also be accomplished by just directly adding this method overload to your base or subclass:

  sub insert {
    my $self = shift;
    my $pk = $self->primary_column;
    $_[0]->{$pk} ||= ($self->maximum_value_of($pk)||0) + 1;
    return $self->SUPER::insert(@_);
  }

There is also Class::DBI::AutoIncrement which is different in nature -- it works by multiple inheritance (you inherit from both it and Class::DBI) and so has some issues there (see its Limitations section); but it does have more features in that you can specify the start and step size of a sequence and do some caching.

But this module is meant to be "Simple" :)

PREREQUISITES

Class::DBI

The following are required for the t/csv.t test script:

File::Temp
File::Basename
DBD::CSV

And all of it's deps (DBD::File, SQL::Statement, etc).

AUTHOR

David Westbrook, <dwestbrook at gmail.com>

BUGS

Please report any bugs or feature requests to bug-class-dbi-autoincrement-simple at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-DBI-AutoIncrement-Simple. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

I'm also available by email or via '/msg davidrw' on http://perlmonks.org.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Class::DBI::AutoIncrement::Simple

You can also look for information at:

COPYRIGHT & LICENSE

Copyright 2006 David Westbrook, all rights reserved.

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