File Coverage

blib/lib/Class/DBI/AutoIncrement/Simple.pm
Criterion Covered Total %
statement 9 20 45.0
branch 0 2 0.0
condition 0 5 0.0
subroutine 3 6 50.0
pod 2 2 100.0
total 14 35 40.0


line stmt bran cond sub pod time code
1             package Class::DBI::AutoIncrement::Simple;
2              
3 1     1   23071 use warnings;
  1         3  
  1         35  
4 1     1   6 use strict;
  1         3  
  1         38  
5 1     1   6 use base 'Class::DBI';
  1         6  
  1         1399  
6             our $VERSION = '0.02';
7              
8             sub __add_row {
9 0     0     my $self = shift;
10 0           my $method = shift;
11 0           my $pk = $self->primary_column;
12 0   0       $_[0]->{$pk} ||= ($self->maximum_value_of($pk)||0) + 1;
      0        
13 0 0         if( $method eq 'create' ){
14 0           return $self->SUPER::create(@_);
15             }else{
16 0           return $self->SUPER::insert(@_);
17             }
18             }
19              
20             sub insert {
21 0     0 1   my $self = shift;
22 0           return $self->__add_row('insert', @_);
23             }
24              
25             sub create {
26 0     0 1   my $self = shift;
27 0           return $self->__add_row('create', @_);
28             }
29              
30             1; # End of Class::DBI::AutoIncrement::Simple
31              
32             =pod
33              
34             =head1 NAME
35              
36             Class::DBI::AutoIncrement::Simple - Add autoincrementing to a Class::DBI subclass
37              
38             =head1 VERSION
39              
40             Version 0.01
41              
42             =head1 SYNOPSIS
43              
44             Provides an alternative L base class that automatically uses an autoincremented value for the (single column) primary key when creating new rows.
45              
46             package My::DB::Base;
47             use base 'Class::DBI::AutoIncrement::Simple';
48             __PACKAGE__->connection("DBI:CSV:f_dir=data/");
49              
50             package My::DB::Table1;
51             use base 'My::DB::Base';
52             __PACKAGE__->table('table1');
53             __PACKAGE__->columns(Primary => qw/ my_id /);
54             __PACKAGE__->columns(Essential => qw/ first_name last_name / );
55              
56             For newer versions of Class::DBI
57              
58             my $foo = My::DB::Table1->insert({first_name=>'foo', last_name=>'bar'});
59             warn $foo->my_id; # will be the autoincremented value
60              
61             my $bar = My::DB::Table1->insert({my_id => 1234, first_name=>'foo', last_name=>'bar'});
62             warn $foo->my_id; # will be 1234
63              
64             For older versions of Class::DBI
65              
66             my $foo = My::DB::Table1->create({first_name=>'foo', last_name=>'bar'});
67             warn $foo->my_id; # will be the autoincremented value
68              
69             my $bar = My::DB::Table1->create({my_id => 1234, first_name=>'foo', last_name=>'bar'});
70             warn $foo->my_id; # will be 1234
71              
72             =head1 METHODS
73              
74             =head2 insert
75              
76             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.
77              
78             =head2 create
79              
80             Same as L -- provided for backwards-compatibility of Class::DBI
81              
82             =head1 NOTES
83              
84             This requires/assumes that the class has a single-column primary key.
85              
86             This could also be accomplished by just directly adding this method overload to your base or subclass:
87              
88             sub insert {
89             my $self = shift;
90             my $pk = $self->primary_column;
91             $_[0]->{$pk} ||= ($self->maximum_value_of($pk)||0) + 1;
92             return $self->SUPER::insert(@_);
93             }
94              
95             There is also L 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.
96              
97             But this module is meant to be "Simple" :)
98              
99             =head1 PREREQUISITES
100              
101             =over 4
102              
103             =item L
104              
105             =back
106              
107             The following are required for the I test script:
108              
109             =over 4
110              
111             =item L
112              
113             =item L
114              
115             =item L
116              
117             And all of it's deps (DBD::File, SQL::Statement, etc).
118              
119             =back
120              
121              
122             =head1 AUTHOR
123              
124             David Westbrook, C<< >>
125              
126             =head1 BUGS
127              
128             Please report any bugs or feature requests to
129             C, or through the web interface at
130             L.
131             I will be notified, and then you'll automatically be notified of progress on
132             your bug as I make changes.
133              
134             I'm also available by email or via '/msg davidrw' on L.
135              
136             =head1 SUPPORT
137              
138             You can find documentation for this module with the perldoc command.
139              
140             perldoc Class::DBI::AutoIncrement::Simple
141              
142             You can also look for information at:
143              
144             =over 4
145              
146             =item * AnnoCPAN: Annotated CPAN documentation
147              
148             L
149              
150             =item * CPAN Ratings
151              
152             L
153              
154             =item * RT: CPAN's request tracker
155              
156             L
157              
158             =item * Search CPAN
159              
160             L
161              
162             =back
163              
164             =head1 COPYRIGHT & LICENSE
165              
166             Copyright 2006 David Westbrook, all rights reserved.
167              
168             This program is free software; you can redistribute it and/or modify it
169             under the same terms as Perl itself.
170              
171             =cut
172