1 package DBIx::Class::Table;
6 use base qw/Class::Data::Inheritable DBIx::Class::SQL/;
8 __PACKAGE__->mk_classdata('_columns' => {});
10 __PACKAGE__->mk_classdata('_table_name');
12 __PACKAGE__->mk_classdata('table_alias'); # Doesn't actually do anything yet!
15 my ($class, $attrs) = @_;
16 $class = ref $class if ref $class;
17 my $new = bless({ _column_data => { } }, $class);
19 die "attrs must be a hashref" unless ref($attrs) eq 'HASH';
20 while (my ($k, $v) = each %{$attrs}) {
21 $new->store_column($k => $v);
29 return if $self->in_database;
30 #use Data::Dumper; warn Dumper($self);
31 my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
32 $self->_table_name, undef);
33 $sth->execute(values %{$self->{_column_data}});
35 $self->in_database(1);
36 $self->{_dirty_columns} = {};
41 my ($self, $val) = @_;
42 $self->{_in_database} = $val if @_ > 1;
43 return $self->{_in_database};
47 my ($class, $attrs) = @_;
48 die "create needs a hashref" unless ref $attrs eq 'HASH';
49 return $class->new($attrs)->insert;
54 die "Not in database" unless $self->in_database;
55 my @to_update = keys %{$self->{_dirty_columns} || {}};
56 return -1 unless @to_update;
57 my $sth = $self->_get_sth('update', \@to_update,
58 $self->_table_name, $self->_ident_cond);
59 my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
60 $self->_ident_values );
63 die "Can't update $self: row not found";
65 die "Can't update $self: updated more than one row";
67 $self->{_dirty_columns} = {};
74 die "Not in database" unless $self->in_database;
75 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
76 my $sth = $self->_get_sth('delete', undef,
77 $self->_table_name, $self->_ident_cond);
78 $sth->execute($self->_ident_values);
80 $self->in_database(undef);
83 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
84 $attrs = { %{ pop(@_) } };
86 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
87 my ($cond, @param) = $self->_cond_resolve($query, $attrs);
88 my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
89 $sth->execute(@param);
96 my ($self, $column) = @_;
97 die "Can't fetch data as class method" unless ref $self;
98 die "No such column '${column}'" unless $self->_columns->{$column};
99 return $self->{_column_data}{$column}
100 if exists $self->{_column_data}{$column};
107 my $ret = $self->store_column(@_);
108 $self->{_dirty_columns}{$column} = 1;
113 my ($self, $column, $value) = @_;
114 die "No such column '${column}'" unless $self->_columns->{$column};
115 die "set_column called for ${column} without value" if @_ < 3;
116 return $self->{_column_data}{$column} = $value;
119 sub _register_columns {
120 my ($class, @cols) = @_;
121 my $names = { %{$class->_columns} };
122 $names->{$_} ||= {} for @cols;
123 $class->_columns($names);
126 sub _mk_column_accessors {
127 my ($class, @cols) = @_;
128 $class->mk_group_accessors('column' => @cols);
132 my ($class, @cols) = @_;
133 $class->_register_columns(@cols);
134 $class->_mk_column_accessors(@cols);
137 sub retrieve_from_sql {
138 my ($class, $cond, @vals) = @_;
139 $cond =~ s/^\s*WHERE//i;
140 my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
141 my @cols = $class->_select_columns($attrs);
142 my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
144 return $class->sth_to_objects($sth, \@vals, \@cols);
148 my ($class, $sth, $args, $cols) = @_;
149 my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
150 $sth->execute(@$args);
152 while (my @row = $sth->fetchrow_array) {
153 push(@found, $class->_row_to_object(\@cols, \@row));
159 sub _row_to_object { # WARNING: Destructive to @$row
160 my ($class, $cols, $row) = @_;
161 my $new = $class->new;
162 $new->store_column($_, shift @$row) for @$cols;
163 $new->in_database(1);
170 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
171 $attrs = { %{ pop(@_) } };
173 my $query = ref $_[0] eq "HASH" ? shift: {@_};
174 my ($cond, @param) = $class->_cond_resolve($query, $attrs);
175 return $class->retrieve_from_sql($cond, @param, $attrs);
181 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
184 return $class->search(@_, { %$attrs, cmp => 'LIKE' });
187 sub _select_columns {
188 return keys %{$_[0]->_columns};
192 my ($self, $changes) = @_;
193 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
194 $new->set_column($_ => $changes->{$_}) for keys %$changes;
199 my ($self, $query, $attrs) = @_;
200 return '1 = 1' unless keys %$query;
201 my $op = $attrs->{'cmp'} || '=';
202 my $cond = join(' AND ',
203 map { (defined $query->{$_}
205 : (do { delete $query->{$_}; "$_ IS NULL"; }));
207 return ($cond, values %$query);
211 shift->_table_name(@_);