Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class::Table; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
510ca912 |
6 | use base qw/Class::Data::Inheritable DBIx::Class::SQL/; |
ea2e61bf |
7 | |
8 | __PACKAGE__->mk_classdata('_columns' => {}); |
9 | |
ea2e61bf |
10 | __PACKAGE__->mk_classdata('_table_name'); |
11 | |
34d52be2 |
12 | __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet! |
13 | |
14 | =head1 NAME |
15 | |
16 | DBIx::Class::Table - Basic table methods |
17 | |
18 | =head1 SYNOPSIS |
19 | |
20 | =head1 DESCRIPTION |
21 | |
22 | This class is responsible for defining and doing basic operations on |
23 | L<DBIx::Class> objects. |
24 | |
25 | =head1 METHODS |
26 | |
27 | =over 4 |
28 | |
29 | =cut |
9bc6db13 |
30 | |
ea2e61bf |
31 | sub new { |
32 | my ($class, $attrs) = @_; |
33 | $class = ref $class if ref $class; |
34 | my $new = bless({ _column_data => { } }, $class); |
35 | if ($attrs) { |
8fe001e1 |
36 | die "attrs must be a hashref" unless ref($attrs) eq 'HASH'; |
ea2e61bf |
37 | while (my ($k, $v) = each %{$attrs}) { |
510ca912 |
38 | $new->store_column($k => $v); |
ea2e61bf |
39 | } |
40 | } |
8fe001e1 |
41 | return $new; |
ea2e61bf |
42 | } |
43 | |
44 | sub insert { |
45 | my ($self) = @_; |
c687b87e |
46 | return if $self->in_database; |
c1d23573 |
47 | #use Data::Dumper; warn Dumper($self); |
ea2e61bf |
48 | my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ], |
49 | $self->_table_name, undef); |
50 | $sth->execute(values %{$self->{_column_data}}); |
604d9f38 |
51 | $sth->finish; |
c687b87e |
52 | $self->in_database(1); |
8fe001e1 |
53 | $self->{_dirty_columns} = {}; |
ea2e61bf |
54 | return $self; |
55 | } |
56 | |
604d9f38 |
57 | sub in_database { |
c687b87e |
58 | my ($self, $val) = @_; |
59 | $self->{_in_database} = $val if @_ > 1; |
60 | return $self->{_in_database}; |
604d9f38 |
61 | } |
62 | |
ea2e61bf |
63 | sub create { |
64 | my ($class, $attrs) = @_; |
8fe001e1 |
65 | die "create needs a hashref" unless ref $attrs eq 'HASH'; |
ea2e61bf |
66 | return $class->new($attrs)->insert; |
67 | } |
68 | |
69 | sub update { |
70 | my ($self) = @_; |
c687b87e |
71 | die "Not in database" unless $self->in_database; |
ea2e61bf |
72 | my @to_update = keys %{$self->{_dirty_columns} || {}}; |
a3018bd3 |
73 | return -1 unless @to_update; |
ea2e61bf |
74 | my $sth = $self->_get_sth('update', \@to_update, |
75 | $self->_table_name, $self->_ident_cond); |
a3018bd3 |
76 | my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update), |
ea2e61bf |
77 | $self->_ident_values ); |
604d9f38 |
78 | $sth->finish; |
a3018bd3 |
79 | if ($rows == 0) { |
80 | die "Can't update $self: row not found"; |
81 | } elsif ($rows > 1) { |
82 | die "Can't update $self: updated more than one row"; |
83 | } |
ea2e61bf |
84 | $self->{_dirty_columns} = {}; |
85 | return $self; |
86 | } |
87 | |
88 | sub delete { |
a3018bd3 |
89 | my $self = shift; |
90 | if (ref $self) { |
c687b87e |
91 | die "Not in database" unless $self->in_database; |
b8e1e21f |
92 | #warn $self->_ident_cond.' '.join(', ', $self->_ident_values); |
a3018bd3 |
93 | my $sth = $self->_get_sth('delete', undef, |
94 | $self->_table_name, $self->_ident_cond); |
95 | $sth->execute($self->_ident_values); |
96 | $sth->finish; |
c687b87e |
97 | $self->in_database(undef); |
a3018bd3 |
98 | } else { |
12bbb339 |
99 | my $attrs = { }; |
100 | if (@_ > 1 && ref $_[$#_] eq 'HASH') { |
101 | $attrs = { %{ pop(@_) } }; |
102 | } |
a3018bd3 |
103 | my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_}); |
12bbb339 |
104 | my ($cond, @param) = $self->_cond_resolve($query, $attrs); |
a3018bd3 |
105 | my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond); |
12bbb339 |
106 | $sth->execute(@param); |
a3018bd3 |
107 | $sth->finish; |
108 | } |
ea2e61bf |
109 | return $self; |
110 | } |
111 | |
510ca912 |
112 | sub get_column { |
ea2e61bf |
113 | my ($self, $column) = @_; |
8fe001e1 |
114 | die "Can't fetch data as class method" unless ref $self; |
510ca912 |
115 | die "No such column '${column}'" unless $self->_columns->{$column}; |
c1d23573 |
116 | return $self->{_column_data}{$column} |
117 | if exists $self->{_column_data}{$column}; |
118 | return undef; |
ea2e61bf |
119 | } |
120 | |
510ca912 |
121 | sub set_column { |
122 | my $self = shift; |
123 | my ($column) = @_; |
124 | my $ret = $self->store_column(@_); |
125 | $self->{_dirty_columns}{$column} = 1; |
126 | return $ret; |
127 | } |
128 | |
129 | sub store_column { |
ea2e61bf |
130 | my ($self, $column, $value) = @_; |
510ca912 |
131 | die "No such column '${column}'" unless $self->_columns->{$column}; |
132 | die "set_column called for ${column} without value" if @_ < 3; |
133 | return $self->{_column_data}{$column} = $value; |
ea2e61bf |
134 | } |
135 | |
ea2e61bf |
136 | sub _register_columns { |
137 | my ($class, @cols) = @_; |
138 | my $names = { %{$class->_columns} }; |
139 | $names->{$_} ||= {} for @cols; |
140 | $class->_columns($names); |
141 | } |
142 | |
143 | sub _mk_column_accessors { |
144 | my ($class, @cols) = @_; |
510ca912 |
145 | $class->mk_group_accessors('column' => @cols); |
ea2e61bf |
146 | } |
147 | |
510ca912 |
148 | sub add_columns { |
8fe001e1 |
149 | my ($class, @cols) = @_; |
150 | $class->_register_columns(@cols); |
151 | $class->_mk_column_accessors(@cols); |
152 | } |
153 | |
154 | sub retrieve_from_sql { |
155 | my ($class, $cond, @vals) = @_; |
a3018bd3 |
156 | $cond =~ s/^\s*WHERE//i; |
604d9f38 |
157 | my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {}); |
158 | my @cols = $class->_select_columns($attrs); |
8fe001e1 |
159 | my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond); |
b8e1e21f |
160 | #warn "$cond @vals"; |
510ca912 |
161 | return $class->sth_to_objects($sth, \@vals, \@cols); |
162 | } |
163 | |
164 | sub sth_to_objects { |
165 | my ($class, $sth, $args, $cols) = @_; |
166 | my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} ); |
167 | $sth->execute(@$args); |
8fe001e1 |
168 | my @found; |
169 | while (my @row = $sth->fetchrow_array) { |
c1d23573 |
170 | push(@found, $class->_row_to_object(\@cols, \@row)); |
8fe001e1 |
171 | } |
604d9f38 |
172 | $sth->finish; |
8fe001e1 |
173 | return @found; |
174 | } |
175 | |
c1d23573 |
176 | sub _row_to_object { # WARNING: Destructive to @$row |
177 | my ($class, $cols, $row) = @_; |
178 | my $new = $class->new; |
179 | $new->store_column($_, shift @$row) for @$cols; |
180 | $new->in_database(1); |
181 | return $new; |
182 | } |
183 | |
8fe001e1 |
184 | sub search { |
12bbb339 |
185 | my $class = shift; |
186 | my $attrs = { }; |
187 | if (@_ > 1 && ref $_[$#_] eq 'HASH') { |
188 | $attrs = { %{ pop(@_) } }; |
189 | } |
190 | my $query = ref $_[0] eq "HASH" ? shift: {@_}; |
191 | my ($cond, @param) = $class->_cond_resolve($query, $attrs); |
c687b87e |
192 | return $class->retrieve_from_sql($cond, @param, $attrs); |
a3018bd3 |
193 | } |
194 | |
195 | sub search_like { |
196 | my $class = shift; |
12bbb339 |
197 | my $attrs = { }; |
198 | if (@_ > 1 && ref $_[$#_] eq 'HASH') { |
199 | $attrs = pop(@_); |
200 | } |
201 | return $class->search(@_, { %$attrs, cmp => 'LIKE' }); |
8fe001e1 |
202 | } |
203 | |
204 | sub _select_columns { |
205 | return keys %{$_[0]->_columns}; |
206 | } |
207 | |
208 | sub copy { |
209 | my ($self, $changes) = @_; |
210 | my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self); |
510ca912 |
211 | $new->set_column($_ => $changes->{$_}) for keys %$changes; |
a3018bd3 |
212 | return $new->insert; |
213 | } |
214 | |
12bbb339 |
215 | sub _cond_resolve { |
216 | my ($self, $query, $attrs) = @_; |
604d9f38 |
217 | return '1 = 1' unless keys %$query; |
12bbb339 |
218 | my $op = $attrs->{'cmp'} || '='; |
a3018bd3 |
219 | my $cond = join(' AND ', |
220 | map { (defined $query->{$_} |
221 | ? "$_ $op ?" |
222 | : (do { delete $query->{$_}; "$_ IS NULL"; })); |
223 | } keys %$query); |
12bbb339 |
224 | return ($cond, values %$query); |
8fe001e1 |
225 | } |
226 | |
510ca912 |
227 | sub table { |
228 | shift->_table_name(@_); |
229 | } |
230 | |
ea2e61bf |
231 | 1; |
34d52be2 |
232 | |
233 | =back |
234 | |
235 | =head1 AUTHORS |
236 | |
237 | Matt S. Trout <perl-stuff@trout.me.uk> |
238 | |
239 | =head1 LICENSE |
240 | |
241 | You may distribute this code under the same terms as Perl itself. |
242 | |
243 | =cut |
244 | |