| 1 | package CHI::Driver::DBI; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 1 | ▶ | use DBI::Const::GetInfoType; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 1 | ▶ | use Moose; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 1 | our $VERSION = '1.24'; | |||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 1 | extends 'CHI::Driver'; | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 1 | my $type = "CHI::Driver::DBI"; | |||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 1 | subtype "$type.DBIHandleGenerator" => as 'CodeRef'; | |||||||||||||||||
| 18 | 1 | subtype "$type.DBIXConnector" => as 'DBIx::Connector'; | |||||||||||||||||
| 19 | 1 | subtype "$type.DBIHandle" => as 'DBI::db'; | |||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 0 | ▶ | coerce "$type.DBIHandleGenerator" => from "$type.DBIXConnector" => via { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
| |||||||||||||||||||
| 22 | 0 | my $dbixconn = $_; | |||||||||||||||||
| 23 | 0 | ▶ | sub { $dbixconn->dbh } | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 24 | 0 | }; | |||||||||||||||||
| 25 | 0 | ▶ | coerce "$type.DBIHandleGenerator" => from "$type.DBIHandle" => via { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
| |||||||||||||||||||
| 26 | 0 | my $dbh = $_; | |||||||||||||||||
| 27 | 0 | ▶ | sub { $dbh } | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 28 | 0 | }; | |||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 1 | has 'db_name' => ( is => 'rw', isa => 'Str' ); | |||||||||||||||||
| 31 | 1 | has 'dbh' => ( is => 'ro', isa => "$type.DBIHandleGenerator", coerce => 1 ); | |||||||||||||||||
| 32 | 1 | has 'dbh_ro' => ( is => 'ro', isa => "$type.DBIHandleGenerator", predicate => 'has_dbh_ro', coerce => 1 ); | |||||||||||||||||
| 33 | 1 | has 'sql_strings' => ( is => 'rw', isa => 'HashRef', lazy_build => 1 ); | |||||||||||||||||
| 34 | 1 | has 'table_prefix' => ( is => 'rw', isa => 'Str', default => 'chi_' ); | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 0 | ▶ | sub BUILD { | ||||||||||||||||
|
Subroutine
BUILD: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 39 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | 0 | my $dbh = $self->dbh->(); | |||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 0 | $self->db_name( $dbh->get_info( $GetInfoType{SQL_DBMS_NAME} ) ); | |||||||||||||||||
| 44 | 0 | $self->sql_strings; | |||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 0 | ▶ | if ( $args->{create_table} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 47 | $dbh->do( $self->sql_strings->{create} ) | ||||||||||||||||||
| 48 | 0 | ▶ | or croak $dbh->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 49 | } | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 0 | return; | |||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 0 | ▶ | sub _table { | ||||||||||||||||
|
Subroutine
_table: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 55 | 0 | my ( $self, ) = @_; | |||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 0 | return $self->table_prefix() . $self->namespace(); | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 0 | ▶ | sub _build_sql_strings { | ||||||||||||||||
|
Subroutine
_build_sql_strings: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 61 | 0 | my ( $self, ) = @_; | |||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | 0 | my $dbh = $self->dbh->(); | |||||||||||||||||
| 64 | 0 | my $table = $dbh->quote_identifier( $self->_table ); | |||||||||||||||||
| 65 | 0 | my $value = $dbh->quote_identifier('value'); | |||||||||||||||||
| 66 | 0 | my $key = $dbh->quote_identifier('key'); | |||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 0 | my $strings = { | |||||||||||||||||
| 69 | fetch => "SELECT $value FROM $table WHERE $key = ?", | ||||||||||||||||||
| 70 | store => "INSERT INTO $table ( $key, $value ) VALUES ( ?, ? )", | ||||||||||||||||||
| 71 | store2 => "UPDATE $table SET $value = ? WHERE $key = ?", | ||||||||||||||||||
| 72 | remove => "DELETE FROM $table WHERE $key = ?", | ||||||||||||||||||
| 73 | clear => "DELETE FROM $table", | ||||||||||||||||||
| 74 | get_keys => "SELECT DISTINCT $key FROM $table", | ||||||||||||||||||
| 75 | create => "CREATE TABLE IF NOT EXISTS $table (" | ||||||||||||||||||
| 76 | . " $key VARCHAR( 300 ), $value TEXT," | ||||||||||||||||||
| 77 | . " PRIMARY KEY ( $key ) )", | ||||||||||||||||||
| 78 | }; | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 0 | ▶ | if ( $self->db_name eq 'MySQL' ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 81 | $strings->{store} = | ||||||||||||||||||
| 82 | 0 | "INSERT INTO $table" | |||||||||||||||||
| 83 | . " ( $key, $value )" | ||||||||||||||||||
| 84 | . " VALUES ( ?, ? )" | ||||||||||||||||||
| 85 | . " ON DUPLICATE KEY UPDATE $value=VALUES($value)"; | ||||||||||||||||||
| 86 | 0 | delete $strings->{store2}; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | elsif ( $self->db_name eq 'SQLite' ) { | ||||||||||||||||||
| 89 | $strings->{store} = | ||||||||||||||||||
| 90 | 0 | "INSERT OR REPLACE INTO $table" | |||||||||||||||||
| 91 | . " ( $key, $value )" | ||||||||||||||||||
| 92 | . " values ( ?, ? )"; | ||||||||||||||||||
| 93 | 0 | delete $strings->{store2}; | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | elsif ( $self->db_name eq 'PostgreSQL' ) { | ||||||||||||||||||
| 96 | $strings->{create} = | ||||||||||||||||||
| 97 | 0 | "CREATE TABLE IF NOT EXISTS $table (" | |||||||||||||||||
| 98 | . " $key BYTEA, $value BYTEA," | ||||||||||||||||||
| 99 | . " PRIMARY KEY ( $key ) )"; | ||||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 0 | return $strings; | |||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 0 | ▶ | sub fetch { | ||||||||||||||||
|
Subroutine
fetch: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||
| 106 | 0 | my ( $self, $key, ) = @_; | |||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 0 | ▶ | my $dbh = $self->has_dbh_ro ? $self->dbh_ro->() : $self->dbh->(); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 109 | my $sth = $dbh->prepare_cached( $self->sql_strings->{fetch} ) | ||||||||||||||||||
| 110 | 0 | ▶ | or croak $dbh->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 111 | 0 | ▶ | if ( $self->db_name eq 'PostgreSQL' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 112 | 0 | $sth->bind_param( 1, undef, { pg_type => DBD::Pg::PG_BYTEA() } ); | |||||||||||||||||
| 113 | } | ||||||||||||||||||
| 114 | 0 | ▶ | $sth->execute($key) or croak $sth->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 115 | 0 | my $results = $sth->fetchall_arrayref; | |||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 0 | return $results->[0]->[0]; | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 0 | ▶ | sub store { | ||||||||||||||||
|
Subroutine
store: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||
| 121 | 0 | my ( $self, $key, $data, ) = @_; | |||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | 0 | my $dbh = $self->dbh->(); | |||||||||||||||||
| 124 | 0 | my $sth = $dbh->prepare_cached( $self->sql_strings->{store} ); | |||||||||||||||||
| 125 | 0 | ▶ | if ( $self->db_name eq 'PostgreSQL' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 126 | 0 | $sth->bind_param( 1, undef, { pg_type => DBD::Pg::PG_BYTEA() } ); | |||||||||||||||||
| 127 | 0 | $sth->bind_param( 2, undef, { pg_type => DBD::Pg::PG_BYTEA() } ); | |||||||||||||||||
| 128 | } | ||||||||||||||||||
| 129 | 0 | ▶ | if ( not $sth->execute( $key, $data ) ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 130 | 0 | ▶ | if ( $self->sql_strings->{store2} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 131 | my $sth = $dbh->prepare_cached( $self->sql_strings->{store2} ) | ||||||||||||||||||
| 132 | 0 | ▶ | or croak $dbh->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 133 | 0 | ▶ | if ( $self->db_name eq 'PostgreSQL' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 134 | 0 | $sth->bind_param( 1, undef, | |||||||||||||||||
| 135 | { pg_type => DBD::Pg::PG_BYTEA() } ); | ||||||||||||||||||
| 136 | 0 | $sth->bind_param( 2, undef, | |||||||||||||||||
| 137 | { pg_type => DBD::Pg::PG_BYTEA() } ); | ||||||||||||||||||
| 138 | } | ||||||||||||||||||
| 139 | 0 | ▶ | $sth->execute( $data, $key ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 140 | or croak $sth->errstr; | ||||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | else { | ||||||||||||||||||
| 143 | 0 | croak $sth->errstr; | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | } | ||||||||||||||||||
| 146 | 0 | $sth->finish; | |||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | 0 | return; | |||||||||||||||||
| 149 | } | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | 0 | ▶ | sub remove { | ||||||||||||||||
|
Subroutine
remove: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||
| 152 | 0 | my ( $self, $key, ) = @_; | |||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | 0 | my $dbh = $self->dbh->(); | |||||||||||||||||
| 155 | my $sth = $dbh->prepare_cached( $self->sql_strings->{remove} ) | ||||||||||||||||||
| 156 | 0 | ▶ | or croak $dbh->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 157 | 0 | ▶ | if ( $self->db_name eq 'PostgreSQL' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 158 | 0 | $sth->bind_param( 1, undef, { pg_type => DBD::Pg::PG_BYTEA() } ); | |||||||||||||||||
| 159 | } | ||||||||||||||||||
| 160 | 0 | ▶ | $sth->execute($key) or croak $sth->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 161 | 0 | $sth->finish; | |||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | 0 | return; | |||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | 0 | ▶ | sub clear { | ||||||||||||||||
|
Subroutine
clear: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 167 | 0 | my ( $self, $key, ) = @_; | |||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | 0 | my $dbh = $self->dbh->(); | |||||||||||||||||
| 170 | my $sth = $dbh->prepare_cached( $self->sql_strings->{clear} ) | ||||||||||||||||||
| 171 | 0 | ▶ | or croak $dbh->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 172 | 0 | ▶ | $sth->execute() or croak $sth->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 173 | 0 | $sth->finish(); | |||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | 0 | return; | |||||||||||||||||
| 176 | } | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | 0 | ▶ | sub get_keys { | ||||||||||||||||
|
Subroutine
get_keys: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||
| 179 | 0 | my ( $self, ) = @_; | |||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | 0 | ▶ | my $dbh = $self->has_dbh_ro ? $self->dbh_ro->() : $self->dbh->(); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 182 | my $sth = $dbh->prepare_cached( $self->sql_strings->{get_keys} ) | ||||||||||||||||||
| 183 | 0 | ▶ | or croak $dbh->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 184 | 0 | ▶ | $sth->execute() or croak $sth->errstr; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 185 | 0 | my $results = $sth->fetchall_arrayref( [0] ); | |||||||||||||||||
| 186 | 0 | $_ = $_->[0] for @{$results}; | |||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | 0 | return @{$results}; | |||||||||||||||||
| 189 | } | ||||||||||||||||||
| 190 | |||||||||||||||||||
| 191 | 0 | ▶ | sub get_namespaces { croak 'not supported' } | ||||||||||||||||
|
Subroutine
get_namespaces: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 192 | |||||||||||||||||||
| 193 | # TODO: For pg see "upsert" - http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE | ||||||||||||||||||
| 194 | |||||||||||||||||||
| 195 | 1 | 1; | |||||||||||||||||
| 196 | |||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||