| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 46 | 4 | eval { $self->create_sql_table( $table, $tables{$table} ) }; | |||||||||||||
| 47 | 4 | ▶ | if( $@ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | ||||||||||||||