use strict;
use warnings;
-use DBIx::Class::Cursor;
+use DBIx::Class::ResultSet;
use base qw/Class::Data::Inheritable/;
__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
-__PACKAGE__->mk_classdata('_cursor_class' => 'DBIx::Class::Cursor');
+__PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
-sub iterator_class { shift->_cursor_class(@_) }
+sub iterator_class { shift->_resultset_class(@_) }
=head1 NAME
my ($self) = @_;
return $self if $self->in_database;
#use Data::Dumper; warn Dumper($self);
- my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
- $self->_table_name, undef);
- $sth->execute(values %{$self->{_column_data}});
- $sth->finish;
+ my %in;
+ $in{$_} = $self->get_column($_)
+ for grep { defined $self->get_column($_) } $self->columns;
+ my %out = %{ $self->storage->insert($self->_table_name, \%in) };
+ $self->store_column($_, $out{$_})
+ for grep { $self->get_column($_) ne $out{$_} } keys %out;
$self->in_database(1);
$self->{_dirty_columns} = {};
return $self;
=cut
sub update {
- my ($self) = @_;
+ my ($self, $upd) = @_;
$self->throw( "Not in database" ) unless $self->in_database;
- my @to_update = keys %{$self->{_dirty_columns} || {}};
- return -1 unless @to_update;
- my $sth = $self->_get_sth('update', \@to_update,
- $self->_table_name, $self->_ident_cond);
- my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
- $self->_ident_values );
- $sth->finish;
+ my %to_update = %{$upd || {}};
+ $to_update{$_} = $self->get_column($_) for $self->is_changed;
+ return -1 unless keys %to_update;
+ my $rows = $self->storage->update($self->_table_name, \%to_update,
+ $self->ident_condition);
if ($rows == 0) {
- $self->throw( "Can't update $self: row not found" );
+ $self->throw( "Can't update ${self}: row not found" );
} elsif ($rows > 1) {
- $self->throw("Can't update $self: updated more than one row");
+ $self->throw("Can't update ${self}: updated more than one row");
}
$self->{_dirty_columns} = {};
return $self;
}
+sub ident_condition {
+ my ($self) = @_;
+ my %cond;
+ $cond{$_} = $self->get_column($_) for keys %{$self->_primaries};
+ return \%cond;
+}
+
=item delete
$obj->delete
if (ref $self) {
$self->throw( "Not in database" ) unless $self->in_database;
#warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
- my $sth = $self->_get_sth('delete', undef,
- $self->_table_name, $self->_ident_cond);
- $sth->execute($self->_ident_values);
- $sth->finish;
+ $self->storage->delete($self->_table_name, $self->ident_condition);
$self->in_database(undef);
+ #$self->store_column($_ => undef) for $self->primary_columns;
# Should probably also arrange to trash PK if auto
+ # but if we do, post-delete cascade triggers fail :/
} else {
my $attrs = { };
if (@_ > 1 && ref $_[$#_] eq 'HASH') {
$attrs = { %{ pop(@_) } };
}
my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
- my ($cond, @param) = $self->_cond_resolve($query, $attrs);
- my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
- $sth->execute(@param);
- $sth->finish;
+ $self->storage->delete($self->_table_name, $query);
}
return $self;
}
$class->_mk_column_accessors(@cols);
}
-=item retrieve_from_sql
+=item search_literal
- my @obj = $class->retrieve_from_sql($sql_where_cond, @bind);
- my $cursor = $class->retrieve_from_sql($sql_where_cond, @bind);
+ my @obj = $class->search_literal($literal_where_cond, @bind);
+ my $cursor = $class->search_literal($literal_where_cond, @bind);
=cut
-sub retrieve_from_sql {
+sub search_literal {
my ($class, $cond, @vals) = @_;
$cond =~ s/^\s*WHERE//i;
- my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
- my @cols = $class->_select_columns($attrs);
- my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
- #warn "$cond @vals";
- return $class->sth_to_objects($sth, \@vals, \@cols, { where => $cond });
+ my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
+ $attrs->{bind} = \@vals;
+ return $class->search(\$cond, $attrs);
}
-=item count_from_sql
+=item count_literal
- my $count = $class->count($sql_where_cond);
+ my $count = $class->count_literal($literal_where_cond);
=cut
-sub count_from_sql {
+sub count_literal {
my ($class, $cond, @vals) = @_;
$cond =~ s/^\s*WHERE//i;
my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
- my @cols = 'COUNT(*)';
- my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
- #warn "$cond @vals";
- $sth->execute(@vals);
- my ($count) = $sth->fetchrow_array;
- $sth->finish;
- return $count;
+ $attrs->{bind} = [ @vals ];
+ return $class->count($cond, $attrs);
}
=item count
if (@_ > 1 && ref $_[$#_] eq 'HASH') {
$attrs = { %{ pop(@_) } };
}
- my $query = ref $_[0] eq "HASH" ? shift: {@_};
- my ($cond, @param) = $class->_cond_resolve($query, $attrs);
- return $class->count_from_sql($cond, @param, $attrs);
+ my $query = ref $_[0] eq "HASH" || (@_ == 1) ? shift: {@_};
+ my @cols = 'COUNT(*)';
+ my $cursor = $class->storage->select($class->_table_name, \@cols,
+ $query, $attrs);
+ return ($cursor->next)[0];
}
-=item sth_to_objects
-
- my @obj = $class->sth_to_objects($sth, \@bind, \@columns, $attrs);
- my $cursor = $class->sth_to_objects($sth, \@bind, \@columns, $attrs);
-
-=cut
-
-sub sth_to_objects {
+sub cursor_to_resultset {
my ($class, $sth, $args, $cols, $attrs) = @_;
- my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
- my @args = map { ref $_ ? ''.$_ : $_ } @$args; # Stringify objects
- my $cursor_class = $class->_cursor_class;
- eval "use $cursor_class;";
- my $cursor = $cursor_class->new($class, $sth, \@args, \@cols, $attrs);
- return (wantarray ? $cursor->all : $cursor);
+ my $rs_class = $class->_resultset_class;
+ eval "use $rs_class;";
+ my $rs = $rs_class->new($class, $sth, $args, $cols, $attrs);
+ return (wantarray ? $rs->all : $rs);
}
sub _row_to_object { # WARNING: Destructive to @$row
sub search {
my $class = shift;
+ #warn "@_";
my $attrs = { };
if (@_ > 1 && ref $_[$#_] eq 'HASH') {
$attrs = { %{ pop(@_) } };
}
- my $query = ref $_[0] eq "HASH" ? shift: {@_};
- my ($cond, @param) = $class->_cond_resolve($query, $attrs);
- return $class->retrieve_from_sql($cond, @param, $attrs);
+ my $query = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
+ my @cols = $class->_select_columns;
+ return $class->cursor_to_resultset(undef, $attrs->{bind}, \@cols,
+ { where => $query, %$attrs });
}
=item search_like
if (@_ > 1 && ref $_[$#_] eq 'HASH') {
$attrs = pop(@_);
}
- return $class->search(@_, { %$attrs, cmp => 'LIKE' });
+ my $query = ref $_[0] eq "HASH" ? { %{shift()} }: {@_};
+ $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
+ return $class->search($query, { %$attrs });
}
sub _select_columns {
return ($self->in_database ? $self->update : $self->insert);
}
-=item retrieve_all
-
- my @all = $class->retrieve_all;
-
-=cut
-
-sub retrieve_all {
- my ($class) = @_;
- return $class->retrieve_from_sql( '1' );
-}
-
=item is_changed
my @changed_col_names = $obj->is_changed
return keys %{shift->{_dirty_columns} || {}};
}
+sub columns { return keys %{shift->_columns}; }
+
1;
=back