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');
18 sub iterator_class { shift->_cursor_class(@_) }
22 DBIx::Class::Table - Basic table methods
28 This class is responsible for defining and doing basic operations on
29 L<DBIx::Class> objects.
38 my ($class, $attrs) = @_;
39 $class = ref $class if ref $class;
40 my $new = bless({ _column_data => { } }, $class);
42 $new->throw("attrs must be a hashref" ) unless ref($attrs) eq 'HASH';
43 while (my ($k, $v) = each %{$attrs}) {
44 $new->store_column($k => $v);
52 return if $self->in_database;
53 #use Data::Dumper; warn Dumper($self);
54 my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
55 $self->_table_name, undef);
56 $sth->execute(values %{$self->{_column_data}});
58 $self->in_database(1);
59 $self->{_dirty_columns} = {};
64 my ($self, $val) = @_;
65 $self->{_in_database} = $val if @_ > 1;
66 return $self->{_in_database};
70 my ($class, $attrs) = @_;
71 $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
72 return $class->new($attrs)->insert;
77 $self->throw( "Not in database" ) unless $self->in_database;
78 my @to_update = keys %{$self->{_dirty_columns} || {}};
79 return -1 unless @to_update;
80 my $sth = $self->_get_sth('update', \@to_update,
81 $self->_table_name, $self->_ident_cond);
82 my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
83 $self->_ident_values );
86 $self->throw( "Can't update $self: row not found" );
88 $self->throw("Can't update $self: updated more than one row");
90 $self->{_dirty_columns} = {};
97 $self->throw( "Not in database" ) unless $self->in_database;
98 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
99 my $sth = $self->_get_sth('delete', undef,
100 $self->_table_name, $self->_ident_cond);
101 $sth->execute($self->_ident_values);
103 $self->in_database(undef);
106 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
107 $attrs = { %{ pop(@_) } };
109 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
110 my ($cond, @param) = $self->_cond_resolve($query, $attrs);
111 my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
112 $sth->execute(@param);
119 my ($self, $column) = @_;
120 $self->throw( "Can't fetch data as class method" ) unless ref $self;
121 $self->throw( "No such column '${column}'" ) unless $self->_columns->{$column};
122 return $self->{_column_data}{$column}
123 if exists $self->{_column_data}{$column};
130 my $ret = $self->store_column(@_);
131 $self->{_dirty_columns}{$column} = 1;
136 my ($self, $column, $value) = @_;
137 $self->throw( "No such column '${column}'" )
138 unless $self->_columns->{$column};
139 $self->throw( "set_column called for ${column} without value" )
141 return $self->{_column_data}{$column} = $value;
144 sub _register_columns {
145 my ($class, @cols) = @_;
146 my $names = { %{$class->_columns} };
147 $names->{$_} ||= {} for @cols;
148 $class->_columns($names);
151 sub _mk_column_accessors {
152 my ($class, @cols) = @_;
153 $class->mk_group_accessors('column' => @cols);
157 my ($class, @cols) = @_;
158 $class->_register_columns(@cols);
159 $class->_mk_column_accessors(@cols);
162 sub retrieve_from_sql {
163 my ($class, $cond, @vals) = @_;
164 $cond =~ s/^\s*WHERE//i;
165 my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
166 my @cols = $class->_select_columns($attrs);
167 my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
169 return $class->sth_to_objects($sth, \@vals, \@cols, { where => $cond });
173 my ($class, $sth, $args, $cols, $attrs) = @_;
174 my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
175 my @args = map { ref $_ ? ''.$_ : $_ } @$args; # Stringify objects
176 my $cursor_class = $class->_cursor_class;
177 eval "use $cursor_class;";
178 my $cursor = $cursor_class->new($class, $sth, \@args, \@cols, $attrs);
179 return (wantarray ? $cursor->all : $cursor);
182 sub _row_to_object { # WARNING: Destructive to @$row
183 my ($class, $cols, $row) = @_;
184 my $new = $class->new;
185 $new->store_column($_, shift @$row) for @$cols;
186 $new->in_database(1);
193 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
194 $attrs = { %{ pop(@_) } };
196 my $query = ref $_[0] eq "HASH" ? shift: {@_};
197 my ($cond, @param) = $class->_cond_resolve($query, $attrs);
198 return $class->retrieve_from_sql($cond, @param, $attrs);
204 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
207 return $class->search(@_, { %$attrs, cmp => 'LIKE' });
210 sub _select_columns {
211 return keys %{$_[0]->_columns};
215 my ($self, $changes) = @_;
216 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
217 $new->set_column($_ => $changes->{$_}) for keys %$changes;
222 my ($self, $query, $attrs) = @_;
223 return '1 = 1' unless keys %$query;
224 my $op = $attrs->{'cmp'} || '=';
225 my $cond = join(' AND ',
226 map { (defined $query->{$_}
228 : (do { delete $query->{$_}; "$_ IS NULL"; }));
230 return ($cond, values %$query);
234 shift->_table_name(@_);
239 my $hash = ref $_[0] eq "HASH" ? shift: {@_};
240 my ($exists) = $class->search($hash);
241 return defined($exists) ? $exists : $class->create($hash);
244 sub insert_or_update {
246 return ($self->in_database ? $self->update : $self->insert);
251 return $class->retrieve_from_sql( '1' );
260 Matt S. Trout <perl-stuff@trout.me.uk>
264 You may distribute this code under the same terms as Perl itself.