From: John Napiorkowski Date: Sat, 18 Nov 2006 00:48:30 +0000 (+0000) Subject: (no commit message) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6e399b4faae26e6661c8bd734a31e619c97b78b8;p=dbsrgits%2FDBIx-Class-Historic.git --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 79d9cde..5212799 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1107,7 +1107,15 @@ sub update { unless ref $values eq 'HASH'; my $cond = $self->_cond_for_update_delete; - + + my $bind_attributes; + foreach my $column ($self->result_source->columns) { + + $bind_attributes->{$column} = $self->result_source->column_info($column)->{bind_attributes} + if defined $self->result_source->column_info($column)->{bind_attributes}; + } + $self->result_source->storage->bind_attributes($bind_attributes); + return $self->result_source->storage->update( $self->result_source->from, $values, $cond ); diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 57f883f..6bf479c 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -74,7 +74,15 @@ sub insert { my $source = $self->{result_source}; $self->throw_exception("No result_source set on this object; can't insert") unless $source; - #use Data::Dumper; warn Dumper($self); + + my $bind_attributes; + foreach my $column ($self->result_source->columns) { + + $bind_attributes->{$column} = $self->result_source->column_info($column)->{bind_attributes} + if defined $self->result_source->column_info($column)->{bind_attributes}; + } + $self->result_source->storage->bind_attributes($bind_attributes); + $source->storage->insert($source->from, { $self->get_columns }); $self->in_storage(1); $self->{_dirty_columns} = {}; @@ -116,6 +124,15 @@ sub update { my $ident_cond = $self->ident_condition; $self->throw_exception("Cannot safely update a row in a PK-less table") if ! keys %$ident_cond; + + my $bind_attributes; + foreach my $column ($self->result_source->columns) { + + $bind_attributes->{$column} = $self->result_source->column_info($column)->{bind_attributes} + if defined $self->result_source->column_info($column)->{bind_attributes}; + } + $self->result_source->storage->bind_attributes($bind_attributes); + my $rows = $self->result_source->storage->update( $self->result_source->from, \%to_update, $ident_cond); if ($rows == 0) { diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index a0a34a8..6859f1c 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -14,7 +14,7 @@ use IO::File; __PACKAGE__->mk_group_accessors( 'simple' => qw/_connect_info _dbh _sql_maker _sql_maker_opts _conn_pid _conn_tid - disable_sth_caching cursor on_connect_do transaction_depth/ + disable_sth_caching cursor on_connect_do transaction_depth bind_attributes/ ); BEGIN { @@ -661,7 +661,7 @@ sub dbh { sub _sql_maker_args { my ($self) = @_; - return ( limit_dialect => $self->dbh, %{$self->_sql_maker_opts} ); + return ( bindtype=>'columns', limit_dialect => $self->dbh, %{$self->_sql_maker_opts} ); } sub sql_maker { @@ -816,22 +816,49 @@ sub _prep_for_execute { } sub _execute { - my $self = shift; - - my ($sql, @bind) = $self->_prep_for_execute(@_); - + my ($self, $op, $extra_bind, $ident, @args) = @_; + my ($sql, @bind) = $self->sql_maker->$op($ident, @args); + unshift(@bind, @$extra_bind) if $extra_bind; if ($self->debug) { - my @debug_bind = map { defined $_ ? qq{'$_'} : q{'NULL'} } @bind; + my @debug_bind = map { defined ($_ && $_->[1]) ? qq{'$_->[1]'} : q{'NULL'} } @bind; $self->debugobj->query_start($sql, @debug_bind); } + my $sth = eval { $self->sth($sql,$op) }; - my $sth = $self->sth($sql); + if (!$sth || $@) { + $self->throw_exception( + 'no sth generated via sql (' . ($@ || $self->_dbh->errstr) . "): $sql" + ); + } my $rv; if ($sth) { my $time = time(); - $rv = eval { $sth->execute(@bind) }; + + $rv = eval { + + my $placeholder_index = 1; + my $bind_attributes = $self->bind_attributes; + + foreach my $bound (@bind) { + + my $attributes = {}; + my($column_name, $data) = @$bound; + + if( $bind_attributes ) { + $attributes = $bind_attributes->{$column_name} + if defined $bind_attributes->{$column_name}; + } + $data = ref $data ? ''.$data : $data; # stringify args + + $sth->bind_param($placeholder_index, $data, $attributes); + $placeholder_index++; + } + $sth->execute; + }; + $self->bind_attributes({}); + if ($@ || !$rv) { $self->throw_exception("Error executing '$sql': ".($@ || $sth->errstr)); } @@ -839,7 +866,7 @@ sub _execute { $self->throw_exception("'$sql' did not generate a statement."); } if ($self->debug) { - my @debug_bind = map { defined $_ ? qq{`$_'} : q{`NULL'} } @bind; + my @debug_bind = map { defined ($_ && $_->[1]) ? qq{'$_->[1]'} : q{'NULL'} } @bind; $self->debugobj->query_end($sql, @debug_bind); } return (wantarray ? ($rv, $sth, @bind) : $rv); diff --git a/t/bindtype_columns.t b/t/bindtype_columns.t new file mode 100644 index 0000000..2100cca --- /dev/null +++ b/t/bindtype_columns.t @@ -0,0 +1,41 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +my $schema = DBICTest->init_schema(); + +plan tests => 2; + +# figure out if we've got a version of sqlite that is older than 3.2.6, in +# which case COUNT(DISTINCT()) doesn't work +my $is_broken_sqlite = 0; +my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) = + split /\./, $schema->storage->dbh->get_info(18); +if( $schema->storage->dbh->get_info(17) eq 'SQLite' && + ( ($sqlite_major_ver < 3) || + ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) || + ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) { + $is_broken_sqlite = 1; +} + + +#Bindtest +{ + my $new = $schema->resultset("Artist")->new({ + + artistid=>25, + name=>'JohnNapiorkowski', + }); + + $new->update_or_insert; + + my $resultset = $schema->resultset("Artist")->find({artistid=>25}); + + is($resultset->id, 25, 'Testing New ID'); + is($resultset->name, 'JohnNapiorkowski', 'Testing New Name'); +} + + diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index cf6eb3a..8799188 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -12,12 +12,14 @@ __PACKAGE__->source_info({ __PACKAGE__->add_columns( 'artistid' => { data_type => 'integer', - is_auto_increment => 1 + is_auto_increment => 1, + bind_attributes => { testkey1 => 1}, }, 'name' => { data_type => 'varchar', size => 100, is_nullable => 1, + bind_attributes => {testkey2 =>2}, }, ); __PACKAGE__->set_primary_key('artistid');