| 1 | package DJabberd::Plugin::PrivateStorage::DBI; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use base 'DJabberd::Plugin::PrivateStorage'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use DJabberd::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | our $logger = DJabberd::Log->get_logger(); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # TODO allow to set a different table name | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head2 set_config_datasource($self, $val) | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Set the DBI datasource that will be used to connect. | ||||||||||||||||||||||||||||
| 16 | 'dbi:' is automatically added, see the documentation of DBI | ||||||||||||||||||||||||||||
| 17 | for more information. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub set_config_datasource { | ||||||||||||||||||||||||||
|
Subroutine
set_config_datasource: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 23 | 0 | $self->{datasource} = $val; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head2 set_config_username($self, $val) | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Set the username used to connect to the database. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub set_config_username { | ||||||||||||||||||||||||||
|
Subroutine
set_config_username: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 0 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 34 | 0 | $self->{username} = $val; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 set_config_password($self, $val) | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Set the password used to connect to the database. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub set_config_password { | ||||||||||||||||||||||||||
|
Subroutine
set_config_password: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 0 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 45 | 0 | $self->{password} = $val; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head2 finalize($self) | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Check that plugin was correctly initialized. | ||||||||||||||||||||||||||||
| 51 | Try to connect to the database and create the table. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub finalize { | ||||||||||||||||||||||||||
|
Subroutine
finalize: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | die "No 'Database' configured'" unless $self->{datasource}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | my $dbh = DBI->connect_cached("dbi:$self->{datasource}",$self->{username}, $self->{password}, { RaiseError => 1, PrintError => 0, AutoCommit => 1 }); | |||||||||||||||||||||||||||
| 59 | 0 | $self->{dbh} = $dbh; | |||||||||||||||||||||||||||
| 60 | 0 | $self->check_install_schema; | |||||||||||||||||||||||||||
| 61 | 0 | $self->{sth} = $dbh->prepare('SELECT content FROM private_storage WHERE username = ? AND namespace = ?'); | |||||||||||||||||||||||||||
| 62 | 0 | return $self; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head2 check_install_schema($self) | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Create the needed table, if it doesn't exist. This requires the | ||||||||||||||||||||||||||||
| 69 | proper privileges to the database. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub check_install_schema { | ||||||||||||||||||||||||||
|
Subroutine
check_install_schema: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 0 | my $dbh = $self->{dbh}; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | eval { | |||||||||||||||||||||||||||
| 78 | # TODO create a primary key | ||||||||||||||||||||||||||||
| 79 | 0 | $dbh->do(qq{ | |||||||||||||||||||||||||||
| 80 | CREATE TABLE private_storage ( | ||||||||||||||||||||||||||||
| 81 | username TEXT NOT NULL, | ||||||||||||||||||||||||||||
| 82 | namespace TEXT NOT NULL, | ||||||||||||||||||||||||||||
| 83 | content TEXT | ||||||||||||||||||||||||||||
| 84 | );}); | ||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($@ && $@ !~ /table \w+ already exists/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ and not $@ =~ /table \w+ already exists/
MC/DC0%0/2
$@ and not $@ =~ /table \w+ already exists/
$@
not $@ =~ /table \w+ already exists/
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ /table \w+ already exists/
| |||||||||||||||||||||||||||||
| 87 | 0 | $logger->logdie("SQL error $@"); | |||||||||||||||||||||||||||
| 88 | 0 | die "SQL error: $@\n"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | $logger->info("Created all roster tables"); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 load_privatestorage($self, $user, $element) | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Load the element $element for $user from memory. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub load_privatestorage { | ||||||||||||||||||||||||||
|
Subroutine
load_privatestorage: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($self, $user, $element) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | $self->{sth}->execute($user,$element); | |||||||||||||||||||||||||||
| 103 | 0 | my ($content) = $self->{sth}->fetchrow_array(); | |||||||||||||||||||||||||||
| 104 | 0 | return $content; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 store_privatestorage($self, $user, $element, $content) | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Store $content for $element and $user in memory. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub store_privatestorage { | ||||||||||||||||||||||||||
|
Subroutine
store_privatestorage: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($self, $user, $element, $content) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | $content = $content->as_xml; | |||||||||||||||||||||||||||
| 116 | 0 | $self->{sth}->execute($user,$element); | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($self->{sth}->fetchrow_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $self->{dbh}->do(qq{ | |||||||||||||||||||||||||||
| 119 | UPDATE private_storage SET content = ? WHERE namespace = ? | ||||||||||||||||||||||||||||
| 120 | AND username = ?;}, undef, $content, $element, $user | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 0 | $self->{dbh}->do(qq{ | |||||||||||||||||||||||||||
| 125 | INSERT INTO private_storage (username, namespace, content) | ||||||||||||||||||||||||||||
| 126 | VALUES (?,?,?)}, undef, $user, $element, $content | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 2 | 1; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||