MtPolicyd/Plugin/Role/SqlUtils.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 4 totaltotal 93.4% 57 / 61 CC 7 SCAR 9.8
CC
7
Cov
100%
check_sql_tables
4.0
create_sql_table
3.0
sql_table_exists
2.0
CRAP 2.7
« MtPolicyd/Plugin/ClearFields.pm ↑ Summary MtPolicyd/Plugin/GeoIPLookup.pm »
1 package Mail::MtPolicyd::Plugin::Role::SqlUtils;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: role with support function for plugins using sql
7 6 our $VERSION = '2.01'; # VERSION
8  
9 6 requires '_db_handle';
10  
11 sub sql_table_exists {
12 8 ▶ my ( $self, $name ) = @_;
Subroutine sql_table_exists: called CC=2 SCAR=6.9
Pod undocumented
13 8 my $dbh = $self->_db_handle;
14 8 my $sql = 'SELECT * FROM '.$dbh->quote_identifier($name).' WHERE 1=0;';
15 8 eval { $dbh->do($sql); };
16 8 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 5 3 8
17 5 return 0;
18 }
19 3 return 1;
20 }
21  
22 sub create_sql_table {
23 8 ▶ my ( $self, $name, $definitions ) = @_;
Subroutine create_sql_table: called CC=3 SCAR=11.0
Pod undocumented
24 8 my $dbh = $self->_db_handle;
25 8 my $table_name = $dbh->quote_identifier($name);
26 8 my $sql;
27 8 my $driver = $dbh->{Driver}->{Name};
28  
29 8 ▶ if( defined $definitions->{$driver} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $definitions->{$driver}) { } 5 3 8
elsif (defined $definitions->{'*'}) { } 2 1 3
30 5 $sql = $definitions->{$driver};
31 } elsif ( defined $definitions->{'*'} ) {
32 2 $sql = $definitions->{'*'};
33 } else {
34 1 die('no data definition for table '.$name.'/'.$driver.' found');
35 }
36  
37 7 $sql =~ s/%TABLE_NAME%/$table_name/g;
38 7 $dbh->do( $sql );
39 4 return;
40 }
41  
42 sub check_sql_tables {
43 3 ▶ my ( $self, %tables ) = @_;
Subroutine check_sql_tables: called CC=4 SCAR=13.9
Pod undocumented
44 3 foreach my $table ( keys %tables ) {
45 6 ▶ if( ! $self->sql_table_exists( $table ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sql_table_exists($table)) 4 2 6
46 4 eval { $self->create_sql_table( $table, $tables{$table} ) };
47 4 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
48 1 die('sql table '.$table.' does not exist and creating it failed: '.$@);
49 }
50 }
51 }
52 }
53  
54 sub execute_sql {
55 31 ▶ my ( $self, $sql, @params ) = @_;
Subroutine execute_sql: called CC=1 SCAR=0.0
Pod undocumented
56 31 my $dbh = $self->_db_handle;
57 31 my $sth = $dbh->prepare( $sql );
58 30 $sth->execute( @params );
59 30 return $sth;
60 }
61  
62 6 1;
63  
64 __END__
65  
66 =pod
67  
68 =encoding UTF-8
69  
70 =head1 NAME
71  
72 Mail::MtPolicyd::Plugin::Role::SqlUtils - role with support function for plugins using sql
73  
74 =head1 VERSION
75  
76 version 2.01
77  
78 =head1 AUTHOR
79  
80 Markus Benning <ich@markusbenning.de>
81  
82 =head1 COPYRIGHT AND LICENSE
83  
84 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
85  
86 This is free software, licensed under:
87  
88 The GNU General Public License, Version 2, June 1991
89  
90 =cut
« MtPolicyd/Plugin/ClearFields.pm ↑ Summary MtPolicyd/Plugin/GeoIPLookup.pm »