From: Matt S Trout Date: Thu, 7 Jan 2010 23:22:45 +0000 (+0000) Subject: switch to hashref based for command args and results X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3347c67ee317a36b62bc02d2c6db2b9d06bc99c3;p=dbsrgits%2FDBIx-Data-Store-old.git switch to hashref based for command args and results --- diff --git a/lib/DBIx/Data/Collection/Set.pm b/lib/DBIx/Data/Collection/Set.pm index 1d772ce..9d54ccd 100644 --- a/lib/DBIx/Data/Collection/Set.pm +++ b/lib/DBIx/Data/Collection/Set.pm @@ -6,8 +6,6 @@ use Data::Perl::Stream::Array; has _store => (is => 'ro', required => 1, init_arg => 'store'); -has _column_order => (is => 'ro', required => 1, init_arg => 'column_order'); - has _class => (is => 'ro', predicate => '_has_class'); has _member_cache => (is => 'rw', lazy_build => 1); @@ -26,11 +24,8 @@ method _new_raw_stream { } method _inflate ($raw) { - my @order = @{$self->_column_order}; - my %final; - @final{@order} = @$raw; - bless(\%final, $self->_class) if $self->_has_class; - \%final; + bless($raw, $self->_class) if $self->_has_class; + $raw; } method flatten { diff --git a/lib/DBIx/Data/Store.pm b/lib/DBIx/Data/Store.pm index 5181bcf..e10bb0a 100644 --- a/lib/DBIx/Data/Store.pm +++ b/lib/DBIx/Data/Store.pm @@ -27,15 +27,16 @@ method new_call_command ($sql, $args) { ); } -method new_row_command ($sql, $args) { +method new_row_command ($sql, $args, $column_order) { return DBIx::Data::Store::Command::Row->new( run => $sql, with => $args, against => $self->connection ); } -method new_stream_command ($sql, $args) { +method new_stream_command ($sql, $args, $column_order) { return DBIx::Data::Store::Command::Stream->new( - run => $sql, with => $args, against => $self->connection + run => $sql, with => $args, against => $self->connection, + column_order => $column_order ); } diff --git a/lib/DBIx/Data/Store/CRUD.pm b/lib/DBIx/Data/Store/CRUD.pm index f6d4d3f..5ba2e89 100644 --- a/lib/DBIx/Data/Store/CRUD.pm +++ b/lib/DBIx/Data/Store/CRUD.pm @@ -7,26 +7,43 @@ has raw_store => (is => 'ro', required => 1); # DBIx::Data::Store object foreach my $type (qw(select insert update delete)) { has "${type}_sql" => (is => 'ro', predicate => "has_${type}_sql"); + has "${type}_argument_order" => (is => 'ro', default => sub { [] }); } +has "select_column_order" => (is => 'ro'); + method new_select_command ($args) { die "$self->has_select_sql" unless $self->has_select_sql; - $self->raw_store->new_stream_command($self->select_sql, $args); + $self->raw_store->new_stream_command( + $self->select_sql, + $self->_unwrap_args_for(select => $args), + $self->select_column_order + ); +} + +method _unwrap_args_for ($type, $args) { + [ @{$args}{@{$self->${\"${type}_argument_order"}}} ] +} + +method _new_call_command ($type, $args) { + my $has_meth = "has_${type}_sql"; + die "${self}->${has_meth}" unless $self->$has_meth; + $self->raw_store->new_call_command( + $self->${\"${type}_sql"}, + $self->_unwrap_args_for($type => $args) + ); } method new_insert_command ($args) { - die "$self->has_insert_sql" unless $self->has_insert_sql; - $self->raw_store->new_call_command($self->insert_sql, $args); + $self->_new_call_command(insert => $args); } method new_update_command ($args) { - die "$self->has_update_sql" unless $self->has_update_sql; - $self->raw_store->new_call_command($self->update_sql, $args); + $self->_new_call_command(update => $args); } method new_delete_command ($args) { - die "$self->has_delete_sql" unless $self->has_delete_sql; - $self->raw_store->new_call_command($self->delete_sql, $args); + $self->_new_call_command(delete => $args); } __PACKAGE__->meta->make_immutable; diff --git a/lib/DBIx/Data/Store/Command/Row.pm b/lib/DBIx/Data/Store/Command/Row.pm index 8ae3749..402b616 100644 --- a/lib/DBIx/Data/Store/Command/Row.pm +++ b/lib/DBIx/Data/Store/Command/Row.pm @@ -4,16 +4,16 @@ use Moose; use Carp qw(carp); use Method::Signatures::Simple; +has 'column_order' => (is => 'ro', required => 1); + method execute { - my $sth = $self->_new_active_sth; - my @row = $sth->fetchrow_array; - my @nextrow = $sth->fetchrow_array if @row; - if(@row && @nextrow) { + my $result = (my $stream = DBIx::Data::Stream::STH->new( + sth => $self->_new_active_sth, column_order => $self->column_order, + ))->next; + if ($stream->next) { carp "Query returned more than one row - did you want a stream command?"; } - # Need to call finish() to work round broken DBDs - $sth->finish(); - return \@row; + return $result; } with 'DBIx::Data::Store::Command'; diff --git a/lib/DBIx/Data/Store/Command/Stream.pm b/lib/DBIx/Data/Store/Command/Stream.pm index 96323e6..e0b0e1d 100644 --- a/lib/DBIx/Data/Store/Command/Stream.pm +++ b/lib/DBIx/Data/Store/Command/Stream.pm @@ -4,10 +4,11 @@ use Moose; use Method::Signatures::Simple; use DBIx::Data::Stream::STH; +has 'column_order' => (is => 'ro', required => 1); + method execute { - my $sth = $self->_new_active_sth; DBIx::Data::Stream::STH->new( - sth => $sth + sth => $self->_new_active_sth, column_order => $self->column_order, ); } diff --git a/lib/DBIx/Data/Stream/STH.pm b/lib/DBIx/Data/Stream/STH.pm index 7a5e162..eb504ef 100644 --- a/lib/DBIx/Data/Stream/STH.pm +++ b/lib/DBIx/Data/Stream/STH.pm @@ -5,12 +5,15 @@ use Method::Signatures::Simple; has 'sth' => (is => 'ro', required => 1, clearer => '_clear_sth'); +has '_column_order' => (is => 'ro', required => 1, init_arg => 'column_order'); + method next { my $sth = $self->sth; return unless $sth; # {Active} only means that there *may* be more results to fetch if ($sth->{Active} and my @next = $self->sth->fetchrow_array) { - return \@next; + my %next; @next{@{$self->_column_order}} = @next; + return \%next; } $sth->finish; # prepare_cached might recycle it now we're finished so get rid of it diff --git a/t/01basic_collection.t b/t/01basic_collection.t index 5b68360..5b57064 100644 --- a/t/01basic_collection.t +++ b/t/01basic_collection.t @@ -31,8 +31,8 @@ sub make_set { store => DBIx::Data::Store::CRUD->new( raw_store => DBIx::Data::Store->connect($dsn), select_sql => q{SELECT id, name FROM person}, + select_column_order => [ qw(id name) ], ), - column_order => [ qw(id name) ], ); }