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.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 1     1   16 use v5.20;
  1         5  
39 1     1   5 use warnings;
  1         2  
  1         45  
40 1     1   6 use Moo;
  1         5  
  1         16  
41 1     1   464 use Time::Piece;
  1         2  
  1         12  
42 1     1   727 use Digest::SHA qw( sha1_base64 );
  1         2820  
  1         81  
43 1     1   9 use List::Util qw( pairs );
  1         2  
  1         108  
44 1     1   9 use experimental qw( signatures postderef );
  1         2  
  1         17  
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 6 sub make( $self, %vars ) {
  2         4  
  2         13  
  2         8  
66 2         13 my $dbh = $self->dbh;
67 2         9 for my $sql ( $self->query->@* ) {
68 4         32929 $dbh->do( $sql );
69             }
70 2         29796 $self->cache->set( $self->name, $self->_cache_hash );
71 2         9 return 0;
72             }
73              
74 11     11   17 sub _cache_hash( $self ) {
  11         17  
  11         15  
75             # If our write query changed, we should update
76 11         97 my $content = sha1_base64( join "\0", $self->query->@* );
77 11         52 return $content;
78             }
79              
80 9     9 1 21 sub last_modified( $self ) {
  9         16  
  9         14  
81 9         50 my $last_modified = $self->cache->last_modified( $self->name, $self->_cache_hash );
82 9         710 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.003
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