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 | |
12 | sub 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 | |
25 | sub 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 | |
36 | sub 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 | |
42 | sub 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 | |
60 | sub delete { |
a3018bd3 |
61 | my $self = shift; |
62 | if (ref $self) { |
b8e1e21f |
63 | #warn $self->_ident_cond.' '.join(', ', $self->_ident_values); |
a3018bd3 |
64 | my $sth = $self->_get_sth('delete', undef, |
65 | $self->_table_name, $self->_ident_cond); |
66 | $sth->execute($self->_ident_values); |
67 | $sth->finish; |
68 | delete $self->{_in_database}; |
69 | } else { |
12bbb339 |
70 | my $attrs = { }; |
71 | if (@_ > 1 && ref $_[$#_] eq 'HASH') { |
72 | $attrs = { %{ pop(@_) } }; |
73 | } |
a3018bd3 |
74 | my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_}); |
12bbb339 |
75 | my ($cond, @param) = $self->_cond_resolve($query, $attrs); |
a3018bd3 |
76 | my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond); |
12bbb339 |
77 | $sth->execute(@param); |
a3018bd3 |
78 | $sth->finish; |
79 | } |
ea2e61bf |
80 | return $self; |
81 | } |
82 | |
510ca912 |
83 | sub get_column { |
ea2e61bf |
84 | my ($self, $column) = @_; |
8fe001e1 |
85 | die "Can't fetch data as class method" unless ref $self; |
510ca912 |
86 | die "No such column '${column}'" unless $self->_columns->{$column}; |
a3018bd3 |
87 | return $self->{_column_data}{$column} if $self->_columns->{$column}; |
ea2e61bf |
88 | } |
89 | |
510ca912 |
90 | sub set_column { |
91 | my $self = shift; |
92 | my ($column) = @_; |
93 | my $ret = $self->store_column(@_); |
94 | $self->{_dirty_columns}{$column} = 1; |
95 | return $ret; |
96 | } |
97 | |
98 | sub store_column { |
ea2e61bf |
99 | my ($self, $column, $value) = @_; |
510ca912 |
100 | die "No such column '${column}'" unless $self->_columns->{$column}; |
101 | die "set_column called for ${column} without value" if @_ < 3; |
102 | return $self->{_column_data}{$column} = $value; |
ea2e61bf |
103 | } |
104 | |
ea2e61bf |
105 | sub _register_columns { |
106 | my ($class, @cols) = @_; |
107 | my $names = { %{$class->_columns} }; |
108 | $names->{$_} ||= {} for @cols; |
109 | $class->_columns($names); |
110 | } |
111 | |
112 | sub _mk_column_accessors { |
113 | my ($class, @cols) = @_; |
510ca912 |
114 | $class->mk_group_accessors('column' => @cols); |
ea2e61bf |
115 | } |
116 | |
510ca912 |
117 | sub add_columns { |
8fe001e1 |
118 | my ($class, @cols) = @_; |
119 | $class->_register_columns(@cols); |
120 | $class->_mk_column_accessors(@cols); |
121 | } |
122 | |
123 | sub retrieve_from_sql { |
124 | my ($class, $cond, @vals) = @_; |
a3018bd3 |
125 | $cond =~ s/^\s*WHERE//i; |
8fe001e1 |
126 | my @cols = $class->_select_columns; |
127 | my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond); |
b8e1e21f |
128 | #warn "$cond @vals"; |
510ca912 |
129 | return $class->sth_to_objects($sth, \@vals, \@cols); |
130 | } |
131 | |
132 | sub sth_to_objects { |
133 | my ($class, $sth, $args, $cols) = @_; |
134 | my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} ); |
135 | $sth->execute(@$args); |
8fe001e1 |
136 | my @found; |
137 | while (my @row = $sth->fetchrow_array) { |
138 | my $new = $class->new; |
510ca912 |
139 | $new->store_column($_, shift @row) for @cols; |
8fe001e1 |
140 | $new->{_in_database} = 1; |
141 | push(@found, $new); |
142 | } |
143 | return @found; |
144 | } |
145 | |
146 | sub search { |
12bbb339 |
147 | my $class = shift; |
148 | my $attrs = { }; |
149 | if (@_ > 1 && ref $_[$#_] eq 'HASH') { |
150 | $attrs = { %{ pop(@_) } }; |
151 | } |
152 | my $query = ref $_[0] eq "HASH" ? shift: {@_}; |
153 | my ($cond, @param) = $class->_cond_resolve($query, $attrs); |
154 | return $class->retrieve_from_sql($cond, @param); |
a3018bd3 |
155 | } |
156 | |
157 | sub search_like { |
158 | my $class = shift; |
12bbb339 |
159 | my $attrs = { }; |
160 | if (@_ > 1 && ref $_[$#_] eq 'HASH') { |
161 | $attrs = pop(@_); |
162 | } |
163 | return $class->search(@_, { %$attrs, cmp => 'LIKE' }); |
8fe001e1 |
164 | } |
165 | |
166 | sub _select_columns { |
167 | return keys %{$_[0]->_columns}; |
168 | } |
169 | |
170 | sub copy { |
171 | my ($self, $changes) = @_; |
172 | my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self); |
510ca912 |
173 | $new->set_column($_ => $changes->{$_}) for keys %$changes; |
a3018bd3 |
174 | return $new->insert; |
175 | } |
176 | |
12bbb339 |
177 | sub _cond_resolve { |
178 | my ($self, $query, $attrs) = @_; |
179 | my $op = $attrs->{'cmp'} || '='; |
a3018bd3 |
180 | my $cond = join(' AND ', |
181 | map { (defined $query->{$_} |
182 | ? "$_ $op ?" |
183 | : (do { delete $query->{$_}; "$_ IS NULL"; })); |
184 | } keys %$query); |
12bbb339 |
185 | return ($cond, values %$query); |
8fe001e1 |
186 | } |
187 | |
510ca912 |
188 | sub table { |
189 | shift->_table_name(@_); |
190 | } |
191 | |
ea2e61bf |
192 | 1; |