Added tests for the core APIs, refactored some
[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
9bc6db13 12__PACKAGE__->mk_classdata('table_alias'); # FIXME XXX
13
ea2e61bf 14sub new {
15 my ($class, $attrs) = @_;
16 $class = ref $class if ref $class;
17 my $new = bless({ _column_data => { } }, $class);
18 if ($attrs) {
8fe001e1 19 die "attrs must be a hashref" unless ref($attrs) eq 'HASH';
ea2e61bf 20 while (my ($k, $v) = each %{$attrs}) {
510ca912 21 $new->store_column($k => $v);
ea2e61bf 22 }
23 }
8fe001e1 24 return $new;
ea2e61bf 25}
26
27sub insert {
28 my ($self) = @_;
29 return if $self->{_in_database};
30 my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
31 $self->_table_name, undef);
32 $sth->execute(values %{$self->{_column_data}});
604d9f38 33 $sth->finish;
ea2e61bf 34 $self->{_in_database} = 1;
8fe001e1 35 $self->{_dirty_columns} = {};
ea2e61bf 36 return $self;
37}
38
604d9f38 39sub in_database {
40 return $_[0]->{_in_database};
41}
42
ea2e61bf 43sub create {
44 my ($class, $attrs) = @_;
8fe001e1 45 die "create needs a hashref" unless ref $attrs eq 'HASH';
ea2e61bf 46 return $class->new($attrs)->insert;
47}
48
49sub update {
50 my ($self) = @_;
51 die "Not in database" unless $self->{_in_database};
52 my @to_update = keys %{$self->{_dirty_columns} || {}};
a3018bd3 53 return -1 unless @to_update;
ea2e61bf 54 my $sth = $self->_get_sth('update', \@to_update,
55 $self->_table_name, $self->_ident_cond);
a3018bd3 56 my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
ea2e61bf 57 $self->_ident_values );
604d9f38 58 $sth->finish;
a3018bd3 59 if ($rows == 0) {
60 die "Can't update $self: row not found";
61 } elsif ($rows > 1) {
62 die "Can't update $self: updated more than one row";
63 }
ea2e61bf 64 $self->{_dirty_columns} = {};
65 return $self;
66}
67
68sub delete {
a3018bd3 69 my $self = shift;
70 if (ref $self) {
604d9f38 71 die "Not in database" unless $self->{_in_database};
b8e1e21f 72 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
a3018bd3 73 my $sth = $self->_get_sth('delete', undef,
74 $self->_table_name, $self->_ident_cond);
75 $sth->execute($self->_ident_values);
76 $sth->finish;
77 delete $self->{_in_database};
78 } else {
12bbb339 79 my $attrs = { };
80 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
81 $attrs = { %{ pop(@_) } };
82 }
a3018bd3 83 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
12bbb339 84 my ($cond, @param) = $self->_cond_resolve($query, $attrs);
a3018bd3 85 my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
12bbb339 86 $sth->execute(@param);
a3018bd3 87 $sth->finish;
88 }
ea2e61bf 89 return $self;
90}
91
510ca912 92sub get_column {
ea2e61bf 93 my ($self, $column) = @_;
8fe001e1 94 die "Can't fetch data as class method" unless ref $self;
510ca912 95 die "No such column '${column}'" unless $self->_columns->{$column};
a3018bd3 96 return $self->{_column_data}{$column} if $self->_columns->{$column};
ea2e61bf 97}
98
510ca912 99sub set_column {
100 my $self = shift;
101 my ($column) = @_;
102 my $ret = $self->store_column(@_);
103 $self->{_dirty_columns}{$column} = 1;
104 return $ret;
105}
106
107sub store_column {
ea2e61bf 108 my ($self, $column, $value) = @_;
510ca912 109 die "No such column '${column}'" unless $self->_columns->{$column};
110 die "set_column called for ${column} without value" if @_ < 3;
111 return $self->{_column_data}{$column} = $value;
ea2e61bf 112}
113
ea2e61bf 114sub _register_columns {
115 my ($class, @cols) = @_;
116 my $names = { %{$class->_columns} };
117 $names->{$_} ||= {} for @cols;
118 $class->_columns($names);
119}
120
121sub _mk_column_accessors {
122 my ($class, @cols) = @_;
510ca912 123 $class->mk_group_accessors('column' => @cols);
ea2e61bf 124}
125
510ca912 126sub add_columns {
8fe001e1 127 my ($class, @cols) = @_;
128 $class->_register_columns(@cols);
129 $class->_mk_column_accessors(@cols);
130}
131
132sub retrieve_from_sql {
133 my ($class, $cond, @vals) = @_;
a3018bd3 134 $cond =~ s/^\s*WHERE//i;
604d9f38 135 my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
136 my @cols = $class->_select_columns($attrs);
8fe001e1 137 my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
b8e1e21f 138 #warn "$cond @vals";
510ca912 139 return $class->sth_to_objects($sth, \@vals, \@cols);
140}
141
142sub sth_to_objects {
143 my ($class, $sth, $args, $cols) = @_;
144 my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
145 $sth->execute(@$args);
8fe001e1 146 my @found;
147 while (my @row = $sth->fetchrow_array) {
148 my $new = $class->new;
510ca912 149 $new->store_column($_, shift @row) for @cols;
8fe001e1 150 $new->{_in_database} = 1;
151 push(@found, $new);
152 }
604d9f38 153 $sth->finish;
8fe001e1 154 return @found;
155}
156
157sub search {
12bbb339 158 my $class = shift;
159 my $attrs = { };
160 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
161 $attrs = { %{ pop(@_) } };
162 }
163 my $query = ref $_[0] eq "HASH" ? shift: {@_};
164 my ($cond, @param) = $class->_cond_resolve($query, $attrs);
165 return $class->retrieve_from_sql($cond, @param);
a3018bd3 166}
167
168sub search_like {
169 my $class = shift;
12bbb339 170 my $attrs = { };
171 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
172 $attrs = pop(@_);
173 }
174 return $class->search(@_, { %$attrs, cmp => 'LIKE' });
8fe001e1 175}
176
177sub _select_columns {
178 return keys %{$_[0]->_columns};
179}
180
181sub copy {
182 my ($self, $changes) = @_;
183 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
510ca912 184 $new->set_column($_ => $changes->{$_}) for keys %$changes;
a3018bd3 185 return $new->insert;
186}
187
12bbb339 188sub _cond_resolve {
189 my ($self, $query, $attrs) = @_;
604d9f38 190 return '1 = 1' unless keys %$query;
12bbb339 191 my $op = $attrs->{'cmp'} || '=';
a3018bd3 192 my $cond = join(' AND ',
193 map { (defined $query->{$_}
194 ? "$_ $op ?"
195 : (do { delete $query->{$_}; "$_ IS NULL"; }));
196 } keys %$query);
12bbb339 197 return ($cond, values %$query);
8fe001e1 198}
199
510ca912 200sub table {
201 shift->_table_name(@_);
202}
203
ea2e61bf 2041;