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
CommitLineData
54bed31b 1package DBIx::Data::Store::CRUD;
2
3use strictures 1;
4
967a2b04 5use DBIx::Data::Stream::STH;
6
54bed31b 7sub new {
8 my $proto = shift;
9 bless({ %{$_[0]} }, ref($proto)||$proto);
10}
11
12sub _sql { shift->{sql} }
13sub _raw { shift->{raw} }
967a2b04 14sub _append_args { shift->{append_args} }
54bed31b 15
16sub _run {
17 my $self = shift;
967a2b04 18 my ($run_type, $sql_type, $args) = @_;
54bed31b 19 my $sql = $self->_sql->{$sql_type}||die "No such sql type ${sql_type}";
967a2b04 20 if (my $append = $self->_append_args) {
21 $args = [ @{$args||[]}, @$append ];
22 }
23 $self->_raw->${\"run_${run_type}"}($sql, $args);
54bed31b 24}
25
26sub flatten { @{shift->_run('rowset', 'select_all', @_)} }
27
28sub to_stream { shift->_run('rowstream', 'select_all', @_) }
29
30sub clear { shift->_run('row','delete_all',@_) }
31
32sub get { shift->_run('row','select_one',@_) }
33
34sub replace {
35 my $self = shift;
36 $self->_run('row','update_one', [ @{$_[1]}, @{$_[0]} ]);
37}
38
4b254cac 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
43sub add { shift->_run('row','insert_one',@_)||$_[0] }
44
54bed31b 45sub remove { shift->_run('row','delete_one',@_) }
46
471;