package DBIx::Data::Store::CRUD;

use strictures 1;

sub new {
  my $proto = shift;
  bless({ %{$_[0]} }, ref($proto)||$proto);
}

sub _sql { shift->{sql} }
sub _raw { shift->{raw} }

sub _run {
  my $self = shift;
  my ($run_type, $sql_type, @args) = @_;
  my $sql = $self->_sql->{$sql_type}||die "No such sql type ${sql_type}";
  $self->_raw->${\"run_${run_type}"}($sql, @args);
}

sub flatten { @{shift->_run('rowset', 'select_all', @_)} }

sub to_stream { shift->_run('rowstream', 'select_all', @_) }

sub clear { shift->_run('row','delete_all',@_) }

sub get { shift->_run('row','select_one',@_) }

sub replace { 
  my $self = shift;
  $self->_run('row','update_one', [ @{$_[1]}, @{$_[0]} ]);
}

sub add { shift->_run('row','insert_one',@_) }
sub remove { shift->_run('row','delete_one',@_) }

1;
