1 package DBIx::Class::Table;
6 use base qw/Class::Data::Inheritable Class::Accessor 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}) {
22 $new->{_dirty_columns} = {};
28 return if $self->{_in_database};
29 my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
30 $self->_table_name, undef);
31 $sth->execute(values %{$self->{_column_data}});
32 $self->{_in_database} = 1;
33 $self->{_dirty_columns} = {};
38 my ($class, $attrs) = @_;
39 die "create needs a hashref" unless ref $attrs eq 'HASH';
40 return $class->new($attrs)->insert;
45 die "Not in database" unless $self->{_in_database};
46 my @to_update = keys %{$self->{_dirty_columns} || {}};
47 return -1 unless @to_update;
48 my $sth = $self->_get_sth('update', \@to_update,
49 $self->_table_name, $self->_ident_cond);
50 my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
51 $self->_ident_values );
53 die "Can't update $self: row not found";
55 die "Can't update $self: updated more than one row";
57 $self->{_dirty_columns} = {};
64 my $sth = $self->_get_sth('delete', undef,
65 $self->_table_name, $self->_ident_cond);
66 $sth->execute($self->_ident_values);
68 delete $self->{_in_database};
70 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
71 my ($cond, $param) = $self->_where_from_hash($query);
72 my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
73 $sth->execute(@$param);
81 $_[0] = $self->retrieve($self->id);
85 my ($self, $column) = @_;
86 die "Can't fetch data as class method" unless ref $self;
87 #die "No such column '${column}'" unless $self->_columns->{$column};
88 return $self->{_column_data}{$column} if $self->_columns->{$column};
89 return shift->SUPER::get(@_);
93 my ($self, $column, $value) = @_;
94 #die "No such column '${column}'" unless $self->_columns->{$column};
95 #die "set_column called for ${column} without value" if @_ < 3;
96 if ($self->_columns->{$column}) {
97 $self->{_dirty_columns}{$column} = 1;
98 return $self->{_column_data}{$column} = $value;
100 return shift->SUPER::set(@_);
103 sub _register_columns {
104 my ($class, @cols) = @_;
105 my $names = { %{$class->_columns} };
106 $names->{$_} ||= {} for @cols;
107 $class->_columns($names);
110 sub _mk_column_accessors {
111 my ($class, @cols) = @_;
112 $class->mk_accessors(@cols);
116 my ($class, @cols) = @_;
117 $class->_register_columns(@cols);
118 $class->_mk_column_accessors(@cols);
121 sub retrieve_from_sql {
122 my ($class, $cond, @vals) = @_;
123 $cond =~ s/^\s*WHERE//i;
124 my @cols = $class->_select_columns;
125 my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
126 $sth->execute(@vals);
128 while (my @row = $sth->fetchrow_array) {
129 my $new = $class->new;
130 $new->set($_, shift @row) for @cols;
131 $new->{_in_database} = 1;
132 $new->{_dirty_columns} = {};
140 my $where = ref $_[0] eq "HASH" ? shift: {@_};
141 my ($cond, $param) = $class->_where_from_hash($where);
142 return $class->retrieve_from_sql($cond, @{$param});
147 my $where = ref $_[0] eq "HASH" ? shift: {@_};
148 my ($cond, $param) = $class->_where_from_hash($where, { cmp => 'like' });
149 return $class->retrieve_from_sql($cond, @{$param});
152 sub _select_columns {
153 return keys %{$_[0]->_columns};
157 my ($self, $changes) = @_;
158 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
159 $new->set($_ => $changes->{$_}) for keys %$changes;
163 sub _where_from_hash {
164 my ($self, $query, $opts) = @_;
165 my $op = $opts->{'cmp'} || '=';
166 my $cond = join(' AND ',
167 map { (defined $query->{$_}
169 : (do { delete $query->{$_}; "$_ IS NULL"; }));
171 return ($cond, [ values %$query ]);