File Coverage

blib/lib/Beam/Make/DBI.pm
Criterion Covered Total %
statement 39 39 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 51 51 100.0


line stmt bran cond sub pod time code
1             package Beam::Make::DBI;
2             our $VERSION = '0.002';
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 1     1   18 use v5.20;
  1         3  
39 1     1   8 use warnings;
  1         2  
  1         41  
40 1     1   7 use Moo;
  1         3  
  1         7  
41 1     1   440 use Time::Piece;
  1         2  
  1         10  
42 1     1   664 use Digest::SHA qw( sha1_base64 );
  1         2747  
  1         79  
43 1     1   9 use List::Util qw( pairs );
  1         2  
  1         77  
44 1     1   8 use experimental qw( signatures postderef );
  1         2  
  1         16  
45              
46             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             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             has query => ( is => 'ro', required => 1 );
64              
65 2     2 1 5 sub make( $self, %vars ) {
  2         3  
  2         3  
  2         5  
66 2         7 my $dbh = $self->dbh;
67 2         7 for my $sql ( $self->query->@* ) {
68 4         19036 $dbh->do( $sql );
69             }
70 2         17784 $self->cache->set( $self->name, $self->_cache_hash );
71 2         9 return 0;
72             }
73              
74 11     11   19 sub _cache_hash( $self ) {
  11         19  
  11         16  
75             # If our write query changed, we should update
76 11         125 my $content = sha1_base64( join "\0", $self->query->@* );
77 11         56 return $content;
78             }
79              
80 9     9 1 17 sub last_modified( $self ) {
  9         14  
  9         12  
81 9         45 my $last_modified = $self->cache->last_modified( $self->name, $self->_cache_hash );
82 9         662 return $last_modified;
83             }
84              
85             1;
86              
87             __END__
88              
89             =pod
90              
91             =head1 NAME
92              
93             Beam::Make::DBI - A Beam::Make recipe for executing SQL queries
94              
95             =head1 VERSION
96              
97             version 0.002
98              
99             =head1 SYNOPSIS
100              
101             ### container.yml
102             # A Beam::Wire container to configure a database connection to use
103             sqlite:
104             $class: DBI
105             $method: connect
106             $args:
107             - dbi:SQLite:conversion.db
108              
109             ### Beamfile
110             convert:
111             $class: Beam::Wire::DBI
112             dbh: { $ref: 'container.yml:sqlite' }
113             query:
114             - |
115             INSERT INTO accounts ( account_id, address )
116             SELECT
117             acct_no,
118             CONCAT( street, "\n", city, " ", state, " ", zip )
119             FROM OLD_ACCTS
120              
121             =head1 DESCRIPTION
122              
123             This L<Beam::Make> recipe class executes one or more SQL queries against
124             the given L<DBI> database handle.
125              
126             =head1 ATTRIBUTES
127              
128             =head2 dbh
129              
130             Required. The L<DBI> database handle to use. Can be a reference to a service
131             in a L<Beam::Wire> container using C<< { $ref: "<container>:<service>" } >>.
132              
133             =head2 query
134              
135             An array of SQL queries to execute.
136              
137             =head1 SEE ALSO
138              
139             L<Beam::Make>, L<Beam::Wire>, L<DBI>
140              
141             =head1 AUTHOR
142              
143             Doug Bell <preaction@cpan.org>
144              
145             =head1 COPYRIGHT AND LICENSE
146              
147             This software is copyright (c) 2020 by Doug Bell.
148              
149             This is free software; you can redistribute it and/or modify it under
150             the same terms as the Perl 5 programming language system itself.
151              
152             =cut