1 package DBIx::Class::Table;
6 use DBIx::Class::Cursor;
8 use base qw/Class::Data::Inheritable/;
10 __PACKAGE__->mk_classdata('_columns' => {});
12 __PACKAGE__->mk_classdata('_table_name');
14 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
16 __PACKAGE__->mk_classdata('_cursor_class' => 'DBIx::Class::Cursor');
20 DBIx::Class::Table - Basic table methods
26 This class is responsible for defining and doing basic operations on
27 L<DBIx::Class> objects.
36 my ($class, $attrs) = @_;
37 $class = ref $class if ref $class;
38 my $new = bless({ _column_data => { } }, $class);
40 die "attrs must be a hashref" unless ref($attrs) eq 'HASH';
41 while (my ($k, $v) = each %{$attrs}) {
42 $new->store_column($k => $v);
50 return if $self->in_database;
51 #use Data::Dumper; warn Dumper($self);
52 my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
53 $self->_table_name, undef);
54 $sth->execute(values %{$self->{_column_data}});
56 $self->in_database(1);
57 $self->{_dirty_columns} = {};
62 my ($self, $val) = @_;
63 $self->{_in_database} = $val if @_ > 1;
64 return $self->{_in_database};
68 my ($class, $attrs) = @_;
69 die "create needs a hashref" unless ref $attrs eq 'HASH';
70 return $class->new($attrs)->insert;
75 die "Not in database" unless $self->in_database;
76 my @to_update = keys %{$self->{_dirty_columns} || {}};
77 return -1 unless @to_update;
78 my $sth = $self->_get_sth('update', \@to_update,
79 $self->_table_name, $self->_ident_cond);
80 my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
81 $self->_ident_values );
84 die "Can't update $self: row not found";
86 die "Can't update $self: updated more than one row";
88 $self->{_dirty_columns} = {};
95 die "Not in database" unless $self->in_database;
96 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
97 my $sth = $self->_get_sth('delete', undef,
98 $self->_table_name, $self->_ident_cond);
99 $sth->execute($self->_ident_values);
101 $self->in_database(undef);
104 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
105 $attrs = { %{ pop(@_) } };
107 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
108 my ($cond, @param) = $self->_cond_resolve($query, $attrs);
109 my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
110 $sth->execute(@param);
117 my ($self, $column) = @_;
118 die "Can't fetch data as class method" unless ref $self;
119 die "No such column '${column}'" unless $self->_columns->{$column};
120 return $self->{_column_data}{$column}
121 if exists $self->{_column_data}{$column};
128 my $ret = $self->store_column(@_);
129 $self->{_dirty_columns}{$column} = 1;
134 my ($self, $column, $value) = @_;
135 die "No such column '${column}'" unless $self->_columns->{$column};
136 die "set_column called for ${column} without value" if @_ < 3;
137 return $self->{_column_data}{$column} = $value;
140 sub _register_columns {
141 my ($class, @cols) = @_;
142 my $names = { %{$class->_columns} };
143 $names->{$_} ||= {} for @cols;
144 $class->_columns($names);
147 sub _mk_column_accessors {
148 my ($class, @cols) = @_;
149 $class->mk_group_accessors('column' => @cols);
153 my ($class, @cols) = @_;
154 $class->_register_columns(@cols);
155 $class->_mk_column_accessors(@cols);
158 sub retrieve_from_sql {
159 my ($class, $cond, @vals) = @_;
160 $cond =~ s/^\s*WHERE//i;
161 my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
162 my @cols = $class->_select_columns($attrs);
163 my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
165 return $class->sth_to_objects($sth, \@vals, \@cols);
169 my ($class, $sth, $args, $cols) = @_;
170 my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
171 my $cursor_class = $class->_cursor_class;
172 eval "use $cursor_class;";
173 my $cursor = $cursor_class->new($class, $sth, $args, \@cols);
174 return (wantarray ? $cursor->all : $cursor);
177 sub _row_to_object { # WARNING: Destructive to @$row
178 my ($class, $cols, $row) = @_;
179 my $new = $class->new;
180 $new->store_column($_, shift @$row) for @$cols;
181 $new->in_database(1);
188 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
189 $attrs = { %{ pop(@_) } };
191 my $query = ref $_[0] eq "HASH" ? shift: {@_};
192 my ($cond, @param) = $class->_cond_resolve($query, $attrs);
193 return $class->retrieve_from_sql($cond, @param, $attrs);
199 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
202 return $class->search(@_, { %$attrs, cmp => 'LIKE' });
205 sub _select_columns {
206 return keys %{$_[0]->_columns};
210 my ($self, $changes) = @_;
211 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
212 $new->set_column($_ => $changes->{$_}) for keys %$changes;
217 my ($self, $query, $attrs) = @_;
218 return '1 = 1' unless keys %$query;
219 my $op = $attrs->{'cmp'} || '=';
220 my $cond = join(' AND ',
221 map { (defined $query->{$_}
223 : (do { delete $query->{$_}; "$_ IS NULL"; }));
225 return ($cond, values %$query);
229 shift->_table_name(@_);
234 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
235 my ($exists) = $class->search($hash);
236 return defined($exists) ? $exists : $class->create($hash);
241 return $class->retrieve_from_sql( '1' );
250 Matt S. Trout <perl-stuff@trout.me.uk>
254 You may distribute this code under the same terms as Perl itself.