[Bast-commits] r3859 -
DBIx-Class/0.08/branches/oracle_sequence/lib/DBIx/Class/Storage/DBI/Oracle
plu at dev.catalyst.perl.org
plu at dev.catalyst.perl.org
Tue Nov 6 21:57:45 GMT 2007
Author: plu
Date: 2007-11-06 21:57:44 +0000 (Tue, 06 Nov 2007)
New Revision: 3859
Modified:
DBIx-Class/0.08/branches/oracle_sequence/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
Log:
fetch nextval from sequence for insert
Modified: DBIx-Class/0.08/branches/oracle_sequence/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
===================================================================
--- DBIx-Class/0.08/branches/oracle_sequence/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm 2007-11-06 21:56:44 UTC (rev 3858)
+++ DBIx-Class/0.08/branches/oracle_sequence/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm 2007-11-06 21:57:44 UTC (rev 3859)
@@ -12,6 +12,7 @@
# In your table classes
__PACKAGE__->load_components(qw/PK::Auto Core/);
+ __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
__PACKAGE__->set_primary_key('id');
__PACKAGE__->sequence('mysequence');
@@ -30,11 +31,14 @@
# __PACKAGE__->load_components(qw/PK::Auto/);
sub _dbh_last_insert_id {
- my ($self, $dbh, $source, $col) = @_;
- my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
- my $sql = 'SELECT ' . $seq . '.currval FROM DUAL';
- my ($id) = $dbh->selectrow_array($sql);
- return $id;
+ my ($self, $dbh, $source, @columns) = @_;
+ my @ids = ();
+ foreach my $col (@columns) {
+ my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
+ my $id = $self->_sequence_fetch( 'currval', $seq );
+ push @ids, $id;
+ }
+ return @ids;
}
sub _dbh_get_autoinc_seq {
@@ -59,6 +63,32 @@
$self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
}
+=head2 insert
+
+Fetch nextval from sequence and handle insert statement.
+
+=cut
+
+sub insert {
+ my ( $self, $source, $to_insert ) = @_;
+ foreach my $col ( $source->columns ) {
+ if ( !defined $to_insert->{$col} ) {
+ my $col_info = $source->column_info($col);
+
+ if ( $col_info->{auto_nextval} ) {
+ $to_insert->{$col} = $self->_sequence_fetch( 'nextval', $col_info->{sequence} || $self->_dbh_get_autoinc_seq($self->dbh, $source) );
+ }
+ }
+ }
+ $self->next::method( $source, $to_insert );
+}
+
+sub _sequence_fetch {
+ my ( $self, $type, $seq ) = @_;
+ my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
+ return $id;
+}
+
=head2 get_autoinc_seq
Returns the sequence name for an autoincrement column
More information about the Bast-commits
mailing list