Commit | Line | Data |
54bed31b |
1 | package DBIx::Data::Store::CRUD; |
2 | |
3 | use strictures 1; |
4 | |
967a2b04 |
5 | use DBIx::Data::Stream::STH; |
6 | |
54bed31b |
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} } |
967a2b04 |
14 | sub _append_args { shift->{append_args} } |
54bed31b |
15 | |
16 | sub _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 | |
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 | |
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 | |
43 | sub add { shift->_run('row','insert_one',@_)||$_[0] } |
44 | |
54bed31b |
45 | sub remove { shift->_run('row','delete_one',@_) } |
46 | |
47 | 1; |