allow coderef in place of SQL for complex commands
[dbsrgits/DBIx-Data-Store.git] / lib / DBIx / Data / Store / Raw.pm
1 package DBIx::Data::Store::Raw;
2
3 use strictures 1;
4
5 use DBIx::Connector;
6
7 sub new {
8   my $proto = shift;
9   bless({ %{$_[0]} }, ref($proto)||$proto);
10 }
11
12 sub connect {
13   my $class = shift;
14   $class->new({ connect_info => [ @_ ] });
15 }
16
17 sub connect_info { shift->{connect_info} }
18
19 sub _connector { $_[0]->{_connector} ||= $_[0]->_build__connector }
20
21 sub _build__connector {
22   DBIx::Connector->new(@{$_[0]->connect_info});
23 }
24
25 sub run_row { shift->_exec_calling('fetchrow_arrayref', @_) }
26
27 sub run_rowset { shift->_exec_calling('fetchall_arrayref', @_) }
28
29 sub run_rowstream {
30   shift->_exec_calling(sub {
31     DBIx::Data::Stream::STH->new({ sth => $_[0] })
32   }, @_);
33 }
34
35 sub _exec_calling {
36   my ($self, $sth_method, $to_call, $sth_args) = @_;
37   
38   $self->_exec(
39     ref($to_call) eq 'CODE'
40       ? sub { $self->$to_call($sth_method, $_[0], $sth_args) }
41       : sub {
42           $self->_sth_for($_[0], $to_call, $sth_args)->$sth_method
43         }
44   );
45 }
46
47 sub _exec {
48   $_[0]->_connector->run($_[1]);
49 }
50
51 sub _sth_for {
52   my ($self, $dbh, $sql, $args) = @_;
53   my $sth = $dbh->prepare_cached($sql, {}, 3);
54   $sth->execute(@$args);
55   return $sth;
56 }
57
58 1;