X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FODBC%2FMicrosoft_SQL_Server.pm;h=48f281b5da9c208a37ac201c2648378d65362458;hb=c9350341181c9f361af3db63f4e19b95df7730e0;hp=ff79f54cc136566046cedf4b938c1abc33321dd5;hpb=c29f2c6d6a24e683b7f6d673997f59e1b16dfb3b;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm index ff79f54..48f281b 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm @@ -3,34 +3,69 @@ use strict; use warnings; use base qw/DBIx::Class::Storage::DBI::MSSQL/; +use mro 'c3'; + +use List::Util(); sub insert_bulk { - my ($self, $source, $cols, $data) = @_; - next::method(@_); + my $self = shift; + my ($source, $cols, $data) = @_; + + my $identity_insert = 0; + + COLUMNS: + foreach my $col (@{$cols}) { + if ($source->column_info($col)->{is_auto_increment}) { + $identity_insert = 1; + last COLUMNS; + } + } + + if ($identity_insert) { + my $table = $source->from; + $self->dbh->do("SET IDENTITY_INSERT $table ON"); + } + + $self->next::method(@_); + + if ($identity_insert) { + my $table = $source->from; + $self->dbh->do("SET IDENTITY_INSERT $table OFF"); + } } sub _prep_for_execute { my $self = shift; my ($op, $extra_bind, $ident, $args) = @_; +# cast MONEY values properly + if ($op eq 'insert' || $op eq 'update') { + my $fields = $args->[0]; + my $col_info = $self->_resolve_column_info($ident, [keys %$fields]); + + for my $col (keys %$fields) { + if ($col_info->{$col}{data_type} =~ /^money\z/i) { + my $val = $fields->{$col}; + $fields->{$col} = \['CAST(? AS MONEY)', [ $col => $val ]]; + } + } + } + my ($sql, $bind) = $self->next::method (@_); - $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert'; - my %identity_insert_tables; - my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]); + if ($op eq 'insert') { + $sql .= ';SELECT SCOPE_IDENTITY()'; - foreach my $bound (@{$bind}) { - my $col = $bound->[0]; - if ($col_info->{$col}->{is_auto_increment}) { - my $table = $col_info->{$col}->{-result_source}->from; - $identity_insert_tables{$table} = 1; + my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]); + if (List::Util::first { $_->{is_auto_increment} } (values %$col_info) ) { + + my $table = $ident->from; + my $identity_insert_on = "SET IDENTITY_INSERT $table ON"; + my $identity_insert_off = "SET IDENTITY_INSERT $table OFF"; + $sql = "$identity_insert_on; $sql; $identity_insert_off"; } } - my $identity_insert_on = join '', map { "SET IDENTITY_INSERT $_ ON; " } keys %identity_insert_tables; - my $identity_insert_off = join '', map { "SET IDENTITY_INSERT $_ OFF; " } keys %identity_insert_tables; - $sql = "$identity_insert_on $sql $identity_insert_off"; - return ($sql, $bind); }