File Coverage

blib/lib/Auth/Kokolores/Plugin/SqlConnection.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Auth::Kokolores::Plugin::SqlConnection;
2              
3 2     2   1547 use Moose;
  2         4  
  2         11  
4 2     2   10858 use DBI;
  2         24580  
  2         137  
5              
6             # ABSTRACT: kokolores plugin to configure a SQL connection
7             our $VERSION = '1.01'; # VERSION
8              
9             extends 'Auth::Kokolores::Plugin';
10              
11 2     2   404 use Auth::Kokolores::ConnectionPool;
  2         3  
  2         423  
12              
13              
14             has 'handle' => ( is => 'rw', isa => 'Str', default => 'sql' );
15              
16             has 'user' => ( is => 'ro', isa => 'Str', default => '' );
17             has 'password' => ( is => 'ro', isa => 'Str', default => '' );
18              
19             has 'dsn' => (
20             is => 'ro', isa => 'Str',
21             default => 'dbi:SQLite:dbname=/var/lib/saslauthd/saslauth.sqlite',
22             );
23              
24             has 'dbh' => (
25             is => 'ro', isa => 'DBI::db',
26             lazy => 1,
27             default => sub {
28             my $self = shift;
29             $self->log(3, 'connecting to '.$self->dsn.'...');
30             my $dbh = DBI->connect(
31             $self->dsn, $self->user, $self->password,
32             { RaiseError => 1 },
33             );
34             return $dbh;
35             },
36             );
37              
38             sub child_init {
39 1     1 0 3572 my ( $self, $r ) = @_;
40            
41 1         29 $self->log(3, 'registring sql connection \''.$self->handle.'\'...');
42 1         332 Auth::Kokolores::ConnectionPool->add_handle(
43             $self->handle => $self->dbh,
44             );
45              
46 1         21 return;
47             }
48              
49             sub shutdown {
50 1     1 0 19 my ( $self, $r ) = @_;
51            
52 1         33 $self->log(3, 'unregistring sql connection \''.$self->handle.'\'...');
53 1         170 Auth::Kokolores::ConnectionPool->clear_handle( $self->handle );
54              
55 1         4 return;
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             Auth::Kokolores::Plugin::SqlConnection - kokolores plugin to configure a SQL connection
69              
70             =head1 VERSION
71              
72             version 1.01
73              
74             =head1 DESCRIPTION
75              
76             This plugin creates a connection to an SQL database for use
77             by further SQL plugins.
78              
79             =head1 EXAMPLE
80              
81             <Plugin database>
82             module = "SqlConnection"
83             dsn = "dbi:SQLite:dbname=/var/lib/kokolores/users.sqlite"
84             # handle = "sql"
85             </Plugin>
86              
87             =head1 MODULE PARAMETERS
88              
89             =head2 user (default: '')
90              
91             User used to connect to SQL database.
92              
93             =head2 password (default: '')
94              
95             Password used to connect to SQL database.
96              
97             =head2 dsn (default: 'dbi:SQLite:dbname=/var/lib/saslauthd/saslauth.sqlite')
98              
99             A connection string for the database in perl-DBI format.
100              
101             =head2 handle (default: 'sql')
102              
103             This string is used to register the database connection
104             within the kokolores connection pool.
105              
106             =head1 AUTHOR
107              
108             Markus Benning <ich@markusbenning.de>
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             This software is Copyright (c) 2016 by Markus Benning <ich@markusbenning.de>.
113              
114             This is free software, licensed under:
115              
116             The GNU General Public License, Version 2, June 1991
117              
118             =cut