First of a two-parter :)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Table.pm
CommitLineData
ea2e61bf 1package DBIx::Class::Table;
2
3use strict;
4use warnings;
5
510ca912 6use base qw/Class::Data::Inheritable DBIx::Class::SQL/;
ea2e61bf 7
8__PACKAGE__->mk_classdata('_columns' => {});
9
ea2e61bf 10__PACKAGE__->mk_classdata('_table_name');
11
12sub new {
13 my ($class, $attrs) = @_;
14 $class = ref $class if ref $class;
15 my $new = bless({ _column_data => { } }, $class);
16 if ($attrs) {
8fe001e1 17 die "attrs must be a hashref" unless ref($attrs) eq 'HASH';
ea2e61bf 18 while (my ($k, $v) = each %{$attrs}) {
510ca912 19 $new->store_column($k => $v);
ea2e61bf 20 }
21 }
8fe001e1 22 return $new;
ea2e61bf 23}
24
25sub insert {
26 my ($self) = @_;
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;
8fe001e1 32 $self->{_dirty_columns} = {};
ea2e61bf 33 return $self;
34}
35
36sub create {
37 my ($class, $attrs) = @_;
8fe001e1 38 die "create needs a hashref" unless ref $attrs eq 'HASH';
ea2e61bf 39 return $class->new($attrs)->insert;
40}
41
42sub update {
43 my ($self) = @_;
44 die "Not in database" unless $self->{_in_database};
45 my @to_update = keys %{$self->{_dirty_columns} || {}};
a3018bd3 46 return -1 unless @to_update;
ea2e61bf 47 my $sth = $self->_get_sth('update', \@to_update,
48 $self->_table_name, $self->_ident_cond);
a3018bd3 49 my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
ea2e61bf 50 $self->_ident_values );
a3018bd3 51 if ($rows == 0) {
52 die "Can't update $self: row not found";
53 } elsif ($rows > 1) {
54 die "Can't update $self: updated more than one row";
55 }
ea2e61bf 56 $self->{_dirty_columns} = {};
57 return $self;
58}
59
60sub delete {
a3018bd3 61 my $self = shift;
62 if (ref $self) {
63 my $sth = $self->_get_sth('delete', undef,
64 $self->_table_name, $self->_ident_cond);
65 $sth->execute($self->_ident_values);
66 $sth->finish;
67 delete $self->{_in_database};
68 } else {
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);
73 $sth->finish;
74 }
ea2e61bf 75 return $self;
76}
77
510ca912 78sub get_column {
ea2e61bf 79 my ($self, $column) = @_;
8fe001e1 80 die "Can't fetch data as class method" unless ref $self;
510ca912 81 die "No such column '${column}'" unless $self->_columns->{$column};
a3018bd3 82 return $self->{_column_data}{$column} if $self->_columns->{$column};
ea2e61bf 83}
84
510ca912 85sub set_column {
86 my $self = shift;
87 my ($column) = @_;
88 my $ret = $self->store_column(@_);
89 $self->{_dirty_columns}{$column} = 1;
90 return $ret;
91}
92
93sub store_column {
ea2e61bf 94 my ($self, $column, $value) = @_;
510ca912 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;
ea2e61bf 98}
99
ea2e61bf 100sub _register_columns {
101 my ($class, @cols) = @_;
102 my $names = { %{$class->_columns} };
103 $names->{$_} ||= {} for @cols;
104 $class->_columns($names);
105}
106
107sub _mk_column_accessors {
108 my ($class, @cols) = @_;
510ca912 109 $class->mk_group_accessors('column' => @cols);
ea2e61bf 110}
111
510ca912 112sub add_columns {
8fe001e1 113 my ($class, @cols) = @_;
114 $class->_register_columns(@cols);
115 $class->_mk_column_accessors(@cols);
116}
117
118sub retrieve_from_sql {
119 my ($class, $cond, @vals) = @_;
a3018bd3 120 $cond =~ s/^\s*WHERE//i;
8fe001e1 121 my @cols = $class->_select_columns;
122 my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
510ca912 123 return $class->sth_to_objects($sth, \@vals, \@cols);
124}
125
126sub sth_to_objects {
127 my ($class, $sth, $args, $cols) = @_;
128 my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
129 $sth->execute(@$args);
8fe001e1 130 my @found;
131 while (my @row = $sth->fetchrow_array) {
132 my $new = $class->new;
510ca912 133 $new->store_column($_, shift @row) for @cols;
8fe001e1 134 $new->{_in_database} = 1;
135 push(@found, $new);
136 }
137 return @found;
138}
139
140sub search {
141 my $class = shift;
142 my $where = ref $_[0] eq "HASH" ? shift: {@_};
a3018bd3 143 my ($cond, $param) = $class->_where_from_hash($where);
144 return $class->retrieve_from_sql($cond, @{$param});
145}
146
147sub search_like {
148 my $class = shift;
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});
8fe001e1 152}
153
154sub _select_columns {
155 return keys %{$_[0]->_columns};
156}
157
158sub copy {
159 my ($self, $changes) = @_;
160 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
510ca912 161 $new->set_column($_ => $changes->{$_}) for keys %$changes;
a3018bd3 162 return $new->insert;
163}
164
165sub _where_from_hash {
166 my ($self, $query, $opts) = @_;
167 my $op = $opts->{'cmp'} || '=';
168 my $cond = join(' AND ',
169 map { (defined $query->{$_}
170 ? "$_ $op ?"
171 : (do { delete $query->{$_}; "$_ IS NULL"; }));
172 } keys %$query);
173 return ($cond, [ values %$query ]);
8fe001e1 174}
175
510ca912 176sub table {
177 shift->_table_name(@_);
178}
179
ea2e61bf 1801;