| 1 | package Web::Session::MariaDBJSON; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Apache::Session::Lock::Null; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Web::Session::Store::MariaDBJSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Web::Session::Generate::SHA256; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Web::Session::Serialize::JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Web::Session::MySQLJSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Web::Session::_common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8 | our $VERSION = '1.0.0'; | |||||||||||||||||||||||||||
| 13 | 8 | our @ISA = qw(Web::Session::MySQLJSON); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | sub populate { | ||||||||||||||||||||||||||
|
Subroutine
populate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | $self->{object_store} = | ||||||||||||||||||||||||||||
| 19 | 4 | new Web::Session::Store::MariaDBJSON $self; | |||||||||||||||||||||||||||
| 20 | 4 | $self->{lock_manager} = new Apache::Session::Lock::Null $self; | |||||||||||||||||||||||||||
| 21 | 4 | $self->{generate} = \&Web::Session::Generate::SHA256::generate; | |||||||||||||||||||||||||||
| 22 | 4 | $self->{validate} = \&Web::Session::Generate::SHA256::validate; | |||||||||||||||||||||||||||
| 23 | 4 | $self->{serialize} = \&Web::Session::Serialize::JSON::serialize; | |||||||||||||||||||||||||||
| 24 | 4 | $self->{unserialize} = \&Web::Session::Serialize::JSON::unserialize; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | return $self; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # DBD::mysql needs mysql_enable_utf8mb4 at connection time to exchange UTF-8 | ||||||||||||||||||||||||||||
| 30 | # (see the Store) | ||||||||||||||||||||||||||||
| 31 | 4 | ▶ | sub _classDbh { | ||||||||||||||||||||||||||
|
Subroutine
_classDbh: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 32 | 4 | my ( $class, $args ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | my $datasource = $args->{DataSource} or die "No datasource given !"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | return DBI->connect_cached( | ||||||||||||||||||||||||||||
| 36 | $datasource, | ||||||||||||||||||||||||||||
| 37 | $args->{UserName}, | ||||||||||||||||||||||||||||
| 38 | $args->{Password}, | ||||||||||||||||||||||||||||
| 39 | 4 | ▶ | Web::Session::_common->_forkSafeAttr( | ||||||||||||||||||||||||||
|
Condition50%1/2
'DBI'->connect_cached($datasource, $args->{'UserName'}, $args->{'Password'}, 'Web::Session::_common'->_forkSafeAttr('Web::Session::Store::MariaDBJSON'->connectAttributes($datasource))) || die($DBI::errstr)
MC/DC0%0/1
'DBI'->connect_cached($datasource, $args->{'UserName'}, $args->{'Password'}, 'Web::Session::_common'->_forkSafeAttr('Web::Session::Store::MariaDBJSON'->connectAttributes($datasource))) || die($DBI::errstr)
'DBI'->connect_cached($datasource, $args->{'UserName'}, $args->{'Password'}, 'Web::Session::_common'->_forkSafeAttr('Web::Session::Store::MariaDBJSON'->connectAttributes($datasource)))
Truth table50%1/2
A
A= 'DBI'->connect_cached($datasource, $args->{'UserName'}, $args->{'Password'}, 'Web::Session::_common'->_forkSafeAttr('Web::Session::Store::MariaDBJSON'->connectAttributes($datasource)))
| |||||||||||||||||||||||||||||
| 40 | Web::Session::Store::MariaDBJSON->connectAttributes($datasource) | ||||||||||||||||||||||||||||
| 41 | ) | ||||||||||||||||||||||||||||
| 42 | ) || die $DBI::errstr; | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Indexed fields are read from the generated column of the same name: MariaDB | ||||||||||||||||||||||||||||
| 46 | # doesn't always use the index of a generated column when a query contains | ||||||||||||||||||||||||||||
| 47 | # its expression. Fields whose column is not usable are read from the JSON | ||||||||||||||||||||||||||||
| 48 | # document like the other ones | ||||||||||||||||||||||||||||
| 49 | 39 | ▶ | sub _sqlField { | ||||||||||||||||||||||||||
|
Subroutine
_sqlField: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 39 | my ( $class, $dbh, $field, $args ) = @_; | |||||||||||||||||||||||||||
| 51 | 39 | ▶ | if ( $class->_useColumn( $dbh, $field, $args ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 20 | my ($f) = $class->_utf8($field); | |||||||||||||||||||||||||||
| 53 | 20 | $f =~ s/`/``/g; | |||||||||||||||||||||||||||
| 54 | 20 | return "`$f`"; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 19 | return 'JSON_VALUE(a_session, ' . $class->_sqlPath( $dbh, $field ) . ')'; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # True if $field is listed in Index and has a usable generated column | ||||||||||||||||||||||||||||
| 60 | 80 | ▶ | sub _useColumn { | ||||||||||||||||||||||||||
|
Subroutine
_useColumn: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 61 | 80 | my ( $class, $dbh, $field, $args ) = @_; | |||||||||||||||||||||||||||
| 62 | 80 | ▶ | return 0 unless ( $args and $class->_fieldIsIndexed( $args, $field ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args and $class->_fieldIsIndexed($args, $field)
MC/DC100%2/2
$args and $class->_fieldIsIndexed($args, $field)
$args
$class->_fieldIsIndexed($args, $field)
Truth table100%3/3
A and B
A= $argsB= $class->_fieldIsIndexed($args, $field)
| |||||||||||||||||||||||||||||
| 63 | 45 | ▶ | my $valid = $class->_checkIndex( $dbh, $args ) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 45 | my ($f) = $class->_utf8($field); | |||||||||||||||||||||||||||
| 65 | 45 | ▶ | return $valid->{$f} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Indexed fields are read from a generated column: a column that exists but | ||||||||||||||||||||||||||||
| 69 | # is not generated (for example left over from a migration from | ||||||||||||||||||||||||||||
| 70 | # the former column-based MySQL backend, where the store wrote real columns) stays NULL. Check | ||||||||||||||||||||||||||||
| 71 | # the columns once per handle, table and Index list. Returns the hash | ||||||||||||||||||||||||||||
| 72 | # reference of the fields that can be read from their column (empty without | ||||||||||||||||||||||||||||
| 73 | # database handle) | ||||||||||||||||||||||||||||
| 74 | 59 | ▶ | sub _checkIndex { | ||||||||||||||||||||||||||
|
Subroutine
_checkIndex: called CC=16 SCAR=27.8
| |||||||||||||||||||||||||||||
| 75 | 59 | my ( $class, $dbh, $args ) = @_; | |||||||||||||||||||||||||||
| 76 | 59 | ▶ | return {} unless ($dbh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | my $index = | ||||||||||||||||||||||||||||
| 78 | 57 | ▶ | ref( $args->{Index} ) ? $args->{Index} : [ split /\s+/, $args->{Index} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 57 | ▶ | my @index = $class->_utf8( grep { defined and length } @$index ); | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && length $_
MC/DC50%1/2
defined $_ && length $_
defined $_
length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 80 | 57 | ▶ | return {} unless (@index); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 57 | ▶ | my $table = $args->{TableName} || $Apache::Session::Store::DBI::TableName; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
MC/DC0%0/2
$args->{'TableName'} || $Apache::Session::Store::DBI::TableName
$args->{'TableName'}
$Apache::Session::Store::DBI::TableName
Truth table66%2/3
A || B
A= $args->{'TableName'}B= $Apache::Session::Store::DBI::TableName
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # DBI only keeps private_* attributes | ||||||||||||||||||||||||||||
| 85 | 57 | ▶ | my $checked = $dbh->{private_asb_mariadbjson_index} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$dbh->{"private_asb_mariadbjson_index"} ||= {}
MC/DC100%1/1
$dbh->{"private_asb_mariadbjson_index"} ||= {}
$dbh->{"private_asb_mariadbjson_index"}
Truth table100%2/2
A
A= $dbh->{"private_asb_mariadbjson_index"}
| |||||||||||||||||||||||||||||
| 86 | 57 | my $key = join "\0", $table, @index; | |||||||||||||||||||||||||||
| 87 | 57 | ▶ | return $checked->{$key} if ( $checked->{$key} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # TableName may be "schema.table", with or without backquotes | ||||||||||||||||||||||||||||
| 90 | 26 | my ( $schema, $name ) = ( undef, $table ); | |||||||||||||||||||||||||||
| 91 | 26 | ▶ | if ( $table =~ /^`?([^`.]+)`?\.`?([^`.]+)`?\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 2 | ( $schema, $name ) = ( $1, $2 ); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 24 | $name =~ s/`//g; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 26 | ▶ | my $sth = $dbh->prepare( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 'SELECT COLUMN_NAME, GENERATION_EXPRESSION' | ||||||||||||||||||||||||||||
| 100 | . ' FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ' | ||||||||||||||||||||||||||||
| 101 | . ( defined $schema ? '?' : 'DATABASE()' ) | ||||||||||||||||||||||||||||
| 102 | . ' AND TABLE_NAME = ? AND COLUMN_NAME IN (' | ||||||||||||||||||||||||||||
| 103 | . join( ',', ('?') x @index ) . ')' ); | ||||||||||||||||||||||||||||
| 104 | 26 | ▶ | $sth->execute( ( defined $schema ? $schema : () ), $name, @index ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 26 | my %expr = map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref }; | |||||||||||||||||||||||||||
| 106 | 26 | $sth->finish; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 26 | my ( %valid, @unusable ); | |||||||||||||||||||||||||||
| 109 | 26 | foreach my $field (@index) { | |||||||||||||||||||||||||||
| 110 | 42 | ▶ | if ( defined( $expr{$field} ) and $expr{$field} =~ /\ba_session\b/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $expr{$field} and $expr{$field} =~ /\ba_session\b/
MC/DC100%2/2
defined $expr{$field} and $expr{$field} =~ /\ba_session\b/
defined $expr{$field}
$expr{$field} =~ /\ba_session\b/
Truth table100%3/3
A and B
A= defined $expr{$field}B= $expr{$field} =~ /\ba_session\b/
| |||||||||||||||||||||||||||||
| 111 | 34 | $valid{$field} = 1; | |||||||||||||||||||||||||||
| 112 | 34 | next; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 8 | ▶ | my $why = exists( $expr{$field} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | ? 'it exists but is not generated from a_session' | ||||||||||||||||||||||||||||
| 116 | : 'there is no column with this name'; | ||||||||||||||||||||||||||||
| 117 | 8 | push @unusable, "\"$field\" ($why)"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 26 | $checked->{$key} = \%valid; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Warn rather than die: the lookup compares names between Perl and | ||||||||||||||||||||||||||||
| 122 | # information_schema, which can fail on an exotic name encoding even when | ||||||||||||||||||||||||||||
| 123 | # the column is fine | ||||||||||||||||||||||||||||
| 124 | 26 | ▶ | warn "Web::Session::MariaDBJSON: unusable Index field(s) in" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | . " table $table: " | ||||||||||||||||||||||||||||
| 126 | . join( ', ', @unusable ) | ||||||||||||||||||||||||||||
| 127 | . ". They are read from the JSON document, which is slower; add a" | ||||||||||||||||||||||||||||
| 128 | . " generated column based on a_session (see the documentation)\n" | ||||||||||||||||||||||||||||
| 129 | if (@unusable); | ||||||||||||||||||||||||||||
| 130 | 26 | return \%valid; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # No CAST for indexed fields: it would prevent the use of the index | ||||||||||||||||||||||||||||
| 134 | 45 | ▶ | sub _buildCompareExpression { | ||||||||||||||||||||||||||
|
Subroutine
_buildCompareExpression: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 135 | 45 | my ( $class, $field, $op, $value, $dbh, $args ) = @_; | |||||||||||||||||||||||||||
| 136 | 45 | $class->_checkOp($op); | |||||||||||||||||||||||||||
| 137 | 37 | ▶ | return $class->_sqlField( $dbh, $field, $args ) . " $op $value" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | if ( $class->_useColumn( $dbh, $field, $args ) ); | ||||||||||||||||||||||||||||
| 139 | 18 | return $class->SUPER::_buildCompareExpression( $field, $op, $value, $dbh ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 8 | 1; | |||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||