| 1 | package Prosody::Storage::SQL; | ||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 1 | $Prosody::Storage::SQL::AUTHORITY = 'cpan:GETTY'; | |||||||||||||||||
| 4 | } | ||||||||||||||||||
| 5 | { | ||||||||||||||||||
| 6 | 1 | $Prosody::Storage::SQL::VERSION = '0.007'; | |||||||||||||||||
| 7 | } | ||||||||||||||||||
| 8 | # ABSTRACT: access a database of mod_storage_sql | ||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 1 | ▶ | use Moose; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 1 | ▶ | use Prosody::Storage::SQL::DB; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 1 | ▶ | use JSON; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 1 | ▶ | use Encode; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 1 | has driver => ( | |||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||
| 18 | isa => enum(["SQLite3","MySQL","PostgreSQL"]), | ||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||
| 20 | ); | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 1 | has database => ( | |||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||
| 24 | isa => 'Str', | ||||||||||||||||||
| 25 | required => 1, | ||||||||||||||||||
| 26 | ); | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | 1 | has username => ( | |||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||
| 30 | isa => 'Str', | ||||||||||||||||||
| 31 | predicate => 'has_username', | ||||||||||||||||||
| 32 | ); | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 1 | has password => ( | |||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||
| 36 | isa => 'Str', | ||||||||||||||||||
| 37 | predicate => 'has_password', | ||||||||||||||||||
| 38 | ); | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 1 | has host => ( | |||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||
| 42 | isa => 'Str', | ||||||||||||||||||
| 43 | predicate => 'has_host', | ||||||||||||||||||
| 44 | ); | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | has _db => ( | ||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||
| 48 | isa => 'Prosody::Storage::SQL::DB', | ||||||||||||||||||
| 49 | lazy => 1, | ||||||||||||||||||
| 50 | 1 | ▶ | default => sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=18.7
| |||||||||||||||||||
| 51 | 1 | my ( $self ) = @_; | |||||||||||||||||
| 52 | 1 | my $vars = { | |||||||||||||||||
| 53 | quote_char => '"', | ||||||||||||||||||
| 54 | name_sep => '.', | ||||||||||||||||||
| 55 | }; | ||||||||||||||||||
| 56 | 1 | ▶ | if ($self->driver eq 'SQLite3') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 57 | 1 | $vars->{sqlite_unicode} = 1; | |||||||||||||||||
| 58 | } elsif ($self->driver eq 'MySQL') { | ||||||||||||||||||
| 59 | 0 | $vars->{mysql_enable_utf8} = 1, | |||||||||||||||||
| 60 | } elsif ($self->driver eq 'PostgreSQL') { | ||||||||||||||||||
| 61 | 0 | $vars->{pg_enable_utf8} = 1, | |||||||||||||||||
| 62 | }; | ||||||||||||||||||
| 63 | 1 | Prosody::Storage::SQL::DB->connect($self->dsn, $self->username, $self->password, $vars); | |||||||||||||||||
| 64 | }, | ||||||||||||||||||
| 65 | 1 | ); | |||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 4 | ▶ | sub rs { shift->resultset(@_) } | ||||||||||||||||
|
Subroutine
rs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 68 | 4 | ▶ | sub resultset { shift->_db->resultset('Prosody') } | ||||||||||||||||
|
Subroutine
resultset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | has dsn => ( | ||||||||||||||||||
| 71 | is => 'ro', | ||||||||||||||||||
| 72 | lazy => 1, | ||||||||||||||||||
| 73 | 1 | ▶ | default => sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.2
| |||||||||||||||||||
| 74 | 1 | my ( $self ) = @_; | |||||||||||||||||
| 75 | 1 | my $driver; | |||||||||||||||||
| 76 | 1 | ▶ | $driver = 'SQLite' if $self->driver eq "SQLite3"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 77 | 1 | ▶ | $driver = 'mysql' if $self->driver eq "MySQL"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 78 | 1 | ▶ | $driver = 'Pg' if $self->driver eq "PostgreSQL"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | 1 | ▶ | return 'dbi:'.$driver.':dbname='.$self->database.( $self->has_host ? ';host='.$self->host : '' ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 80 | }, | ||||||||||||||||||
| 81 | 1 | ); | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | 1 | ▶ | sub host_list { | ||||||||||||||||
|
Subroutine
host_list: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 84 | 1 | my ( $self ) = @_; | |||||||||||||||||
| 85 | 1 | my @hosts = $self->rs->search({},{ | |||||||||||||||||
| 86 | columns => [ qw/host/ ], | ||||||||||||||||||
| 87 | distinct => 1, | ||||||||||||||||||
| 88 | }); | ||||||||||||||||||
| 89 | 1 | my @hostlist; | |||||||||||||||||
| 90 | 1 | for (@hosts) { push @hostlist, $_->host } | |||||||||||||||||
| 91 | 1 | return @hostlist; | |||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 2 | ▶ | sub user_list { | ||||||||||||||||
|
Subroutine
user_list: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||
| 95 | 2 | my ( $self, $host ) = @_; | |||||||||||||||||
| 96 | 2 | my %query; | |||||||||||||||||
| 97 | 2 | ▶ | $query{host} = $host if $host; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | 2 | my @users = $self->rs->search(\%query,{ | |||||||||||||||||
| 99 | columns => [ qw/user/ ], | ||||||||||||||||||
| 100 | distinct => 1, | ||||||||||||||||||
| 101 | }); | ||||||||||||||||||
| 102 | 2 | my @userlist; | |||||||||||||||||
| 103 | 2 | for (@users) { push @userlist, $_->user } | |||||||||||||||||
| 104 | 2 | return @userlist; | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | 0 | ▶ | sub all_user { | ||||||||||||||||
|
Subroutine
all_user: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 108 | 0 | my ( $self, $host ) = @_; | |||||||||||||||||
| 109 | 0 | my %query; | |||||||||||||||||
| 110 | 0 | ▶ | $query{host} = $host if $host; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 111 | 0 | my @keys = $self->rs->search(\%query); | |||||||||||||||||
| 112 | 0 | my %v; | |||||||||||||||||
| 113 | 0 | for (@keys) { | |||||||||||||||||
| 114 | 0 | $v{$_->user.'@'.$_->host}->{$_->store}->{$_->key} = $self->get_value($_); | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | 0 | return \%v; | |||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 1 | ▶ | sub user { | ||||||||||||||||
|
Subroutine
user: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||
| 120 | 1 | my ( $self, $jid ) = @_; | |||||||||||||||||
| 121 | 1 | my @jidparts = split(/@/,$jid); | |||||||||||||||||
| 122 | 1 | ▶ | die __PACKAGE__.': user parameter needs to be user@host' unless (@jidparts == 2); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 123 | 1 | my @keys = $self->rs->search({ | |||||||||||||||||
| 124 | host => $jidparts[1], | ||||||||||||||||||
| 125 | user => $jidparts[0], | ||||||||||||||||||
| 126 | }); | ||||||||||||||||||
| 127 | 1 | my %v; | |||||||||||||||||
| 128 | 1 | for (@keys) { | |||||||||||||||||
| 129 | 1 | $v{$_->store}->{$_->key} = $self->get_value($_); | |||||||||||||||||
| 130 | } | ||||||||||||||||||
| 131 | 1 | return \%v; | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 1 | ▶ | sub get_value { | ||||||||||||||||
|
Subroutine
get_value: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||
| 135 | 1 | my ( $self, $row ) = @_; | |||||||||||||||||
| 136 | 1 | ▶ | if ($row->type eq 'string') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 137 | 1 | return $row->value; | |||||||||||||||||
| 138 | } elsif ($row->type eq 'json') { | ||||||||||||||||||
| 139 | 0 | return decode_json(encode('utf8', $row->value)); | |||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | 1 | 1; | |||||||||||||||||
| 144 | __END__ | ||||||||||||||||||