| 1 | package DBIx::POS::Sth; | ||
| 2 | use strict; | ||
| 3 | use utf8; | ||
| 4 | use Hash::Merge qw(merge); | ||
| 5 | |||
| 6 | sub new { | ||
| 7 | #~ my ($class, $dbh, $pos) = map shift, 0..2; | ||
| 8 | #~ my %opt = @_; | ||
| 9 | #~ return bless [$dbh, $pos, \%opt], $class; | ||
| 10 | my $class = shift; | ||
| 11 | return bless \@_, $class; | ||
| 12 | } | ||
| 13 | |||
| 14 | |||
| 15 | sub sth { | ||
| 16 | my ($dbh, $pos, $template) = @{ shift() }; | ||
| 17 | my $name = shift; | ||
| 18 | my %arg = @_; | ||
| 19 | die "No such name[$name] in POS-SQL dict! @{[ join ':', keys %$pos ]}" unless $pos->{$name}; | ||
| 20 | |||
| 21 | my $sql = $pos->{$name}->template(%$template ? %arg ? %{merge($template, \%arg)} : %$template : %arg).sprintf("\n--DBIx::POS::Sth name: [%s]", $pos->{$name}->name); | ||
| 22 | my $param = $pos->{$name}->param; | ||
| 23 | |||
| 24 | my $sth; | ||
| 25 | |||
| 26 | #~ local $dbh->{TraceLevel} = "3|DBD"; | ||
| 27 | |||
| 28 | if ($param && $param->{cached}) { | ||
| 29 | $sth = $dbh->prepare_cached($sql); | ||
| 30 | #~ warn "ST cached: ", $sth->{pg_prepare_name}; | ||
| 31 | } else { | ||
| 32 | $sth = $dbh->prepare($sql); | ||
| 33 | } | ||
| 34 | |||
| 35 | return $sth; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | 1; | ||
| 40 | |||
| 41 | =pod | ||
| 42 | |||
| 43 | =encoding utf8 | ||
| 44 | |||
| 45 | =head1 DBIx::POS::Sth | ||
| 46 | |||
| 47 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||
| 48 | |||
| 49 | =head1 NAME | ||
| 50 | |||
| 51 | DBIx::POS::Sth - is a DBI statements hub. Works with L<DBIx::POS::Template> in pair. | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | my $sth = DBIx::POS::Sth->new( | ||
| 56 | $dbh, | ||
| 57 | $pos, # SQL dict | ||
| 58 | foo => 'bar', # any pairs opts | ||
| 59 | ..., | ||
| 60 | ); | ||
| 61 | my $r = $dbh->selectrow_hashref($sth->sth('foo name')); | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | Dictionary of DBI statements. | ||
| 66 | |||
| 67 | =head1 new($dbh, $pos, ...) | ||
| 68 | |||
| 69 | =head2 $dbh (first in list) | ||
| 70 | |||
| 71 | DBI handle | ||
| 72 | |||
| 73 | =head2 $pos (second in list) | ||
| 74 | |||
| 75 | An SQL dictionary object/instance of the L<DBIx::POS::Template>. | ||
| 76 | |||
| 77 | =head2 <any key=>value pairs> | ||
| 78 | |||
| 79 | Used for templates of $pos. | ||
| 80 | |||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | L<DBIx::POS::Template> | ||
| 85 | |||
| 86 | L<https://www.depesz.com/2012/12/02/what-is-the-point-of-bouncing/> | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | |||
| 91 | __END__ |