9873ce42e65e4ff85bb535ab97d1068bf476316b
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Table.pm
1 package DBIx::Class::Table;
2
3 use strict;
4 use warnings;
5
6 use base qw/Class::Data::Inheritable DBIx::Class::SQL/;
7
8 __PACKAGE__->mk_classdata('_columns' => {});
9
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) {
17     die "attrs must be a hashref" unless ref($attrs) eq 'HASH';
18     while (my ($k, $v) = each %{$attrs}) {
19       $new->store_column($k => $v);
20     }
21   }
22   return $new;
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;
32   $self->{_dirty_columns} = {};
33   return $self;
34 }
35
36 sub create {
37   my ($class, $attrs) = @_;
38   die "create needs a hashref" unless ref $attrs eq 'HASH';
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} || {}};
46   return -1 unless @to_update;
47   my $sth = $self->_get_sth('update', \@to_update,
48                               $self->_table_name, $self->_ident_cond);
49   my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
50                   $self->_ident_values );
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   }
56   $self->{_dirty_columns} = {};
57   return $self;
58 }
59
60 sub delete {
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   }
75   return $self;
76 }
77
78 sub get_column {
79   my ($self, $column) = @_;
80   die "Can't fetch data as class method" unless ref $self;
81   die "No such column '${column}'" unless $self->_columns->{$column};
82   return $self->{_column_data}{$column} if $self->_columns->{$column};
83 }
84
85 sub 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
93 sub store_column {
94   my ($self, $column, $value) = @_;
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;
98 }
99
100 sub _register_columns {
101   my ($class, @cols) = @_;
102   my $names = { %{$class->_columns} };
103   $names->{$_} ||= {} for @cols;
104   $class->_columns($names); 
105 }
106
107 sub _mk_column_accessors {
108   my ($class, @cols) = @_;
109   $class->mk_group_accessors('column' => @cols);
110 }
111
112 sub add_columns {
113   my ($class, @cols) = @_;
114   $class->_register_columns(@cols);
115   $class->_mk_column_accessors(@cols);
116 }
117
118 sub retrieve_from_sql {
119   my ($class, $cond, @vals) = @_;
120   $cond =~ s/^\s*WHERE//i;
121   my @cols = $class->_select_columns;
122   my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
123   return $class->sth_to_objects($sth, \@vals, \@cols);
124 }
125
126 sub sth_to_objects {
127   my ($class, $sth, $args, $cols) = @_;
128   my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
129   $sth->execute(@$args);
130   my @found;
131   while (my @row = $sth->fetchrow_array) {
132     my $new = $class->new;
133     $new->store_column($_, shift @row) for @cols;
134     $new->{_in_database} = 1;
135     push(@found, $new);
136   }
137   return @found;
138 }
139
140 sub search {
141   my $class    = shift;
142   my $where    = ref $_[0] eq "HASH" ? shift: {@_};
143   my ($cond, $param)  = $class->_where_from_hash($where);
144   return $class->retrieve_from_sql($cond, @{$param});
145 }
146
147 sub 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});
152 }
153
154 sub _select_columns {
155   return keys %{$_[0]->_columns};
156 }
157
158 sub copy {
159   my ($self, $changes) = @_;
160   my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
161   $new->set_column($_ => $changes->{$_}) for keys %$changes;
162   return $new->insert;
163 }
164
165 sub _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 ]);
174 }
175
176 sub table {
177   shift->_table_name(@_);
178 }
179
180 1;