TER1 (Statement): 97.30%
TER2 (Branch): 90.62%
TER3 (LCSAJ): 100.0% (6/6)
Approximate LCSAJ segments: 65
โ Covered โ this LCSAJ path was executed during testing.
โ Not covered โ this LCSAJ path was never executed. These are the paths to focus on.
Multiple dots on a line indicate that multiple control-flow paths begin at that line. Hovering over any dot shows:
start โ end โ jump
Uncovered paths show [NOT COVERED] in the tooltip.
1: package Database::Join::Catalog; 2: 3: use 5.010001; 4: use strict; 5: use warnings; 6: 7: use Carp qw(croak); 8: use DBI; 9: use File::Temp qw(tempfile); 10: use Params::Get qw(get_params); 11: use Params::Validate::Strict qw(validate_strict); 12: use Readonly; 13: use Scalar::Util qw(blessed); 14: 15: use version; our $VERSION = version->declare('0.011.4'); 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: Readonly my $SQLITE_RE => qr/\Adbi:SQLite:/i; 27: Readonly my $MYSQL_RE => qr/\Adbi:(?:MariaDB|mysql):/i; 28: Readonly my $DRIVER_RE => qr/\A[A-Za-z][A-Za-z0-9_]*\z/; 29: 30: 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: sub _msg { 43: my ($key, @args) = @_; 44: return @args ? sprintf($MESSAGES{$key}, @args) : $MESSAGES{$key};Mutants (Total: 2, Killed: 2, Survived: 0)
45: } 46: 47: # _fail($stage, $why) -- croak a Database::Join::Catalog::Error. 48: sub _fail { 49: my ($stage, $why) = @_; 50: $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: $why =~ s/\A(?:DBD::\w+::\w+ \w+|DBI \w+\(.*?\)) failed: //s; 54: $why =~ s/\s+at \S+ line \d+\.?\s*\z//; 55: $why = _msg('error_no_reason') unless length $why; 56: croak(Database::Join::Catalog::Error->new(stage => $stage, message => $why)); 57: } 58: 59: # _source(\%args) -> ($dsn, $username, $password, $is_sqlite) 60: sub _source { โ61 โ 63 โ 66 61: my ($p) = @_; 62: croak _msg('error_source_both') if defined $p->{file} && defined $p->{dsn}; 63: if (defined $p->{file}) {
Mutants (Total: 1, Killed: 1, Survived: 0)
64: return ("dbi:SQLite:dbname=$p->{file}", '', '', 1); 65: } 66: croak _msg('error_source_required') unless defined $p->{dsn} && length $p->{dsn}; 67: return ($p->{dsn}, $p->{username}, $p->{password}, $p->{dsn} =~ $SQLITE_RE ? 1 : 0); 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: sub _connect { โ75 โ 77 โ 81 75: my ($dsn, $username, $password, $is_sqlite) = @_; 76: my %attr = (RaiseError => 1, PrintError => 0, AutoCommit => 1); 77: if ($is_sqlite) {
Mutants (Total: 1, Killed: 1, Survived: 0)
78: require DBD::SQLite::Constants; 79: $attr{sqlite_open_flags} = DBD::SQLite::Constants::SQLITE_OPEN_READONLY(); 80: } 81: my $dbh = eval { DBI->connect($dsn, $username, $password, \%attr) }; 82: _fail('connect', $@) unless $dbh; 83: return $dbh;
Mutants (Total: 2, Killed: 2, Survived: 0)
84: } 85: 86: # _query($dbh, $code) -> whatever $code returns; disconnects either way. 87: sub _query { 88: my ($dbh, $code) = @_; 89: my $got = eval { [ $code->() ] }; 90: my $why = $@; 91: eval { $dbh->disconnect }; 92: _fail('query', $why) unless $got; 93: return wantarray ? @{$got} : $got->[0];
Mutants (Total: 2, Killed: 2, Survived: 0)
94: } 95: 96: sub _params { 97: my ($schema, @args) = @_; 98: shift @args if @args && !ref $args[0] && $args[0] eq __PACKAGE__; 99: return validate_strict(
Mutants (Total: 2, Killed: 2, Survived: 0)
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: %{$schema}, 106: }, 107: input => get_params(undef, \@args) // {}, 108: ); 109: } 110: 111: sub available_driver { โ112 โ 114 โ 119 112: my ($class, @names) = @_; 113: local ($@, $!); 114: for my $name (@names) { 115: next unless defined $name && $name =~ $DRIVER_RE; 116: my $module = "DBD/$name.pm"; 117: return $name if eval { require $module; 1 };
Mutants (Total: 2, Killed: 2, Survived: 0)
118: } 119: return undef;
Mutants (Total: 2, Killed: 2, Survived: 0)
120: } 121: 122: sub schema { 123: my ($class, @args) = @_; 124: local ($@, $!); 125: my $p = _params({}, @args); 126: my ($dsn, $username, $password, $is_sqlite) = _source($p); 127: croak _msg('error_driver_unsupported', ($dsn =~ /\Adbi:([^:]*)/i)[0] // $dsn) 128: unless $is_sqlite || $dsn =~ $MYSQL_RE; 129: 130: my $dbh = _connect($dsn, $username, $password, $is_sqlite); 131: return _query($dbh, sub {
Mutants (Total: 2, Killed: 2, Survived: 0)
132: my @tables; 133: if ($is_sqlite) {
Mutants (Total: 1, Killed: 1, Survived: 0)
134: 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: for my $name (@{ $names // [] }) { 137: my $info = $dbh->selectall_arrayref('PRAGMA table_info(' . $dbh->quote_identifier($name) . ')', 138: { Slice => {} }); 139: push @tables, { name => $name, columns => [ map { $_->{name} } @{ $info // [] } ] }; 140: } 141: } else { 142: 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: my %seen; 145: for my $row (@{ $rows // [] }) { 146: my ($name, $column) = @{$row}; 147: push @tables, ($seen{$name} = { name => $name, columns => [] }) unless $seen{$name}; 148: push @{ $seen{$name}{columns} }, $column; 149: } 150: } 151: return \@tables;
Mutants (Total: 2, Killed: 2, Survived: 0)
152: }); 153: } 154: 155: sub tables { 156: my ($class, @args) = @_; 157: local ($@, $!); 158: my $p = _params({}, @args); 159: my ($dsn, $username, $password, $is_sqlite) = _source($p); 160: return [ map { $_->{name} } @{ $class->schema(%{$p}) } ] unless $is_sqlite; 161: 162: my $dbh = _connect($dsn, $username, $password, 1); 163: return _query($dbh, sub {
Mutants (Total: 2, Killed: 2, Survived: 0)
164: return $dbh->selectcol_arrayref(q{SELECT name FROM sqlite_master }
Mutants (Total: 2, Killed: 2, Survived: 0)
165: . q{WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name}) // []; 166: }); 167: } 168: 169: sub table_info { 170: my ($class, @args) = @_; 171: local ($@, $!); 172: my $p = _params({ table => { type => 'string', optional => 1 } }, @args); 173: croak _msg('error_table_required') unless defined $p->{table} && length $p->{table}; 174: my ($dsn, $username, $password, $is_sqlite) = _source($p); 175: 176: my $dbh = _connect($dsn, $username, $password, $is_sqlite); 177: return _query($dbh, sub {
Mutants (Total: 2, Killed: 2, Survived: 0)
178: my $sth = $dbh->prepare('SELECT * FROM ' . $dbh->quote_identifier($p->{table}) . ' WHERE 1=0'); 179: $sth->execute; 180: my @columns = @{ $sth->{NAME} // [] }; 181: $sth->finish if $sth->can('finish'); 182: my @primary = grep { defined } eval { $dbh->primary_key(undef, undef, $p->{table}) }; 183: return { columns => \@columns, primary_key => \@primary }; 184: }); 185: } 186: 187: # _write($file, \%args) -- the table and its rows, into a new SQLite file. 188: sub _write { 189: my ($file, $p) = @_; 190: my $columns = $p->{columns}; 191: my $table = $p->{table} // 'data'; 192: my $dbh = eval { DBI->connect("dbi:SQLite:dbname=$file", '', '', { RaiseError => 1, PrintError => 0, AutoCommit => 1 }) }; 193: _fail('connect', $@) unless $dbh; 194: my $ok = eval { 195: my $t = $dbh->quote_identifier($table); 196: my @c = map { $dbh->quote_identifier($_) } @{$columns}; 197: $dbh->do("CREATE TABLE $t (" . join(', ', map { "$_ TEXT" } @c) . ')'); 198: if (@{ $p->{rows} }) {
Mutants (Total: 1, Killed: 1, Survived: 0)
199: $dbh->begin_work; 200: my $sth = $dbh->prepare("INSERT INTO $t (" . join(', ', @c) . ') VALUES (' . join(', ', ('?') x @c) . ')'); 201: for my $row (@{ $p->{rows} }) { 202: $sth->execute(map { $row->{$_} } @{$columns}); 203: } 204: $dbh->commit; 205: } 206: 1; 207: }; 208: my $why = $@; 209: eval { $dbh->rollback } unless $ok || $dbh->{AutoCommit}; 210: eval { $dbh->disconnect }; 211: _fail('query', $why) unless $ok; 212: return 1;
Mutants (Total: 2, Killed: 2, Survived: 0)
213: } 214: 215: sub _write_params { โ216 โ 225 โ 228 216: my @args = @_; 217: my $p = _params({ 218: table => { type => 'string', optional => 1 }, 219: columns => { type => 'arrayref', optional => 1 }, 220: rows => { type => 'arrayref', optional => 1 }, 221: }, @args); 222: croak _msg('error_columns_required') unless ref $p->{columns} eq 'ARRAY' && @{ $p->{columns} }; 223: croak _msg('error_table_required') if defined $p->{table} && !length $p->{table}; 224: $p->{rows} //= []; 225: for my $i (0 .. $#{ $p->{rows} }) { 226: croak _msg('error_row_not_hash', $i) unless ref $p->{rows}[$i] eq 'HASH'; 227: } 228: return $p;
Mutants (Total: 2, Killed: 2, Survived: 0)
229: } 230: 231: sub write_sqlite { โ232 โ 239 โ 243 232: my ($class, @args) = @_; 233: local ($@, $!); 234: my $p = _write_params(@args); 235: croak _msg('error_source_required') unless defined $p->{file} && length $p->{file}; 236: croak _msg('error_file_exists', $p->{file}) if -e $p->{file} || -l $p->{file}; 237: my $ok = eval { _write($p->{file}, $p) }; 238: my $err = $@; 239: unless ($ok) {
Mutants (Total: 1, Killed: 1, Survived: 0)
240: unlink $p->{file}; 241: croak $err; 242: } 243: return 1;
Mutants (Total: 2, Killed: 2, Survived: 0)
244: } 245: 246: sub sqlite_bytes { โ247 โ 256 โ 260 247: my ($class, @args) = @_; 248: local ($@, $!); 249: my $p = _write_params(@args); 250: # EXLOCK => 0: on a BSD, File::Temp's lock stops SQLite writing the file. 251: my ($fh, $file) = eval { tempfile('database-join-XXXXXXXX', SUFFIX => '.db', TMPDIR => 1, UNLINK => 0, EXLOCK => 0) }; 252: croak _msg('error_temp_file', $@ || $!) unless $fh; 253: close $fh; 254: my $ok = eval { _write($file, $p) }; 255: my $err = $@; 256: unless ($ok) {
Mutants (Total: 1, Killed: 1, Survived: 0)
257: unlink $file; 258: croak $err; 259: } 260: my $bytes = do { 261: open(my $in, '<:raw', $file) or do { my $e = $!; unlink $file; croak _msg('error_temp_file', $e) }; 262: local $/; 263: my $b = <$in>; 264: close $in; 265: $b; 266: }; 267: unlink $file; 268: return $bytes;
Mutants (Total: 2, Killed: 2, Survived: 0)
269: } 270: 271: package Database::Join::Catalog::Error; 272: 273: use strict; 274: use warnings; 275: use overload '""' => sub { $_[0]->{message} }, bool => sub { 1 }, fallback => 1; 276: 277: sub new { 278: my ($class, %args) = @_; 279: return bless { stage => $args{stage}, message => $args{message} }, $class;
Mutants (Total: 2, Killed: 2, Survived: 0)
280: } 281: 282: sub stage { return $_[0]->{stage} }
Mutants (Total: 2, Killed: 2, Survived: 0)
283: sub message { return $_[0]->{message} }
284: 285: 1; 286: 287: __END__ 288: 289: =head1 NAME 290: 291: Database::Join::Catalog - List a database's tables and columns, and write a table as SQLite 292: 293: =head1 VERSION 294: 295: 0.011.4 296: 297: =head1 SYNOPSIS 298: 299: use Database::Join::Catalog; 300: 301: my $names = Database::Join::Catalog->tables(file => 'store.db'); # [ 'customers', 'orders' ] 302: my $all = Database::Join::Catalog->schema(file => 'store.db'); 303: # [ { name => 'customers', columns => [ 'entry', 'name' ] }, ... ] 304: 305: my $info = Database::Join::Catalog->table_info( 306: dsn => 'dbi:MariaDB:database=shop;host=db', username => 'ro', password => '...', 307: table => 'orders', 308: ); 309: # { columns => [ 'entry', 'item', 'qty' ], primary_key => [ 'entry' ] } 310: 311: my $bytes = Database::Join::Catalog->sqlite_bytes( 312: table => 'data', columns => [ 'id', 'name' ], rows => [ { id => 1, name => 'a' } ], 313: ); 314: 315: my $driver = Database::Join::Catalog->available_driver('MariaDB', 'mysql'); # or undef 316: 317: =head1 DESCRIPTION 318: 319: L<Database::Abstraction> opens a table it is told the name of, and 320: L<Database::Join> reads across such tables. Neither says which tables a 321: database holds, what a table's columns are in their declared order, or what 322: its key is, and neither writes a file. This module does those, so that an 323: application reads and describes its data through this distribution and needs 324: no database driver of its own. 325: 326: Every method is a class method. A database is named either by C<file>, a 327: SQLite file, which is opened read-only (so a name that is not there is not 328: made into an empty database), or by C<dsn> with optional C<username> and 329: C<password>. Listing tables works for SQLite, MariaDB and MySQL. 330: 331: A database that cannot be reached, or a query it refuses, croaks with a 332: L</Database::Join::Catalog::Error>, which says which of the two it was. 333: Arguments that are wrong croak with a plain message. 334: 335: =head1 METHODS 336: 337: =head2 tables 338: 339: my $names = Database::Join::Catalog->tables(file => $path); 340: 341: =head3 API SPECIFICATION 342: 343: =head4 INPUT 344: 345: file: string, a SQLite file; or 346: dsn: string, a DBI data source (SQLite, MariaDB, MySQL) 347: username: string, optional (dsn only) 348: password: string, optional (dsn only) 349: 350: =head4 OUTPUT 351: 352: An arrayref of the table names, sorted by name. For SQLite, the user tables 353: (not C<sqlite_*>); for MariaDB and MySQL, those of the connection's database. 354: A file that is not a SQLite database croaks at stage C<query>; one that is not 355: there, at stage C<connect>. 356: 357: =head2 schema 358: 359: my $tables = Database::Join::Catalog->schema(dsn => $dsn, username => $u, password => $p); 360: 361: =head3 API SPECIFICATION 362: 363: =head4 INPUT 364: 365: As L</tables>. 366: 367: =head4 OUTPUT 368: 369: An arrayref of C<< { name => $table, columns => [ $column, ... ] } >>, sorted by 370: table name, each table's columns in their declared order. Croaks as 371: L</tables>, and with C<error_driver_unsupported> for another driver. 372: 373: =head2 table_info 374: 375: my $info = Database::Join::Catalog->table_info(dsn => $dsn, table => 'orders'); 376: 377: =head3 API SPECIFICATION 378: 379: =head4 INPUT 380: 381: file or dsn, username, password: as tables 382: table: string, required 383: 384: =head4 OUTPUT 385: 386: C<< { columns => [ ... ], primary_key => [ ... ] } >>: the columns in the 387: table's declared order (L<Database::Abstraction>'s C<columns()> is 388: alphabetical), and the primary key's columns, empty when it has none or the 389: driver does not say. A database that cannot be reached croaks at stage 390: C<connect>; a table it does not have, at stage C<query>. 391: 392: =head2 write_sqlite 393: 394: Database::Join::Catalog->write_sqlite(file => 'out.db', table => 'data', 395: columns => [ 'id', 'name' ], rows => \@rows); 396: 397: =head3 API SPECIFICATION 398: 399: =head4 INPUT 400: 401: file: string, required; a name that does not exist yet 402: table: string, optional (default 'data') 403: columns: arrayref of strings, required, at least one 404: rows: arrayref of hashrefs, optional; each row's values by column name 405: 406: =head4 OUTPUT 407: 408: 1. Makes a SQLite file with one table whose columns are TEXT, in the order 409: given, and one row for each element of C<rows> (a missing value is NULL). 410: Names are quoted, so any column name is safe. The rows go in one transaction. 411: Croaks with C<error_file_exists> rather than replace a file, and leaves no 412: file behind when the write fails. 413: 414: =head2 sqlite_bytes 415: 416: my $bytes = Database::Join::Catalog->sqlite_bytes(columns => \@columns, rows => \@rows); 417: 418: =head3 API SPECIFICATION 419: 420: =head4 INPUT 421: 422: As L</write_sqlite>, without C<file>. 423: 424: =head4 OUTPUT 425: 426: The SQLite file's contents, made in a temporary file that is removed 427: afterwards, whether or not the write succeeds. 428: 429: =head2 available_driver 430: 431: my $driver = Database::Join::Catalog->available_driver('MariaDB', 'mysql'); 432: 433: =head3 API SPECIFICATION 434: 435: =head4 INPUT 436: 437: @names: DBD driver names, in order of preference 438: 439: =head4 OUTPUT 440: 441: The first whose C<DBD::> module loads, or undef. A name that is not a 442: driver name is skipped, never loaded. 443: 444: =head1 Database::Join::Catalog::Error 445: 446: What a failed C<connect> or C<query> croaks with. It reads as the driver's 447: message (C<"$err">), and: 448: 449: $err->stage # 'connect' or 'query' 450: $err->message # the driver's message 451: 452: =head1 MESSAGES 453: 454: error_source_required -- neither file nor dsn (or write_sqlite without file) 455: error_source_both -- both file and dsn 456: error_driver_unsupported -- schema() or tables() for a driver other than SQLite, MariaDB, MySQL 457: error_table_required -- table_info() without a table, or an empty table name 458: error_columns_required -- write_sqlite() or sqlite_bytes() without columns 459: error_row_not_hash -- an element of rows is not a hashref 460: error_file_exists -- write_sqlite() to a name that exists 461: error_temp_file -- sqlite_bytes() could not make its temporary file 462: error_no_reason -- the driver failed without a message (connect returned undef) 463: 464: =head1 SEE ALSO 465: 466: L<Database::Join>, L<Database::Abstraction> 467: 468: =head1 AUTHOR 469: 470: Nigel Horne, C<< <njh@nigelhorne.com> >> 471: 472: =head1 LICENSE AND COPYRIGHT 473: 474: Copyright (C) 2026 Nigel Horne. 475: 476: Usage is subject to the GPL2 licence terms. 477: If you use it, please let me know. 478: 479: =cutMutants (Total: 2, Killed: 0, Survived: 2)
- BOOL_NEGATE_283_15: Negate boolean return expression
MEDIUM: Add tests asserting both true and false outcomes๐งช Suggested Test# Boolean branch test suggestion ok( !func(INPUT), 'Verify boolean branch behaviour' );- RETURN_UNDEF_283_15: Replace return expression with undef
LOW: Mutation survived, but impact may be minor๐งช Suggested Test# Return value assertion is( func(INPUT), EXPECTED, 'Verify correct return value' );