line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::Plugin::Role::SqlUtils; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
4194
|
use strict; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
207
|
|
4
|
6
|
|
|
6
|
|
22
|
use Moose::Role; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
41
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: role with support function for plugins using sql |
7
|
|
|
|
|
|
|
our $VERSION = '2.01'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
requires '_db_handle'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub sql_table_exists { |
12
|
7
|
|
|
7
|
0
|
319
|
my ( $self, $name ) = @_; |
13
|
7
|
|
|
|
|
227
|
my $dbh = $self->_db_handle; |
14
|
7
|
|
|
|
|
31
|
my $sql = 'SELECT * FROM '.$dbh->quote_identifier($name).' WHERE 1=0;'; |
15
|
7
|
|
|
|
|
142
|
eval { $dbh->do($sql); }; |
|
7
|
|
|
|
|
23
|
|
16
|
7
|
100
|
|
|
|
726
|
if( $@ ) { |
17
|
5
|
|
|
|
|
17
|
return 0; |
18
|
|
|
|
|
|
|
} |
19
|
2
|
|
|
|
|
10
|
return 1; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub create_sql_table { |
23
|
8
|
|
|
8
|
0
|
1501
|
my ( $self, $name, $definitions ) = @_; |
24
|
8
|
|
|
|
|
257
|
my $dbh = $self->_db_handle; |
25
|
8
|
|
|
|
|
34
|
my $table_name = $dbh->quote_identifier($name); |
26
|
8
|
|
|
|
|
150
|
my $sql; |
27
|
8
|
|
|
|
|
80
|
my $driver = $dbh->{Driver}->{Name}; |
28
|
|
|
|
|
|
|
|
29
|
8
|
100
|
|
|
|
25
|
if( defined $definitions->{$driver} ) { |
|
|
100
|
|
|
|
|
|
30
|
5
|
|
|
|
|
7
|
$sql = $definitions->{$driver}; |
31
|
|
|
|
|
|
|
} elsif ( defined $definitions->{'*'} ) { |
32
|
2
|
|
|
|
|
3
|
$sql = $definitions->{'*'}; |
33
|
|
|
|
|
|
|
} else { |
34
|
1
|
|
|
|
|
13
|
die('no data definition for table '.$name.'/'.$driver.' found'); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
7
|
|
|
|
|
21
|
$sql =~ s/%TABLE_NAME%/$table_name/g; |
38
|
7
|
|
|
|
|
58
|
$dbh->do( $sql ); |
39
|
4
|
|
|
|
|
744
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub check_sql_tables { |
43
|
3
|
|
|
3
|
0
|
46
|
my ( $self, %tables ) = @_; |
44
|
3
|
|
|
|
|
9
|
foreach my $table ( keys %tables ) { |
45
|
5
|
100
|
|
|
|
14
|
if( ! $self->sql_table_exists( $table ) ) { |
46
|
4
|
|
|
|
|
5
|
eval { $self->create_sql_table( $table, $tables{$table} ) }; |
|
4
|
|
|
|
|
13
|
|
47
|
4
|
100
|
|
|
|
38
|
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
|
74
|
my ( $self, $sql, @params ) = @_; |
56
|
31
|
|
|
|
|
899
|
my $dbh = $self->_db_handle; |
57
|
31
|
|
|
|
|
106
|
my $sth = $dbh->prepare( $sql ); |
58
|
30
|
|
|
|
|
1985
|
$sth->execute( @params ); |
59
|
30
|
|
|
|
|
277
|
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.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 |