X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FTable.pm;h=29cc816b6a541172ccd1d42a3d997b4a77a8c401;hb=9b6e0845b81d5328b534cd8411639607fdc21685;hp=78a97bdb19101993d6e2c054ed42aab0041a0cfc;hpb=b8e1e21f0fcd55e6e3ce987e57601b279a75b666;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index 78a97bd..29cc816 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -9,6 +9,8 @@ __PACKAGE__->mk_classdata('_columns' => {}); __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; @@ -24,15 +26,23 @@ sub new { 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'; @@ -41,13 +51,14 @@ sub create { 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) { @@ -60,12 +71,13 @@ sub update { 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') { @@ -84,7 +96,9 @@ 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}; - 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 { @@ -123,7 +137,8 @@ sub add_columns { 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); @@ -135,14 +150,20 @@ sub sth_to_objects { $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 = { }; @@ -151,7 +172,7 @@ sub search { } 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 { @@ -176,6 +197,7 @@ sub copy { sub _cond_resolve { my ($self, $query, $attrs) = @_; + return '1 = 1' unless keys %$query; my $op = $attrs->{'cmp'} || '='; my $cond = join(' AND ', map { (defined $query->{$_}