add a couple bits and pieces
[dbsrgits/DBIx-Data-Store-old.git] / lib / DBIx / Data / Store / CRUD.pm
1 package DBIx::Data::Store::CRUD;
2
3 use Moose;
4 use Method::Signatures::Simple;
5
6 has raw_store => (is => 'ro', required => 1); # DBIx::Data::Store object
7
8 foreach my $type (qw(select select_single insert update update_single delete delete_single)) {
9   has "${type}_sql" => (is => 'ro', predicate => "has_${type}_sql");
10   has "${type}_argument_order" => (is => 'ro', default => sub { [] });
11 }
12
13 has 'insert_command_constructor' => (is => 'ro');
14
15 has "select_column_order" => (is => 'ro');
16
17 has implicit_arguments => (is => 'ro');
18
19 method new_select_command ($args) {
20   die "$self->has_select_sql" unless $self->has_select_sql;
21   $self->raw_store->new_stream_command(
22     $self->select_sql,
23     $self->_unwrap_args_for(select => $args),
24     $self->select_column_order
25   );
26 }
27
28 method new_select_single_command ($args) {
29   die "$self->has_select_single_sql" unless $self->has_select_single_sql;
30   $self->raw_store->new_row_command(
31     $self->select_single_sql,
32     $self->_unwrap_args_for(select_single => $args),
33     $self->select_column_order
34   );
35 }
36
37 method _unwrap_args_for ($type, $args) {
38   if (my $implicit = $self->implicit_arguments) {
39     $args = { %$implicit, %$args };
40   }
41   [ @{$args}{@{$self->${\"${type}_argument_order"}}} ]
42 }
43
44 method _new_command ($builder, $type, $args) {
45   my $has_meth = "has_${type}_sql";
46   die "${self}->${has_meth}" unless $self->$has_meth;
47   $self->$builder(
48     $self->${\"${type}_sql"},
49     $self->_unwrap_args_for($type => $args),
50   );
51 }
52
53 method _new_call_command ($type, $args) {
54   $self->_new_command(
55     sub { shift->raw_store->new_call_command(@_) },
56     $type => $args,
57   );
58 }
59
60 method new_insert_command ($args) {
61   my $builder = $self->insert_command_constructor;
62   $builder
63     ? $self->_new_command($builder => insert => $args)
64     : $self->_new_call_command(insert => $args);
65 }
66
67 method new_update_command ($args) {
68   $self->_new_call_command(update => $args);
69 }
70
71 method new_delete_command ($args) {
72   $self->_new_call_command(delete => $args);
73 }
74
75 method new_update_single_command ($args) {
76   $self->_new_call_command(update_single => $args);
77 }
78
79 method new_delete_single_command ($args) {
80   $self->_new_call_command(delete_single => $args);
81 }
82
83 __PACKAGE__->meta->make_immutable;
84
85 1;