line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::Plugin::Role::SqlUtils; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
6167
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
175
|
|
4
|
6
|
|
|
6
|
|
31
|
use Moose::Role; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
50
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
32543
|
use Mail::MtPolicyd::SqlConnection; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
3290
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# ABSTRACT: role with support function for plugins using sql |
9
|
|
|
|
|
|
|
our $VERSION = '1.23'; # VERSION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
before 'init' => sub { |
12
|
|
|
|
|
|
|
my $self = shift; |
13
|
|
|
|
|
|
|
if( ! Mail::MtPolicyd::SqlConnection->is_initialized ) { |
14
|
|
|
|
|
|
|
die('no sql database initialized, but required for plugin '.$self->name); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
return; |
17
|
|
|
|
|
|
|
}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub sql_table_exists { |
20
|
7
|
|
|
7
|
0
|
470
|
my ( $self, $name ) = @_; |
21
|
7
|
|
|
|
|
32
|
my $dbh = Mail::MtPolicyd::SqlConnection->instance->dbh; |
22
|
7
|
|
|
|
|
203
|
my $sql = 'SELECT * FROM '.$dbh->quote_identifier($name).' WHERE 1=0;'; |
23
|
7
|
|
|
|
|
199
|
eval { $dbh->do($sql); }; |
|
7
|
|
|
|
|
31
|
|
24
|
7
|
100
|
|
|
|
965
|
if( $@ ) { |
25
|
5
|
|
|
|
|
24
|
return 0; |
26
|
|
|
|
|
|
|
} |
27
|
2
|
|
|
|
|
14
|
return 1; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub create_sql_table { |
31
|
8
|
|
|
8
|
0
|
1538
|
my ( $self, $name, $definitions ) = @_; |
32
|
8
|
|
|
|
|
43
|
my $dbh = Mail::MtPolicyd::SqlConnection->instance->dbh; |
33
|
8
|
|
|
|
|
200
|
my $table_name = $dbh->quote_identifier($name); |
34
|
8
|
|
|
|
|
203
|
my $sql; |
35
|
8
|
|
|
|
|
94
|
my $driver = $dbh->{Driver}->{Name}; |
36
|
|
|
|
|
|
|
|
37
|
8
|
100
|
|
|
|
40
|
if( defined $definitions->{$driver} ) { |
|
|
100
|
|
|
|
|
|
38
|
5
|
|
|
|
|
11
|
$sql = $definitions->{$driver}; |
39
|
|
|
|
|
|
|
} elsif ( defined $definitions->{'*'} ) { |
40
|
2
|
|
|
|
|
4
|
$sql = $definitions->{'*'}; |
41
|
|
|
|
|
|
|
} else { |
42
|
1
|
|
|
|
|
14
|
die('no data definition for table '.$name.'/'.$driver.' found'); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
7
|
|
|
|
|
24
|
$sql =~ s/%TABLE_NAME%/$table_name/g; |
46
|
7
|
|
|
|
|
41
|
$dbh->do( $sql ); |
47
|
4
|
|
|
|
|
1012
|
return; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub check_sql_tables { |
51
|
3
|
|
|
3
|
0
|
61
|
my ( $self, %tables ) = @_; |
52
|
3
|
|
|
|
|
11
|
foreach my $table ( keys %tables ) { |
53
|
5
|
100
|
|
|
|
17
|
if( ! $self->sql_table_exists( $table ) ) { |
54
|
4
|
|
|
|
|
6
|
eval { $self->create_sql_table( $table, $tables{$table} ) }; |
|
4
|
|
|
|
|
15
|
|
55
|
4
|
100
|
|
|
|
42
|
if( $@ ) { |
56
|
1
|
|
|
|
|
9
|
die('sql table '.$table.' does not exist and creating it failed: '.$@); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub execute_sql { |
63
|
31
|
|
|
31
|
0
|
114
|
my ( $self, $sql, @params ) = @_; |
64
|
31
|
|
|
|
|
149
|
my $dbh = Mail::MtPolicyd::SqlConnection->instance->dbh; |
65
|
31
|
|
|
|
|
853
|
my $sth = $dbh->prepare( $sql ); |
66
|
30
|
|
|
|
|
2879
|
$sth->execute( @params ); |
67
|
30
|
|
|
|
|
446
|
return $sth; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=pod |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=encoding UTF-8 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 NAME |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Mail::MtPolicyd::Plugin::Role::SqlUtils - role with support function for plugins using sql |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 VERSION |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
version 1.23 |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 AUTHOR |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Markus Benning <ich@markusbenning.de> |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This is free software, licensed under: |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
The GNU General Public License, Version 2, June 1991 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |