__PACKAGE__->mk_classdata('_cursor_class' => 'DBIx::Class::Cursor');
+sub iterator_class { shift->_cursor_class(@_) }
+
=head1 NAME
DBIx::Class::Table - Basic table methods
$class = ref $class if ref $class;
my $new = bless({ _column_data => { } }, $class);
if ($attrs) {
- die "attrs must be a hashref" unless ref($attrs) eq 'HASH';
+ $new->throw("attrs must be a hashref" ) unless ref($attrs) eq 'HASH';
while (my ($k, $v) = each %{$attrs}) {
$new->store_column($k => $v);
}
sub create {
my ($class, $attrs) = @_;
- die "create needs a hashref" unless ref $attrs eq 'HASH';
+ $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
return $class->new($attrs)->insert;
}
sub update {
my ($self) = @_;
- die "Not in database" unless $self->in_database;
+ $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->_ident_values );
$sth->finish;
if ($rows == 0) {
- die "Can't update $self: row not found";
+ $self->throw( "Can't update $self: row not found" );
} elsif ($rows > 1) {
- die "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 delete {
my $self = shift;
if (ref $self) {
- die "Not in database" unless $self->in_database;
+ $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);
sub get_column {
my ($self, $column) = @_;
- die "Can't fetch data as class method" unless ref $self;
- die "No such column '${column}'" unless $self->_columns->{$column};
+ $self->throw( "Can't fetch data as class method" ) unless ref $self;
+ $self->throw( "No such column '${column}'" ) unless $self->_columns->{$column};
return $self->{_column_data}{$column}
if exists $self->{_column_data}{$column};
return undef;
sub set_column {
my $self = shift;
my ($column) = @_;
+ my $old = $self->get_column($column);
my $ret = $self->store_column(@_);
- $self->{_dirty_columns}{$column} = 1;
+ $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
return $ret;
}
sub store_column {
my ($self, $column, $value) = @_;
- die "No such column '${column}'" unless $self->_columns->{$column};
- die "set_column called for ${column} without value" if @_ < 3;
+ $self->throw( "No such column '${column}'" )
+ unless $self->_columns->{$column};
+ $self->throw( "set_column called for ${column} without value" )
+ if @_ < 3;
return $self->{_column_data}{$column} = $value;
}
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);
+ return $class->sth_to_objects($sth, \@vals, \@cols, { where => $cond });
+}
+
+sub count {
+ my $class = shift;
+ my $attrs = { };
+ if (@_ > 1 && ref $_[$#_] eq 'HASH') {
+ $attrs = { %{ pop(@_) } };
+ }
+ my $query = ref $_[0] eq "HASH" ? shift: {@_};
+ my ($cond, @param) = $class->_cond_resolve($query, $attrs);
+ my $sth = $class->_get_sth( 'select', [ 'COUNT(*)' ],
+ $class->_table_name, $cond );
+ $sth->execute(@param);
+ my ($count) = $sth->fetchrow_array;
+ $sth->finish;
+ return $count;
}
sub sth_to_objects {
- my ($class, $sth, $args, $cols) = @_;
+ 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);
+ my $cursor = $cursor_class->new($class, $sth, \@args, \@cols, $attrs);
return (wantarray ? $cursor->all : $cursor);
}
return defined($exists) ? $exists : $class->create($hash);
}
+sub insert_or_update {
+ my $self = shift;
+ return ($self->in_database ? $self->update : $self->insert);
+}
+
sub retrieve_all {
my ($class) = @_;
return $class->retrieve_from_sql( '1' );
}
+sub is_changed {
+ return keys %{shift->{_dirty_columns} || {}};
+}
+
1;
=back