line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::Plugin::Role::SqlUtils; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
4087
|
use strict; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
188
|
|
4
|
6
|
|
|
6
|
|
22
|
use Moose::Role; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
43
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: role with support function for plugins using sql |
7
|
|
|
|
|
|
|
our $VERSION = '2.02'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
requires '_db_handle'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub sql_table_exists { |
12
|
8
|
|
|
8
|
0
|
225
|
my ( $self, $name ) = @_; |
13
|
8
|
|
|
|
|
275
|
my $dbh = $self->_db_handle; |
14
|
8
|
|
|
|
|
38
|
my $sql = 'SELECT * FROM '.$dbh->quote_identifier($name).' WHERE 1=0;'; |
15
|
8
|
|
|
|
|
174
|
eval { $dbh->do($sql); }; |
|
8
|
|
|
|
|
29
|
|
16
|
8
|
100
|
|
|
|
818
|
if( $@ ) { |
17
|
5
|
|
|
|
|
16
|
return 0; |
18
|
|
|
|
|
|
|
} |
19
|
3
|
|
|
|
|
13
|
return 1; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub create_sql_table { |
23
|
8
|
|
|
8
|
0
|
842
|
my ( $self, $name, $definitions ) = @_; |
24
|
8
|
|
|
|
|
303
|
my $dbh = $self->_db_handle; |
25
|
8
|
|
|
|
|
33
|
my $table_name = $dbh->quote_identifier($name); |
26
|
8
|
|
|
|
|
149
|
my $sql; |
27
|
8
|
|
|
|
|
73
|
my $driver = $dbh->{Driver}->{Name}; |
28
|
|
|
|
|
|
|
|
29
|
8
|
100
|
|
|
|
28
|
if( defined $definitions->{$driver} ) { |
|
|
100
|
|
|
|
|
|
30
|
5
|
|
|
|
|
7
|
$sql = $definitions->{$driver}; |
31
|
|
|
|
|
|
|
} elsif ( defined $definitions->{'*'} ) { |
32
|
2
|
|
|
|
|
3
|
$sql = $definitions->{'*'}; |
33
|
|
|
|
|
|
|
} else { |
34
|
1
|
|
|
|
|
11
|
die('no data definition for table '.$name.'/'.$driver.' found'); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
7
|
|
|
|
|
19
|
$sql =~ s/%TABLE_NAME%/$table_name/g; |
38
|
7
|
|
|
|
|
61
|
$dbh->do( $sql ); |
39
|
4
|
|
|
|
|
753
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub check_sql_tables { |
43
|
3
|
|
|
3
|
0
|
43
|
my ( $self, %tables ) = @_; |
44
|
3
|
|
|
|
|
12
|
foreach my $table ( keys %tables ) { |
45
|
6
|
100
|
|
|
|
12
|
if( ! $self->sql_table_exists( $table ) ) { |
46
|
4
|
|
|
|
|
5
|
eval { $self->create_sql_table( $table, $tables{$table} ) }; |
|
4
|
|
|
|
|
14
|
|
47
|
4
|
100
|
|
|
|
41
|
if( $@ ) { |
48
|
1
|
|
|
|
|
7
|
die('sql table '.$table.' does not exist and creating it failed: '.$@); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub execute_sql { |
55
|
31
|
|
|
31
|
0
|
89
|
my ( $self, $sql, @params ) = @_; |
56
|
31
|
|
|
|
|
1060
|
my $dbh = $self->_db_handle; |
57
|
31
|
|
|
|
|
153
|
my $sth = $dbh->prepare( $sql ); |
58
|
30
|
|
|
|
|
2905
|
$sth->execute( @params ); |
59
|
30
|
|
|
|
|
400
|
return $sth; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
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.02 |
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 |