| 1 | package Beam::Make::DBI; | ||
| 2 | 2 | our $VERSION = '0.003'; | |
| 3 | # ABSTRACT: A Beam::Make recipe for executing SQL queries | ||
| 4 | |||
| 5 | #pod =head1 SYNOPSIS | ||
| 6 | #pod | ||
| 7 | #pod ### container.yml | ||
| 8 | #pod # A Beam::Wire container to configure a database connection to use | ||
| 9 | #pod sqlite: | ||
| 10 | #pod $class: DBI | ||
| 11 | #pod $method: connect | ||
| 12 | #pod $args: | ||
| 13 | #pod - dbi:SQLite:conversion.db | ||
| 14 | #pod | ||
| 15 | #pod ### Beamfile | ||
| 16 | #pod convert: | ||
| 17 | #pod $class: Beam::Wire::DBI | ||
| 18 | #pod dbh: { $ref: 'container.yml:sqlite' } | ||
| 19 | #pod query: | ||
| 20 | #pod - | | ||
| 21 | #pod INSERT INTO accounts ( account_id, address ) | ||
| 22 | #pod SELECT | ||
| 23 | #pod acct_no, | ||
| 24 | #pod CONCAT( street, "\n", city, " ", state, " ", zip ) | ||
| 25 | #pod FROM OLD_ACCTS | ||
| 26 | #pod | ||
| 27 | #pod =head1 DESCRIPTION | ||
| 28 | #pod | ||
| 29 | #pod This L<Beam::Make> recipe class executes one or more SQL queries against | ||
| 30 | #pod the given L<DBI> database handle. | ||
| 31 | #pod | ||
| 32 | #pod =head1 SEE ALSO | ||
| 33 | #pod | ||
| 34 | #pod L<Beam::Make>, L<Beam::Wire>, L<DBI> | ||
| 35 | #pod | ||
| 36 | #pod =cut | ||
| 37 | |||
| 38 | 2 | ▶ | use v5.20; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 2 | ▶ | use Time::Piece; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 2 | ▶ | use Digest::SHA qw( sha1_base64 ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 2 | ▶ | use List::Util qw( pairs ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 2 | ▶ | use experimental qw( signatures postderef ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | |||
| 46 | 2 | extends 'Beam::Make::Recipe'; | |
| 47 | |||
| 48 | #pod =attr dbh | ||
| 49 | #pod | ||
| 50 | #pod Required. The L<DBI> database handle to use. Can be a reference to a service | ||
| 51 | #pod in a L<Beam::Wire> container using C<< { $ref: "<container>:<service>" } >>. | ||
| 52 | #pod | ||
| 53 | #pod =cut | ||
| 54 | |||
| 55 | 2 | has dbh => ( is => 'ro', required => 1 ); | |
| 56 | |||
| 57 | #pod =attr query | ||
| 58 | #pod | ||
| 59 | #pod An array of SQL queries to execute. | ||
| 60 | #pod | ||
| 61 | #pod =cut | ||
| 62 | |||
| 63 | 2 | has query => ( is => 'ro', required => 1 ); | |
| 64 | |||
| 65 | 2 | ▶ | sub make( $self, %vars ) { |
|
Subroutine
make: called CC=2 SCAR=6.9
Pod
documented
| |||
| 66 | 2 | my $dbh = $self->dbh; | |
| 67 | 2 | for my $sql ( $self->query->@* ) { | |
| 68 | 4 | $dbh->do( $sql ); | |
| 69 | } | ||
| 70 | 2 | $self->cache->set( $self->name, $self->_cache_hash ); | |
| 71 | 2 | return 0; | |
| 72 | } | ||
| 73 | |||
| 74 | 11 | ▶ | sub _cache_hash( $self ) { |
|
Subroutine
_cache_hash: called CC=1 SCAR=0.0
| |||
| 75 | # If our write query changed, we should update | ||
| 76 | 11 | my $content = sha1_base64( join "\0", $self->query->@* ); | |
| 77 | 11 | return $content; | |
| 78 | } | ||
| 79 | |||
| 80 | 9 | ▶ | sub last_modified( $self ) { |
|
Subroutine
last_modified: called CC=1 SCAR=0.0
Pod
documented
| |||
| 81 | 9 | my $last_modified = $self->cache->last_modified( $self->name, $self->_cache_hash ); | |
| 82 | 9 | return $last_modified; | |
| 83 | } | ||
| 84 | |||
| 85 | 2 | 1; | |
| 86 | |||
| 87 | __END__ | ||