$self->dbh_do('_dbh_execute', @_); # retry over disconnects
}
-sub insert {
+sub _prefetch_autovalues {
my ($self, $source, $to_insert) = @_;
my $colinfo = $source->columns_info;
- # mix with auto-nextval marked values (a bit of a speed hit, but
- # no saner way to handle this yet)
- my $auto_nextvals = {} ;
+ my %values;
for my $col (keys %$colinfo) {
if (
$colinfo->{$col}{auto_nextval}
ref $to_insert->{$col} eq 'SCALAR'
)
) {
- $auto_nextvals->{$col} = $self->_sequence_fetch(
+ $values{$col} = $self->_sequence_fetch(
'nextval',
( $colinfo->{$col}{sequence} ||=
$self->_dbh_get_autoinc_seq($self->_get_dbh, $source, $col)
}
}
+ \%values;
+}
+
+sub insert {
+ my ($self, $source, $to_insert) = @_;
+
+ my $prefetched_values = $self->_prefetch_autovalues($source, $to_insert);
+
# fuse the values
- $to_insert = { %$to_insert, %$auto_nextvals };
+ $to_insert = { %$to_insert, %$prefetched_values };
# list of primary keys we try to fetch from the database
# both not-exsists and scalarrefs are considered
my ($rv, $sth) = $self->_execute('insert' => [], $source, $bind_attributes, $to_insert, $sqla_opts);
- my %returned_cols = %$auto_nextvals;
+ my %returned_cols;
if (my $retlist = $sqla_opts->{returning}) {
my @ret_vals = try {
@returned_cols{@$retlist} = @ret_vals if @ret_vals;
}
- return \%returned_cols;
+ return { %$prefetched_values, %returned_cols };
}
sub _new_uuid { 'UUIDTOSTR(NEWID())' }
-sub insert {
+sub _prefetch_autovalues {
my $self = shift;
my ($source, $to_insert) = @_;
+ my $values = $self->next::method(@_);
+
my $colinfo = $source->columns_info;
my $identity_col =
- first { $_->{is_auto_increment} } values %$colinfo;
+ first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
# user might have an identity PK without is_auto_increment
if (not $identity_col) {
};
if (defined $identity) {
- $to_insert->{$identity_col} = $identity;
+ $values->{$identity_col} = $identity;
$self->_identity($identity);
}
}
- return $self->next::method(@_);
+ return $values;
}
# convert UUIDs to strings in selects
first { $bind_info->{$_}{is_auto_increment} }
keys %$bind_info
;
+
+ my $columns_info = blessed $ident && $ident->columns_info;
+
my $identity_col =
- blessed $ident &&
- first { $_->{is_auto_increment} } values %{ $ident->columns_info }
+ $columns_info &&
+ first { $columns_info->{$_}{is_auto_increment} }
+ keys %$columns_info
;
if (($op eq 'insert' && $bound_identity_col) ||
my $self = shift;
my ($source, $to_insert) = @_;
+ my $columns_info = $source->columns_info;
+
my $identity_col =
- (first { $_->{is_auto_increment} } values %{ $source->columns_info } )
+ (first { $columns_info->{$_}{is_auto_increment} }
+ keys %$columns_info )
|| '';
# check for empty insert
my $table = $source->name;
+ my $columns_info = $source->columns_info;
+
my $identity_col =
- first { $_->{is_auto_increment} } values %{ $source->columns_info };
+ first { $columns_info->{$_}{is_auto_increment} }
+ keys %$columns_info;
my $is_identity_update = $identity_col && defined $fields->{$identity_col};
my $self = shift;
my ($source, $cols, $data) = @_;
+ my $columns_info = $source->columns_info;
+
my $identity_col =
- first { $_->{is_auto_increment} } values %{ $source->columns_info };
+ first { $columns_info->{$_}{is_auto_increment} }
+ keys %$columns_info;
my $is_identity_insert = (first { $_ eq $identity_col } @{$cols}) ? 1 : 0;