__PACKAGE__->mk_classdata('_table_name');
+__PACKAGE__->mk_classdata('table_alias'); # Doesn't actually do anything yet!
+
sub new {
my ($class, $attrs) = @_;
$class = ref $class if ref $class;
sub insert {
my ($self) = @_;
- return if $self->{_in_database};
+ return 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}});
- $self->{_in_database} = 1;
+ $sth->finish;
+ $self->in_database(1);
$self->{_dirty_columns} = {};
return $self;
}
+sub in_database {
+ my ($self, $val) = @_;
+ $self->{_in_database} = $val if @_ > 1;
+ return $self->{_in_database};
+}
+
sub create {
my ($class, $attrs) = @_;
die "create needs a hashref" unless ref $attrs eq 'HASH';
sub update {
my ($self) = @_;
- die "Not in database" unless $self->{_in_database};
+ die "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;
if ($rows == 0) {
die "Can't update $self: row not found";
} elsif ($rows > 1) {
sub delete {
my $self = shift;
if (ref $self) {
+ die "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;
- delete $self->{_in_database};
+ $self->in_database(undef);
} else {
my $attrs = { };
if (@_ > 1 && ref $_[$#_] eq 'HASH') {
my ($self, $column) = @_;
die "Can't fetch data as class method" unless ref $self;
die "No such column '${column}'" unless $self->_columns->{$column};
- return $self->{_column_data}{$column} if $self->_columns->{$column};
+ return $self->{_column_data}{$column}
+ if exists $self->{_column_data}{$column};
+ return undef;
}
sub set_column {
sub retrieve_from_sql {
my ($class, $cond, @vals) = @_;
$cond =~ s/^\s*WHERE//i;
- my @cols = $class->_select_columns;
+ 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);
$sth->execute(@$args);
my @found;
while (my @row = $sth->fetchrow_array) {
- my $new = $class->new;
- $new->store_column($_, shift @row) for @cols;
- $new->{_in_database} = 1;
- push(@found, $new);
+ push(@found, $class->_row_to_object(\@cols, \@row));
}
+ $sth->finish;
return @found;
}
+sub _row_to_object { # WARNING: Destructive to @$row
+ my ($class, $cols, $row) = @_;
+ my $new = $class->new;
+ $new->store_column($_, shift @$row) for @$cols;
+ $new->in_database(1);
+ return $new;
+}
+
sub search {
my $class = shift;
my $attrs = { };
}
my $query = ref $_[0] eq "HASH" ? shift: {@_};
my ($cond, @param) = $class->_cond_resolve($query, $attrs);
- return $class->retrieve_from_sql($cond, @param);
+ return $class->retrieve_from_sql($cond, @param, $attrs);
}
sub search_like {
sub _cond_resolve {
my ($self, $query, $attrs) = @_;
+ return '1 = 1' unless keys %$query;
my $op = $attrs->{'cmp'} || '=';
my $cond = join(' AND ',
map { (defined $query->{$_}