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');
13 my ($class, $attrs) = @_;
14 $class = ref $class if ref $class;
15 my $new = bless({ _column_data => { } }, $class);
17 die "attrs must be a hashref" unless ref($attrs) eq 'HASH';
18 while (my ($k, $v) = each %{$attrs}) {
19 $new->store_column($k => $v);
27 return if $self->{_in_database};
28 my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
29 $self->_table_name, undef);
30 $sth->execute(values %{$self->{_column_data}});
31 $self->{_in_database} = 1;
32 $self->{_dirty_columns} = {};
37 my ($class, $attrs) = @_;
38 die "create needs a hashref" unless ref $attrs eq 'HASH';
39 return $class->new($attrs)->insert;
44 die "Not in database" unless $self->{_in_database};
45 my @to_update = keys %{$self->{_dirty_columns} || {}};
46 return -1 unless @to_update;
47 my $sth = $self->_get_sth('update', \@to_update,
48 $self->_table_name, $self->_ident_cond);
49 my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
50 $self->_ident_values );
52 die "Can't update $self: row not found";
54 die "Can't update $self: updated more than one row";
56 $self->{_dirty_columns} = {};
63 my $sth = $self->_get_sth('delete', undef,
64 $self->_table_name, $self->_ident_cond);
65 $sth->execute($self->_ident_values);
67 delete $self->{_in_database};
69 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
70 my ($cond, $param) = $self->_where_from_hash($query);
71 my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
72 $sth->execute(@$param);
79 my ($self, $column) = @_;
80 die "Can't fetch data as class method" unless ref $self;
81 die "No such column '${column}'" unless $self->_columns->{$column};
82 return $self->{_column_data}{$column} if $self->_columns->{$column};
88 my $ret = $self->store_column(@_);
89 $self->{_dirty_columns}{$column} = 1;
94 my ($self, $column, $value) = @_;
95 die "No such column '${column}'" unless $self->_columns->{$column};
96 die "set_column called for ${column} without value" if @_ < 3;
97 return $self->{_column_data}{$column} = $value;
100 sub _register_columns {
101 my ($class, @cols) = @_;
102 my $names = { %{$class->_columns} };
103 $names->{$_} ||= {} for @cols;
104 $class->_columns($names);
107 sub _mk_column_accessors {
108 my ($class, @cols) = @_;
109 $class->mk_group_accessors('column' => @cols);
113 my ($class, @cols) = @_;
114 $class->_register_columns(@cols);
115 $class->_mk_column_accessors(@cols);
118 sub retrieve_from_sql {
119 my ($class, $cond, @vals) = @_;
120 $cond =~ s/^\s*WHERE//i;
121 my @cols = $class->_select_columns;
122 my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
123 return $class->sth_to_objects($sth, \@vals, \@cols);
127 my ($class, $sth, $args, $cols) = @_;
128 my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
129 $sth->execute(@$args);
131 while (my @row = $sth->fetchrow_array) {
132 my $new = $class->new;
133 $new->store_column($_, shift @row) for @cols;
134 $new->{_in_database} = 1;
142 my $where = ref $_[0] eq "HASH" ? shift: {@_};
143 my ($cond, $param) = $class->_where_from_hash($where);
144 return $class->retrieve_from_sql($cond, @{$param});
149 my $where = ref $_[0] eq "HASH" ? shift: {@_};
150 my ($cond, $param) = $class->_where_from_hash($where, { cmp => 'like' });
151 return $class->retrieve_from_sql($cond, @{$param});
154 sub _select_columns {
155 return keys %{$_[0]->_columns};
159 my ($self, $changes) = @_;
160 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
161 $new->set_column($_ => $changes->{$_}) for keys %$changes;
165 sub _where_from_hash {
166 my ($self, $query, $opts) = @_;
167 my $op = $opts->{'cmp'} || '=';
168 my $cond = join(' AND ',
169 map { (defined $query->{$_}
171 : (do { delete $query->{$_}; "$_ IS NULL"; }));
173 return ($cond, [ values %$query ]);
177 shift->_table_name(@_);