return add args if no return from the insert_one query in CRUD so RETURNING isn't...
[dbsrgits/DBIx-Data-Store.git] / lib / DBIx / Data / Store / CRUD.pm
1 package DBIx::Data::Store::CRUD;
2
3 use strictures 1;
4
5 use DBIx::Data::Stream::STH;
6
7 sub new {
8   my $proto = shift;
9   bless({ %{$_[0]} }, ref($proto)||$proto);
10 }
11
12 sub _sql { shift->{sql} }
13 sub _raw { shift->{raw} }
14 sub _append_args { shift->{append_args} }
15
16 sub _run {
17   my $self = shift;
18   my ($run_type, $sql_type, $args) = @_;
19   my $sql = $self->_sql->{$sql_type}||die "No such sql type ${sql_type}";
20   if (my $append = $self->_append_args) {
21     $args = [ @{$args||[]}, @$append ];
22   }
23   $self->_raw->${\"run_${run_type}"}($sql, $args);
24 }
25
26 sub flatten { @{shift->_run('rowset', 'select_all', @_)} }
27
28 sub to_stream { shift->_run('rowstream', 'select_all', @_) }
29
30 sub clear { shift->_run('row','delete_all',@_) }
31
32 sub get { shift->_run('row','select_one',@_) }
33
34 sub replace { 
35   my $self = shift;
36   $self->_run('row','update_one', [ @{$_[1]}, @{$_[0]} ]);
37 }
38
39 # returning args ($_[0]) if we don't get anything back saves us needing
40 # to emulate INSERT RETURNING in the case where no columns are supplied
41 # by the database. I think this is a feature.
42
43 sub add { shift->_run('row','insert_one',@_)||$_[0] }
44
45 sub remove { shift->_run('row','delete_one',@_) }
46
47 1;