| 1 | package Database::Join::Catalog; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Params::Get qw(get_params); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use version; our $VERSION = version->declare('0.011.4'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # What a caller needs to know about a database before it opens a table of it | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # through Database::Abstraction, and the one write it may need: a table of | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # rows as a SQLite file. Database::Abstraction opens a table it is told the | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # name of, and Database::Join reads; neither lists the tables a file or a | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # server holds, says what a table's columns are in their declared order or | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # what its key is, or writes a file. An application that used DBI for those | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # would need a driver of its own beside the ones this distribution requires, | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # so they are here. | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | Readonly my $SQLITE_RE => qr/\Adbi:SQLite:/i; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | Readonly my $MYSQL_RE => qr/\Adbi:(?:MariaDB|mysql):/i; | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | Readonly my $DRIVER_RE => qr/\A[A-Za-z][A-Za-z0-9_]*\z/; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | Readonly::Hash my %MESSAGES => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | error_source_required => 'Give a file (a SQLite database) or a dsn', | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | error_source_both => 'Give a file or a dsn, not both', | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | error_driver_unsupported => 'Listing tables is supported for SQLite, MariaDB and MySQL, not "%s"', | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | error_table_required => 'A table name is required', | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | error_columns_required => 'At least one column is required', | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | error_row_not_hash => 'rows[%d] is not a hash of column values', | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | error_file_exists => '"%s" already exists; write_sqlite makes a new file and never replaces one', | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | error_temp_file => 'Cannot make a temporary file: %s', | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | error_no_reason => 'The database driver failed and gave no reason', | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 10 | ▶ | sub _msg { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_msg: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 10 | my ($key, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 10 | ▶ | return @args ? sprintf($MESSAGES{$key}, @args) : $MESSAGES{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # _fail($stage, $why) -- croak a Database::Join::Catalog::Error. | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | ▶ | sub _fail { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fail: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | my ($stage, $why) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | ▶ | $why //= ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$why //= ""
MC/DC0%0/1
$why //= ""
$why
Truth table50%1/2
A
A= $why
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # RaiseError's form: "DBD::SQLite::db prepare failed: no such table: x at FILE line N." | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # and "DBI connect('dsn','user',...) failed: ...", which names the data source. | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 9 | $why =~ s/\A(?:DBD::\w+::\w+ \w+|DBI \w+\(.*?\)) failed: //s; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 9 | $why =~ s/\s+at \S+ line \d+\.?\s*\z//; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 9 | ▶ | $why = _msg('error_no_reason') unless length $why; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | croak(Database::Join::Catalog::Error->new(stage => $stage, message => $why)); | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # _source(\%args) -> ($dsn, $username, $password, $is_sqlite) | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 25 | ▶ | sub _source { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_source: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 25 | my ($p) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 25 | ▶ | croak _msg('error_source_both') if defined $p->{file} && defined $p->{dsn}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $p->{"file"} and defined $p->{"dsn"}
MC/DC100%2/2
defined $p->{"file"} and defined $p->{"dsn"}
defined $p->{"file"}
defined $p->{"dsn"}
Truth table100%3/3
A and B
A= defined $p->{"file"}B= defined $p->{"dsn"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 24 | ▶ | if (defined $p->{file}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 17 | return ("dbi:SQLite:dbname=$p->{file}", '', '', 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | ▶ | croak _msg('error_source_required') unless defined $p->{dsn} && length $p->{dsn}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $p->{"dsn"} and length $p->{"dsn"}
MC/DC50%1/2
defined $p->{"dsn"} and length $p->{"dsn"}
defined $p->{"dsn"}
length $p->{"dsn"}
Truth table66%2/3
A and B
A= defined $p->{"dsn"}B= length $p->{"dsn"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | ▶ | return ($p->{dsn}, $p->{username}, $p->{password}, $p->{dsn} =~ $SQLITE_RE ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # _connect($dsn, $username, $password, $is_sqlite) -> $dbh | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # A SQLite file is opened read-only: connecting to a name that is not there | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # would otherwise make an empty database of it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 21 | ▶ | sub _connect { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 21 | my ($dsn, $username, $password, $is_sqlite) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 21 | my %attr = (RaiseError => 1, PrintError => 0, AutoCommit => 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 21 | ▶ | if ($is_sqlite) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 18 | require DBD::SQLite::Constants; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 18 | $attr{sqlite_open_flags} = DBD::SQLite::Constants::SQLITE_OPEN_READONLY(); | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 21 | my $dbh = eval { DBI->connect($dsn, $username, $password, \%attr) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 21 | ▶ | _fail('connect', $@) unless $dbh; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 18 | return $dbh; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # _query($dbh, $code) -> whatever $code returns; disconnects either way. | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 18 | ▶ | sub _query { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_query: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 18 | my ($dbh, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 18 | my $got = eval { [ $code->() ] }; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 18 | my $why = $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 18 | eval { $dbh->disconnect }; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | 18 | ▶ | _fail('query', $why) unless $got; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 15 | ▶ | return wantarray ? @{$got} : $got->[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 37 | ▶ | sub _params { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_params: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 37 | my ($schema, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 37 | ▶ | shift @args if @args && !ref $args[0] && $args[0] eq __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@args and not ref $args[0] and $args[0] eq __PACKAGE__
@args and not ref $args[0]
MC/DC0%0/3
@args and not ref $args[0] and $args[0] eq __PACKAGE__
@args
not ref $args[0]
$args[0] eq __PACKAGE__
Truth table75%3/4
A and not(B) and C
A= @argsB= not ref $args[0]C= $args[0] eq __PACKAGE__
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | return validate_strict( | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | schema => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | file => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | dsn => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | username => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | password => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 37 | ▶ | %{$schema}, | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
get_params(undef, \@args) // {}
MC/DC100%1/1
get_params(undef, \@args) // {}
get_params(undef, \@args)
Truth table100%2/2
A
A= get_params(undef, \@args)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | input => get_params(undef, \@args) // {}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | sub available_driver { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
available_driver: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | my ($class, @names) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | local ($@, $!); | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | for my $name (@names) { | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | ▶ | next unless defined $name && $name =~ $DRIVER_RE; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and $name =~ /$DRIVER_RE/
MC/DC50%1/2
defined $name and $name =~ /$DRIVER_RE/
defined $name
$name =~ /$DRIVER_RE/
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /$DRIVER_RE/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | my $module = "DBD/$name.pm"; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | ▶ | return $name if eval { require $module; 1 }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 7 | ▶ | sub schema { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schema: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | local ($@, $!); | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 7 | my $p = _params({}, @args); | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 7 | my ($dsn, $username, $password, $is_sqlite) = _source($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 7 | ▶ | croak _msg('error_driver_unsupported', ($dsn =~ /\Adbi:([^:]*)/i)[0] // $dsn) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$is_sqlite or $dsn =~ /$MYSQL_RE/i
($dsn =~ /\Adbi:([^:]*)/i)[0] // $dsn
MC/DC100%2/2
$is_sqlite or $dsn =~ /$MYSQL_RE/i
$is_sqlite
$dsn =~ /$MYSQL_RE/i
MC/DC0%0/2
($dsn =~ /\Adbi:([^:]*)/i)[0] // $dsn
($dsn =~ /\Adbi:([^:]*)/i)[0]
$dsn
Truth table100%3/3
A or B
A= $is_sqliteB= $dsn =~ /$MYSQL_RE/i
Truth table33%1/3
A // B
A= ($dsn =~ /\Adbi:([^:]*)/i)[0]B= $dsn
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | unless $is_sqlite || $dsn =~ $MYSQL_RE; | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 6 | my $dbh = _connect($dsn, $username, $password, $is_sqlite); | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | ▶ | return _query($dbh, sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 5 | my @tables; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 5 | ▶ | if ($is_sqlite) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | my $names = $dbh->selectcol_arrayref(q{SELECT name FROM sqlite_master } | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | . q{WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | ▶ | for my $name (@{ $names // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$names // []
MC/DC0%0/1
$names // []
$names
Truth table50%1/2
A
A= $names
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 7 | my $info = $dbh->selectall_arrayref('PRAGMA table_info(' . $dbh->quote_identifier($name) . ')', | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | { Slice => {} }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | ▶ | push @tables, { name => $name, columns => [ map { $_->{name} } @{ $info // [] } ] }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info // []
MC/DC0%0/1
$info // []
$info
Truth table50%1/2
A
A= $info
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | my $rows = $dbh->selectall_arrayref(q{SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.COLUMNS } | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | . q{WHERE TABLE_SCHEMA = DATABASE() ORDER BY TABLE_NAME, ORDINAL_POSITION}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | ▶ | for my $row (@{ $rows // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rows // []
MC/DC0%0/1
$rows // []
$rows
Truth table50%1/2
A
A= $rows
| |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 6 | my ($name, $column) = @{$row}; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 6 | ▶ | push @tables, ($seen{$name} = { name => $name, columns => [] }) unless $seen{$name}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 6 | push @{ $seen{$name}{columns} }, $column; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 5 | return \@tables; | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | 5 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 12 | ▶ | sub tables { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tables: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 12 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 12 | local ($@, $!); | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | 12 | my $p = _params({}, @args); | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 12 | my ($dsn, $username, $password, $is_sqlite) = _source($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 10 | ▶ | return [ map { $_->{name} } @{ $class->schema(%{$p}) } ] unless $is_sqlite; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 9 | my $dbh = _connect($dsn, $username, $password, 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 7 | ▶ | return _query($dbh, sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 7 | ▶ | return $dbh->selectcol_arrayref(q{SELECT name FROM sqlite_master } | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dbh->selectcol_arrayref("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name") // []
MC/DC0%0/1
$dbh->selectcol_arrayref("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name") // []
$dbh->selectcol_arrayref("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name")
Truth table50%1/2
A
A= $dbh->selectcol_arrayref("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | . q{WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name}) // []; | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 7 | ▶ | sub table_info { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table_info: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | local ($@, $!); | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | 7 | my $p = _params({ table => { type => 'string', optional => 1 } }, @args); | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | 7 | ▶ | croak _msg('error_table_required') unless defined $p->{table} && length $p->{table}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $p->{"table"} and length $p->{"table"}
MC/DC50%1/2
defined $p->{"table"} and length $p->{"table"}
defined $p->{"table"}
length $p->{"table"}
Truth table66%2/3
A and B
A= defined $p->{"table"}B= length $p->{"table"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | my ($dsn, $username, $password, $is_sqlite) = _source($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 6 | my $dbh = _connect($dsn, $username, $password, $is_sqlite); | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 6 | ▶ | return _query($dbh, sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 6 | my $sth = $dbh->prepare('SELECT * FROM ' . $dbh->quote_identifier($p->{table}) . ' WHERE 1=0'); | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 4 | $sth->execute; | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | 4 | ▶ | my @columns = @{ $sth->{NAME} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$sth->{"NAME"} // []
MC/DC0%0/1
$sth->{"NAME"} // []
$sth->{"NAME"}
Truth table50%1/2
A
A= $sth->{"NAME"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 4 | ▶ | $sth->finish if $sth->can('finish'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 4 | my @primary = grep { defined } eval { $dbh->primary_key(undef, undef, $p->{table}) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | return { columns => \@columns, primary_key => \@primary }; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 6 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # _write($file, \%args) -- the table and its rows, into a new SQLite file. | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 6 | ▶ | sub _write { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 6 | my ($file, $p) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | 6 | my $columns = $p->{columns}; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 6 | ▶ | my $table = $p->{table} // 'data'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$p->{"table"} // "data"
MC/DC100%1/1
$p->{"table"} // "data"
$p->{"table"}
Truth table100%2/2
A
A= $p->{"table"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 6 | my $dbh = eval { DBI->connect("dbi:SQLite:dbname=$file", '', '', { RaiseError => 1, PrintError => 0, AutoCommit => 1 }) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 6 | ▶ | _fail('connect', $@) unless $dbh; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 5 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 5 | my $t = $dbh->quote_identifier($table); | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 5 | my @c = map { $dbh->quote_identifier($_) } @{$columns}; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 5 | $dbh->do("CREATE TABLE $t (" . join(', ', map { "$_ TEXT" } @c) . ')'); | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 3 | ▶ | if (@{ $p->{rows} }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 3 | $dbh->begin_work; | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | my $sth = $dbh->prepare("INSERT INTO $t (" . join(', ', @c) . ') VALUES (' . join(', ', ('?') x @c) . ')'); | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | 3 | for my $row (@{ $p->{rows} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | $sth->execute(map { $row->{$_} } @{$columns}); | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 3 | $dbh->commit; | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 5 | my $why = $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | eval { $dbh->rollback } unless $ok || $dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ok or $dbh->{"AutoCommit"}
MC/DC0%0/2
$ok or $dbh->{"AutoCommit"}
$ok
$dbh->{"AutoCommit"}
Truth table66%2/3
A or B
A= $okB= $dbh->{"AutoCommit"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | eval { $dbh->disconnect }; | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 5 | ▶ | _fail('query', $why) unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 11 | ▶ | sub _write_params { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_params: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 11 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | 11 | my $p = _params({ | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | table => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | columns => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | rows => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | }, @args); | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 11 | ▶ | croak _msg('error_columns_required') unless ref $p->{columns} eq 'ARRAY' && @{ $p->{columns} }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $p->{"columns"} eq "ARRAY" and @{$p->{"columns"};}
MC/DC50%1/2
ref $p->{"columns"} eq "ARRAY" and @{$p->{"columns"};}
ref $p->{"columns"} eq "ARRAY"
@{$p->{"columns"};}
Truth table66%2/3
A and B
A= ref $p->{"columns"} eq "ARRAY"B= @{$p->{"columns"};}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 10 | ▶ | croak _msg('error_table_required') if defined $p->{table} && !length $p->{table}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $p->{"table"} and not length $p->{"table"}
MC/DC100%2/2
defined $p->{"table"} and not length $p->{"table"}
defined $p->{"table"}
not length $p->{"table"}
Truth table100%3/3
A and not(B)
A= defined $p->{"table"}B= not length $p->{"table"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 9 | ▶ | $p->{rows} //= []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$p->{"rows"} //= []
MC/DC100%1/1
$p->{"rows"} //= []
$p->{"rows"}
Truth table100%2/2
A
A= $p->{"rows"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 9 | for my $i (0 .. $#{ $p->{rows} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 226 | 7 | ▶ | croak _msg('error_row_not_hash', $i) unless ref $p->{rows}[$i] eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 8 | return $p; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 5 | ▶ | sub write_sqlite { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_sqlite: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 5 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | 5 | local ($@, $!); | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | my $p = _write_params(@args); | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | 5 | ▶ | croak _msg('error_source_required') unless defined $p->{file} && length $p->{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $p->{"file"} and length $p->{"file"}
MC/DC50%1/2
defined $p->{"file"} and length $p->{"file"}
defined $p->{"file"}
length $p->{"file"}
Truth table66%2/3
A and B
A= defined $p->{"file"}B= length $p->{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 4 | ▶ | croak _msg('error_file_exists', $p->{file}) if -e $p->{file} || -l $p->{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $p->{"file"} or -l $p->{"file"}
MC/DC50%1/2
-e $p->{"file"} or -l $p->{"file"}
-e $p->{"file"}
-l $p->{"file"}
Truth table66%2/3
A or B
A= -e $p->{"file"}B= -l $p->{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 3 | my $ok = eval { _write($p->{file}, $p) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | 3 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | 3 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | unlink $p->{file}; | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | croak $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 6 | ▶ | sub sqlite_bytes { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sqlite_bytes: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 6 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | 6 | local ($@, $!); | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | 6 | my $p = _write_params(@args); | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | # EXLOCK => 0: on a BSD, File::Temp's lock stops SQLite writing the file. | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | my ($fh, $file) = eval { tempfile('database-join-XXXXXXXX', SUFFIX => '.db', TMPDIR => 1, UNLINK => 0, EXLOCK => 0) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | ▶ | croak _msg('error_temp_file', $@ || $!) unless $fh; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ || $!
MC/DC0%0/2
$@ || $!
$@
$!
Truth table0%0/3
A || B
A= $@B= $!
| |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 3 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | 3 | my $ok = eval { _write($file, $p) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | 3 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 1 | unlink $file; | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | 1 | croak $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | my $bytes = do { | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | open(my $in, '<:raw', $file) or do { my $e = $!; unlink $file; croak _msg('error_temp_file', $e) }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | local $/; | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | my $b = <$in>; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | close $in; | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | $b; | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | unlink $file; | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | 2 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | package Database::Join::Catalog::Error; | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | use overload '""' => sub { $_[0]->{message} }, bool => sub { 1 }, fallback => 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
__ANON__: called CC=1 SCAR=0.1
Subroutine
__ANON__: not called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 9 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 9 | return bless { stage => $args{stage}, message => $args{message} }, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 7 | ▶ | sub stage { return $_[0]->{stage} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | sub message { return $_[0]->{message} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||