allow coderef in place of SQL for complex commands
[dbsrgits/DBIx-Data-Store.git] / lib / DBIx / Data / Store / Raw.pm
CommitLineData
54bed31b 1package DBIx::Data::Store::Raw;
2
3use strictures 1;
4
5use DBIx::Connector;
6
7sub new {
8 my $proto = shift;
9 bless({ %{$_[0]} }, ref($proto)||$proto);
10}
11
12sub connect {
13 my $class = shift;
14 $class->new({ connect_info => [ @_ ] });
15}
16
17sub connect_info { shift->{connect_info} }
18
19sub _connector { $_[0]->{_connector} ||= $_[0]->_build__connector }
20
21sub _build__connector {
22 DBIx::Connector->new(@{$_[0]->connect_info});
23}
24
25sub run_row { shift->_exec_calling('fetchrow_arrayref', @_) }
26
27sub run_rowset { shift->_exec_calling('fetchall_arrayref', @_) }
28
29sub run_rowstream {
30 shift->_exec_calling(sub {
31 DBIx::Data::Stream::STH->new({ sth => $_[0] })
32 }, @_);
33}
34
35sub _exec_calling {
937bf544 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 );
54bed31b 45}
46
47sub _exec {
48 $_[0]->_connector->run($_[1]);
49}
50
51sub _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
581;