Commit | Line | Data |
65b76960 |
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 | |
a1e15ee1 |
8 | foreach my $type (qw(select select_single insert update update_single delete delete_single)) { |
65b76960 |
9 | has "${type}_sql" => (is => 'ro', predicate => "has_${type}_sql"); |
3347c67e |
10 | has "${type}_argument_order" => (is => 'ro', default => sub { [] }); |
65b76960 |
11 | } |
12 | |
3a2e7c1c |
13 | has 'insert_command_constructor' => (is => 'ro'); |
14 | |
3347c67e |
15 | has "select_column_order" => (is => 'ro'); |
16 | |
7cd0d8a9 |
17 | has implicit_arguments => (is => 'ro'); |
18 | |
65b76960 |
19 | method new_select_command ($args) { |
20 | die "$self->has_select_sql" unless $self->has_select_sql; |
3347c67e |
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 | |
e49bd861 |
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 | |
3347c67e |
37 | method _unwrap_args_for ($type, $args) { |
7cd0d8a9 |
38 | if (my $implicit = $self->implicit_arguments) { |
39 | $args = { %$implicit, %$args }; |
40 | } |
3347c67e |
41 | [ @{$args}{@{$self->${\"${type}_argument_order"}}} ] |
42 | } |
43 | |
3a2e7c1c |
44 | method _new_command ($builder, $type, $args) { |
3347c67e |
45 | my $has_meth = "has_${type}_sql"; |
46 | die "${self}->${has_meth}" unless $self->$has_meth; |
3a2e7c1c |
47 | $self->$builder( |
3347c67e |
48 | $self->${\"${type}_sql"}, |
3a2e7c1c |
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, |
3347c67e |
57 | ); |
65b76960 |
58 | } |
59 | |
60 | method new_insert_command ($args) { |
3a2e7c1c |
61 | my $builder = $self->insert_command_constructor; |
62 | $builder |
63 | ? $self->_new_command($builder => insert => $args) |
64 | : $self->_new_call_command(insert => $args); |
65b76960 |
65 | } |
66 | |
67 | method new_update_command ($args) { |
3347c67e |
68 | $self->_new_call_command(update => $args); |
65b76960 |
69 | } |
70 | |
71 | method new_delete_command ($args) { |
3347c67e |
72 | $self->_new_call_command(delete => $args); |
65b76960 |
73 | } |
74 | |
a1e15ee1 |
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 | |
65b76960 |
83 | __PACKAGE__->meta->make_immutable; |
84 | |
85 | 1; |