blib/lib/Database/BI/Model/DataSource.pm

filter: Statementstmt 74.2% 680 / 916 Branchbran 62.5% 300 / 480 Conditioncond 53.6% 161 / 300 MC/DCmcdc 37.2% 66 / 177 Subroutinesub 92.1% 70 / 76 Podpod 100% 17 / 17 totaltotal 65.8% 1294 / 1966 CC 365 SCAR 50.7
CC
365
Cov
67%
_detect_file_info
814.9
_json_table
462.0
_json_key_order
420.0
CRAP 159.0
« blib/lib/Database/BI/Model/Boundary.pm ↑ Summary blib/lib/Database/BI/SafeUA.pm »
1 package Database::BI::Model::DataSource;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Database::BI::I18N ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Sub::Protected;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Params::Get ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 our $VERSION = '0.023.1';
17  
18 =head1 NAME
19  
20 Database::BI::Model::DataSource - Table-agnostic adapter around Database::Abstraction
21  
22 =head1 VERSION
23  
24 0.023.1
25  
26 =head1 SYNOPSIS
27  
28 B<Read all rows from a CSV file:>
29  
30 use Database::BI::Model::DataSource;
31  
32 my $source = Database::BI::Model::DataSource->new(
33 directory => '/path/to/data',
34 table => 'sales', # looks for data/sales.csv, tsv, .psv, .sql, .xml, etc.
35 );
36  
37 my $records = $source->fetch_all; # arrayref of hashrefs -- one hashref per row
38  
39 for my $row (@{$records}) {
40 printf "Product: %s, Amount: %s\n", $row->{product}, $row->{amount};
41 }
42  
43 B<Get column names in the original file order (CSV/PSV/TSV only):>
44  
45 my $cols = $source->columns; # returns undef for SQLite and XML
46 if ($cols) {
47 print join(', ', @{$cols}), "\n";
48 }
49  
50 B<Find out which column is the primary key:>
51  
52 print "Primary key column: ", $source->id_column, "\n";
53  
54 B<Open a SQLite file (.sql extension):>
55  
56 my $source = Database::BI::Model::DataSource->new(
57 directory => '/var/data',
58 table => 'inventory', # looks for /var/data/inventory.sql
59 );
60  
61 B<Open a pipe-separated file (.psv extension):>
62  
63 my $source = Database::BI::Model::DataSource->new(
64 directory => '/var/data',
65 table => 'products', # looks for /var/data/products.psv
66 );
67  
68 B<Open a tab-separated file (.tsv extension):>
69  
70 my $source = Database::BI::Model::DataSource->new(
71 directory => '/var/data',
72 table => 'products', # looks for /var/data/products.tsv
73 );
74  
75 B<Use a custom i18n object to translate error messages:>
76  
77 # The i18n object must have a maketext($key, @args) method.
78 my $source = Database::BI::Model::DataSource->new(
79 directory => '/path/to/data',
80 table => 'sales',
81 i18n => My::I18N::Handle->new,
82 );
83  
84 B<Open a remote HTML table from a URL:>
85  
86 # Requires LWP::UserAgent::Cached and HTML::TableExtract.
87 # The table is fetched and cached in memory; no file is saved to disk.
88 my $source = Database::BI::Model::DataSource->new(
89 url => 'https://example.com/data-page.html',
90 );
91 my $records = $source->fetch_all;
92  
93 B<Select a specific table when a page has more than one HTML table:>
94  
95 my $source = Database::BI::Model::DataSource->new(
96 url => 'https://example.com/page.html',
97 html_table_index => 2, # zero-based: 0 = first table, 2 = third table
98 );
99  
100 B<Fetch from an intranet host, which resolves to a private address:>
101  
102 my $source = Database::BI::Model::DataSource->new(
103 url => 'https://reports.corp.example/sales.html',
104 private_hosts => ['*.corp.example'],
105 );
106  
107 B<Open one specific internal table from a multi-table SQLite database:>
108  
109 # A SQLite file may contain more than one table.
110 # Use internal_table to say which one you want.
111 my $source = Database::BI::Model::DataSource->new(
112 directory => '/var/data',
113 table => 'store', # looks for store.db, store.sql, etc.
114 internal_table => 'customer', # open just the "customer" table inside
115 );
116 my $records = $source->fetch_all;
117  
118 B<Check whether a SQLite file has more than one table:>
119  
120 my $source = Database::BI::Model::DataSource->new(
121 directory => '/var/data',
122 table => 'store',
123 );
124 my $tbl_list = $source->sqlite_tables; # undef unless it is a multi-table SQLite
125 if ($tbl_list && @{$tbl_list} > 1) {
126 # let the user choose which table to open
127 for my $name (@{$tbl_list}) {
128 printf " table: %s\n", $name;
129 }
130 }
131  
132 B<Open a remote file over SSH (SFTP):>
133  
134 # Requires Net::SFTP::Foreign.
135 my $source = Database::BI::Model::DataSource->new(
136 directory => '/remote/data',
137 table => 'report',
138 host => 'deploy@dbserver.example.com',
139 file_ext => 'log', # hint: try report.log first, then other exts
140 );
141 my $records = $source->fetch_all;
142  
143 B<Handle errors gracefully:>
144  
145 my $source = eval {
146 Database::BI::Model::DataSource->new(
147 directory => $dir,
148 table => $table,
149 );
150 };
151 if ($@) {
152 carp "Could not open table: $@";
153 # $@ contains a translated message from %MESSAGES
154 }
155  
156 my $records = eval { $source->fetch_all };
157 if ($@) {
158 carp "Could not read records: $@";
159 }
160  
161 =head1 DESCRIPTION
162  
163 C<Database::BI::Model::DataSource> is a thin, table-agnostic adapter that
164 wraps L<Database::Abstraction> and exposes three accessors (C<fetch_all>,
165 C<columns>, C<id_column>) used by the controller.
166  
167 L<Database::Abstraction> is a read-only ORM that discovers data files
168 (CSV, PSV, TSV, SQLite, XML, etc.) automatically from a directory based on the
169 calling class name. C<DataSource> generates an ephemeral subclass at
170 construction time so callers never interact with L<Database::Abstraction>
171 directly. To swap the backend for L<Database::Join> in Phase 2, only the
172 C<open_table> helper in C<Database::BI> needs to change; the controller and
173 C<DataSource> are untouched.
174  
175 C<_detect_file_info> peeks at the first header line of CSV/PSV/TSV files to
176 extract the correct separator character, the primary-key column name, and
177 the full ordered column list. Without this, two silent L<Database::Abstraction>
178 defaults corrupt every result: C<sep_char> defaults to C<'!'> (turning a
179 comma-separated file into a single-field table) and C<id> defaults to
180 C<'entry'> (causing every row to be discarded when no C<entry> column
181 exists).
182  
183 Result filtering (C<eq>, C<contains>, C<gt>, etc.) is performed at the
184 controller layer by C<Dashboard::_apply_filter_spec> after C<fetch_all>
185 returns. C<DataSource> itself is filter-unaware.
186  
187 All user-visible strings and exception messages are keyed through the
188 C<%MESSAGES> dictionary and routed via C<_msg()>, making every diagnostic
189 replaceable by an i18n object at instantiation time.
190  
191 =cut
192  
193 =head1 ENCODING
194  
195 =head2 What is safe to put in cell values
196  
197 Cell values (the actual data inside rows) are passed through as Perl
198 character strings exactly as C<Database::Abstraction> and the underlying
199 DBI driver return them. Full Unicode -- including non-ASCII letters,
200 accented characters, CJK ideographs, and emoji -- is supported in cell
201 values, provided the source file is encoded in UTF-8 (or the encoding
202 declared in its HTTP headers for URL-backed tables).
203  
204 =head2 What must stay ASCII
205  
206 The following structural items must contain B<only ASCII characters>:
207  
208 =over 4
209  
210 =item C<table> constructor argument
211  
212 The bare file stem used to look up the file on disk. Non-ASCII characters
213 are not valid SQL identifiers and will be silently replaced with underscores
214 (C<_>) by the sanitization step in C<new()>.
215  
216 =item Column header names (CSV / TSV / PSV first line)
217  
218 Column names are used as hash keys and as SQL identifiers inside
219 C<Database::Abstraction>. Non-ASCII headers cause C<DBD::CSV> to
220 fail or produce garbage keys. If your source file has non-ASCII
221 column headers, rename them before opening via C<DataSource>.
222  
223 =item C<host> argument
224  
225 The hostname or C<user@hostname> string for SFTP access. Must match
226 C<[A-Za-z0-9._@-]>; anything outside that character set is rejected by
227 the host-validation regex and produces C<error_backend_init>.
228  
229 =back
230  
231 =head2 URL-backed tables
232  
233 URL-backed tables (the C<url =E<gt>> constructor path) are fetched with
234 L<Database::BI::SafeUA>, which refuses (C<403>, so the read croaks
235 C<error_fetch_failed>) any request or redirect whose host resolves to this
236 machine, a private network or a link-local address such as a cloud metadata
237 service, and connects to the address it checked. C<private_hosts =E<gt> [...]>
238 names the hosts (exact, or C<*.suffix>) that may resolve privately: an
239 intranet. If the remote server declares a charset in its
240 HTTP headers or in an HTML C<meta> tag, L<Database::Abstraction> uses it
241 to decode the response body. If the charset declaration is absent or wrong,
242 cell values may arrive as raw bytes rather than character strings, and
243 multi-byte sequences will look garbled.
244  
245 =head2 File-backed tables (CSV / TSV / PSV)
246  
247 For files smaller than 16 KB, L<Text::xSV::Slurp> reads bytes and returns
248 them without re-encoding; for larger files the L<DBD::CSV> path is used
249 instead. In both cases C<DataSource> forces the slurp path (by passing
250 C<max_slurp_size =E<gt> -s $file>) so that column names with spaces are
251 preserved as-is. The caller (the Mojolicious controller) is responsible
252 for setting the correct C<Content-Type: text/html; charset=UTF-8> response
253 header.
254  
255 =cut
256  
257 # ---------------------------------------------------------------------------
258 # I18N message dictionary.
259 # All user-visible strings and exception messages are keyed here.
260 # To plug in a real i18n backend (e.g. Locale::Maketext), pass an object
261 # that responds to maketext($key, @args) as the "i18n" constructor argument;
262 # it will be called in preference to this table.
263 # ---------------------------------------------------------------------------
264  
265 8 Readonly our %MESSAGES => (
266 error_directory_required => 'DataSource: argument "directory" is required',
267 error_table_required => 'DataSource: argument "table" is required',
268 error_directory_missing => 'DataSource: directory "%s" does not exist or is not readable',
269 error_table_name_invalid => 'DataSource: table name "%s" contains illegal characters (alphanumeric and underscore only)',
270 error_backend_init => 'DataSource: failed to initialise database backend for table "%s": %s',
271 error_fetch_failed => 'DataSource: fetch_all failed for table "%s": %s',
272 error_rows_not_records => 'the source did not return a table of rows',
273 error_url_invalid => 'DataSource: URL "%s" must begin with http:// or https://',
274 error_url_fetch => 'DataSource: failed to open HTML table at "%s": %s',
275 error_no_safe_id => 'DataSource: table "%s" has no column with a safe identifier name (letters, digits, underscore); rename at least one column header',
276 error_no_tables => 'DataSource: SQLite file "%s" contains no user-defined tables',
277 error_table_not_found => 'DataSource: SQLite file "%s" does not contain a table named "%s"',
278 error_table_name_unsafe => 'The table "%s" in "%s" cannot be opened, because its name contains characters other than letters, digits and underscores. %s To open this one, rename it in a copy of the file, for example with sqlite3: ALTER TABLE "%s" RENAME TO %s;',
279 error_table_name_others => 'Open another table in the file instead: %s.',
280 error_no_usable_tables => 'No table in "%s" can be opened, because their names contain characters other than letters, digits and underscores: %s. To open one, rename it in a copy of the file, for example with sqlite3: ALTER TABLE "%s" RENAME TO %s;',
281 error_unsupported_db => 'DataSource: "%s" is a Berkeley DB file but the installed DB_File library cannot open it (file may use a newer Berkeley DB format)',
282 error_redis_invalid => 'DataSource: Redis URL "%s" must begin with redis://',
283 error_mariadb_unknown_server => 'DataSource: no MariaDB server is called "%s"; add it to mariadb_servers in the configuration',
284 error_mariadb_settings => 'DataSource: MariaDB server "%s" needs a valid %s in mariadb_servers in the configuration',
285 error_mariadb_no_driver => 'DataSource: DBD::MariaDB or DBD::mysql must be installed to open MariaDB server "%s"',
286 error_mariadb_connect => 'DataSource: cannot connect to MariaDB server "%s": %s',
287 error_mariadb_table => 'DataSource: cannot read table "%s" on MariaDB server "%s": %s',
288 error_json_invalid => 'DataSource: "%s" is not valid JSON: %s',
289 error_json_no_rows => 'DataSource: "%s" holds no table: it needs a list of objects, an object of objects, or an object holding one list of objects',
290 error_json_list_not_found => 'DataSource: JSON file "%s" has no list of objects called "%s"',
291 warn_empty_result => 'DataSource: fetch_all returned no records for table "%s"',
292 warn_data_normalised => 'DataSource: result from backend was a hashref; converted to arrayref for table "%s"',
293 );
294  
295 # A table name is a bare SQL-safe identifier: starts with a letter or
296 # underscore, followed by zero or more alphanumeric/underscore characters.
297 8 Readonly my $TABLE_NAME_RE => qr/\A[A-Za-z_][A-Za-z0-9_]*\z/;
298  
299 # What may go into a MariaDB DSN. A ';' would start another DSN attribute.
300 8 Readonly my $MARIADB_HOST_RE => qr/\A[A-Za-z0-9._-]+\z/;
301 8 Readonly my $MARIADB_DATABASE_RE => qr/\A[A-Za-z0-9_\$-]+\z/;
302 8 Readonly my $MARIADB_PORT_RE => qr/\A[0-9]+\z/;
303 # A page should not hang on a server that has gone away.
304 8 Readonly my $MARIADB_CONNECT_TIMEOUT => 5;
305 # Text::CSV's error_diag code for the plain end of a file.
306 8 Readonly my $CSV_EOF => 2012;
307  
308 # count() remembers each file table's row count until the file changes (see
309 # _count_key): a SQLite table paged in SQL shows its number of pages, and a
310 # COUNT(*) on every page of a big file would cost what the N+1 paging saves.
311 # Emptied when it reaches $COUNT_MEMO_MAX entries.
312 8 my %COUNT_MEMO;
313 8 Readonly my $COUNT_MEMO_MAX => 256;
314  
315 # The extensions D::A looks for before .json; a file with one of them is the
316 # table, and a .json beside it is not read.
317 8 Readonly my @NON_JSON_EXT => qw(sql sqlite sqlite3 db psv tsv xls xlsx csv xml);
318 # The sort columns D::A accepts; it ignores any other, with a warning.
319 8 Readonly my $DA_SORTABLE_RE => qr/\A[a-zA-Z_][a-zA-Z0-9_.]*\z/;
320  
321 # ---------------------------------------------------------------------------
322 # Protected helpers
323 # ---------------------------------------------------------------------------
324  
325 # _safe_table_name( $name ) -> $string
326 #
327 # The name to suggest for a table whose own name Database::Abstraction refuses:
328 # each illegal character becomes an underscore, and a leading digit gets one.
329 10 ▶sub _safe_table_name {
Subroutine _safe_table_name: called CC=2 SCAR=6.9
330 10 my $name = $_[0];
331 10 $name =~ s/[^A-Za-z0-9_]/_/g;
332 10 ▶ $name = "_$name" unless $name =~ /\A[A-Za-z_]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[A-Za-z_]/ 2 8 10
333 10 return $name;
334 }
335  
336 # _url_label( $url ) -> $string
337 #
338 # Derive a safe, lowercase identifier from a URL for use as the table label.
339 # Takes the last non-empty path component, strips the extension, then replaces
340 # non-alphanumeric characters with underscores. Falls back to the hostname
341 # when the path component is absent or starts with a digit.
342 10 ▶sub _url_label {
Subroutine _url_label: called CC=6 SCAR=17.9
343 10 my $url = $_[0];
344 10 my ($path) = $url =~ m{https?://[^/?#]+(.*)}i;
345 10 ▶ my @parts = grep { length } split m{/}, ($path // '');
Condition50%1/2
$path // ""
l!l
100
MC/DC0%0/1
$path // ""
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
346 10 ▶ my $last = @parts ? $parts[-1] : '';
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 8 2 10
347 # /s: . must cross \n in case a percent-decoded newline hides inside the URL.
348 10 $last =~ s/[?#].*//s; # strip query / fragment
349 10 $last =~ s/\.[^.]+\z//; # strip file extension (\z: no trailing-\n loophole)
350 10 $last =~ s/[^A-Za-z0-9_]/_/g; # sanitize
351 10 ▶ unless (length $last && $last =~ /\A[A-Za-z_]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $last and $last =~ /\A[A-Za-z_]/) 3 7 10
Condition100%3/3
length $last and $last =~ /\A[A-Za-z_]/
!ll&&!rl&&r
217
MC/DC100%2/2
length $last and $last =~ /\A[A-Za-z_]/
length $last $last =~ /\A[A-Za-z_]/
Truth table100%3/3
A and B
A= length $lastB= $last =~ /\A[A-Za-z_]/
ABresult
0X0
100
111
352 # No usable path component -- fall back to hostname
353 3 my ($host) = $url =~ m{https?://([^/:?#]+)};
354 3 ▶ $last = defined $host ? do { (my $h = $host) =~ s/[^A-Za-z0-9_]/_/g; $h } : 'html';
Branch: 1 branch
BranchTrueFalseTotal
defined $host ? : 3 0 3
355 }
356 # $last is always non-empty here: the unless-block sets it to either
357 # the sanitized hostname (>= 1 char) or the literal 'html'.
358 10 return lc($last);
359 }
360  
361 # _fmt( $key [, @sprintf_args] ) -> $string
362 #
363 # Package-level (not a method) i18n formatter. Looks up $key in %MESSAGES and
364 # applies sprintf if positional arguments are supplied. This function is used
365 # in new() before the object exists; instance methods should use _msg() instead
366 # so that a caller-supplied i18n object can override the built-in strings.
367 51 ▶sub _fmt :Protected {
Subroutine _fmt: called CC=3 SCAR=11.0
368 51 my ($key, @args) = @_;
369 51 ▶ my $tmpl = Database::BI::I18N::text(__PACKAGE__, \%MESSAGES, $key) // "Internal error: unknown message key '$key'";
Condition100%2/2
Database::BI::I18N::text(__PACKAGE__, \%Database::BI::Model::DataSource::MESSAGES, $key) // "Internal error: unknown message key '${key}'"
l!l
501
MC/DC100%1/1
Database::BI::I18N::text(__PACKAGE__, \%Database::BI::Model::DataSource::MESSAGES, $key) // "Internal error: unknown message key '${key}'"
Database::BI::I18N::text(__PACKAGE__, \%Database::BI::Model::DataSource::MESSAGES, $key)
Truth table100%2/2
A
A= Database::BI::I18N::text(__PACKAGE__, \%Database::BI::Model::DataSource::MESSAGES, $key)
Aresult
11
00
370 51 ▶ return @args ? sprintf($tmpl, @args) : $tmpl;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 44 7 51
371 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
372  
373 # _msg( $self, $key [, @sprintf_args] ) -> $string
374 #
375 # Instance-level i18n formatter. Delegates to the caller-supplied i18n object
376 # (if any) before falling back to _fmt(). The i18n object must implement
377 # maketext($key, @args).
378 11 ▶sub _msg :Protected {
Subroutine _msg: called CC=2 SCAR=6.9
379 11 my ($self, $key, @args) = @_;
380 11 ▶ if (my $i18n = $self->{_i18n}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i18n = $self->{"_i18n"}) 1 10 11
381 1 return $i18n->maketext($key, @args);
382 }
383 10 return _fmt($key, @args);
384 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
385  
386 # ---------------------------------------------------------------------------
387 # Constructor
388 # ---------------------------------------------------------------------------
389  
390 =head1 CONSTRUCTOR
391  
392 =head2 new
393  
394 Creates and returns a new C<Database::BI::Model::DataSource> instance.
395  
396 =head3 API SPECIFICATION
397  
398 =head4 INPUT
399  
400 {
401 directory => 'string', # required; local dir, or remote dir when host is set
402 table => 'string', # required; bare file stem (no extension)
403 host => 'string', # optional; "hostname" or "user@hostname" for SFTP access
404 file_ext => 'string', # optional; extension hint when remote file has non-standard suffix (e.g. "log")
405 internal_table => 'string', # optional; a SQLite file's table, or a JSON file's list, to open
406 column_order => \@names, # optional; the order columns() gives, when the backend has none
407 cache => 'object', # optional; CHI cache object for result caching
408 cache_ttl_url => 'string', # optional; CHI TTL string for URL-backed tables (default "15 min")
409 mariadb => \%settings, # optional; a mariadb_servers entry; needs server and table
410 server => 'string', # the name of that entry
411 i18n => 'object', # optional; must implement maketext($key, @args) for i18n
412 }
413  
414 C<internal_table> selects a named table inside a multi-table SQLite file.
415 When omitted, C<DataSource> auto-selects the first user table whose name
416 matches the filename stem; if no match, it picks the first table found.
417 Supplying a name that does not exist in the file croaks C<error_table_not_found>.
418  
419 A C<.json> file is read as a table when it is a list of objects, an object
420 of objects (each key a row, kept in a column C<id>, or C<key> when the rows
421 have an C<id>), or an object holding lists of objects. Each such list is a
422 table, as a SQLite file's are: C<internal_table> names one, the first in the
423 file is read without it, and C<sqlite_tables> lists them. A cell holding an
424 object or a list is shown as its JSON text, C<true> and C<false> as words and
425 C<null> as an empty cell; the columns are in the order the file first names
426 them. A C<.csv> (or any other format's file) beside it with the same name is
427 the one read. Not JSON croaks C<error_json_invalid>; JSON with no table in
428 it, C<error_json_no_rows>; a list it does not have, C<error_json_list_not_found>.
429  
430 C<column_order> is for a caller that knows the columns' order better than the
431 backend does. A SQLite backend lists its columns alphabetically, so an
432 extract of an accelerated dataset passes the order its file had. It is used
433 only when the file itself gives no order (SQLite, XML), and is trusted, not
434 checked against the columns the backend has.
435  
436 Accepts a flat key/value list, a hashref, or positional arguments via
437 C<Params::Get>.
438  
439 When C<host> is provided, C<directory> is treated as a path on the remote
440 host and is not checked with C<-d> locally. C<DataSource> downloads the
441 file via SFTP (L<Net::SFTP::Foreign>) to a process-local temporary directory
442 and then processes the local copy. The temporary directory persists for the
443 lifetime of the C<DataSource> object and is cleaned up automatically on
444 destruction.
445  
446 When C<file_ext> is provided together with C<host>, it is used as the first
447 candidate extension when probing the remote host for the file. If the
448 downloaded file does not have a standard extension (one of C<csv>, C<tsv>,
449 C<psv>, C<xlsx>, C<xls>, C<sql>, C<sqlite>, C<sqlite3>, C<db>, C<xml>, C<json>),
450 the file content is sniffed (SQLite magic bytes, XML preamble, a leading
451 C<[> or C<{> for JSON, or first-line
452 field separator) and the file is renamed to the correct standard extension
453 before further processing.
454  
455 =head4 DOMAIN CONSTRAINTS
456  
457 =over 4
458  
459 =item C<directory>
460  
461 Must satisfy C<-d $directory> (must exist and be a directory). An empty
462 string, a non-existent path, or the path to a regular file all produce
463 C<error_directory_missing>.
464  
465 Valid partition: any existing directory path
466 Invalid partition: non-existent path, regular file path, empty string ""
467  
468 =item C<table>
469  
470 The bare file stem (no extension). Characters that are illegal in SQL
471 identifiers - hyphens, dots, spaces, etc. - are silently replaced with
472 underscores before the name is used internally. A stem that starts with a
473 digit is prefixed with C<_>. Only a completely empty string croaks.
474  
475 Valid partition: "sales", "_tmp", "report_2024",
476 "Transactions-2026-09-08" (hyphens sanitized to underscores),
477 "my.data" (dot sanitized), "1sales" (prefixed to "_1sales")
478 Invalid partition: "" (empty string)
479 Boundary values: "a" (length-1 letter, valid), "_" (length-1 underscore,
480 valid), "" (empty string, croaks error_table_name_invalid)
481  
482 =back
483  
484 =head4 OUTPUT
485  
486 Returns C<$self> (a blessed hashref). Croaks on invalid arguments.
487  
488 =head3 MESSAGES
489  
490 error_directory_required -- "directory" argument was not supplied
491 error_table_required -- "table" argument was not supplied
492 error_directory_missing -- supplied directory does not exist / is unreadable
493 error_table_name_invalid -- table name contains illegal characters or is empty
494 error_table_name_unsafe -- internal_table names a SQLite table whose own name
495 has illegal characters; says what else to open and
496 how to rename it
497 error_no_usable_tables -- every table in a SQLite file has such a name
498 error_json_invalid -- a .json file is not valid JSON
499 error_json_no_rows -- a .json file holds no list of objects, object of
500 objects or object holding such a list
501 error_json_list_not_found -- internal_table names no list in a .json file
502 error_no_safe_id -- no column in the file has a safe SQL identifier name;
503 rename at least one column header to an alphanumeric name
504 error_backend_init -- backend initialisation failed; sub-cases:
505 * Net::SFTP::Foreign is not installed
506 * could not connect to <host> via SFTP
507 * could not fetch any supported file from <host>:<dir>/<stem>.*
508 * Database::Abstraction subclass could not be instantiated
509 error_no_tables -- SQLite file opened successfully but contains no user-defined tables
510 error_unsupported_db -- .db file has BerkeleyDB magic bytes but DB_File could not open it
511 (file likely uses a newer BDB format than the installed libdb supports)
512 error_fetch_failed -- fetch_all raised an exception (wraps the underlying DBI/D::A error)
513 error_url_invalid -- URL passed to the url=> constructor path does not begin
514 with http:// or https://
515 error_url_fetch -- the backend for a URL could not be set up; a page
516 that cannot be fetched is error_fetch_failed, from the read
517  
518 =cut
519  
520 1288 ▶sub new {
Subroutine new: called CC=12 SCAR=25.0
Pod documented
521 # Strategy: normalise the argument list with Params::Get so callers may
522 # pass a hashref or a flat list interchangeably, then validate strictly
523 # with Params::Validate before touching any value.
524 # When a "url" key is present, dispatch to the URL/HTML-table path instead.
525 1288 my $class = shift;
526 # Opening a file tries several drivers in turn; what they leave in $@ and $! is
527 # of no interest to a caller, who may be holding its own.
528 1288 local ($@, $!);
529 1288 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
530 1288 ▶ my $raw = Params::Get::get_params(undef, \@_) // {};
Condition50%1/2
Params::Get::get_params(undef, \@_) // {}
l!l
12880
MC/DC0%0/1
Params::Get::get_params(undef, \@_) // {}
Params::Get::get_params(undef, \@_)
Truth table50%1/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
531 1288 ▶ return $class->_new_from_url($raw) if exists $raw->{url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $raw->{"url"} 5 1283 1288
532 1283 ▶ return $class->_new_from_redis($raw) if exists $raw->{redis};
Branch: 1 branch
BranchTrueFalseTotal
if exists $raw->{"redis"} 0 1283 1283
533 1283 ▶ return $class->_new_from_mariadb($raw) if exists $raw->{mariadb};
Branch: 1 branch
BranchTrueFalseTotal
if exists $raw->{"mariadb"} 0 1283 1283
534 1283 ▶ croak _fmt('error_table_required') unless defined $raw->{table};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw->{"table"} 1 1282 1283
535  
536 1282 my $args = validate_strict(
537 schema => {
538 directory => { type => 'string' },
539 table => { type => 'string' },
540 host => { type => 'string', optional => 1, default => undef },
541 file_ext => { type => 'string', optional => 1, default => undef },
542 internal_table => { type => 'string', optional => 1, default => undef },
543 column_order => { type => 'arrayref', optional => 1, default => undef },
544 i18n => { type => 'object', optional => 1, default => undef, can => 'maketext' },
545 cache => { type => 'object', optional => 1, default => undef },
546 cache_ttl_url => { type => 'string', optional => 1, default => '15 min' },
547 },
548 input => $raw,
549 );
550  
551 # For remote files (host is set), the directory is on the remote host and
552 # cannot be stat()d locally. Only check local existence when no host is given.
553 croak _fmt('error_directory_missing', $args->{directory})
554 1281 ▶ unless defined $args->{host} || -d $args->{directory};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"host"} or -d $args->{"directory"} 5 1276 1281
Condition100%3/3
defined $args->{"host"} or -d $args->{"directory"}
l!l&&r!l&&!r
612705
MC/DC100%2/2
defined $args->{"host"} or -d $args->{"directory"}
defined $args->{"host"} -d $args->{"directory"}
Truth table100%3/3
A or B
A= defined $args->{"host"}B= -d $args->{"directory"}
ABresult
1X1
011
000
555  
556 # Reject path-traversal characters first: '/', '\', and NUL are the only
557 # characters that could let _raw_table escape the intended directory when
558 # D::A constructs "$dir/$dbname.$ext". Everything else is either safe as a
559 # filename component or will be sanitized below.
560 croak _fmt('error_table_name_invalid', $args->{table})
561 1276 ▶ if !length($args->{table}) || $args->{table} =~ m{[/\\\x00]};
Branch: 1 branch
BranchTrueFalseTotal
if not length $args->{"table"} or $args->{"table"} =~ m[[/\\\x00]] 1 1275 1276
Condition66%2/3
not length $args->{"table"} or $args->{"table"} =~ m[[/\\\x00]]
l!l&&r!l&&!r
011275
MC/DC50%1/2
not length $args->{"table"} or $args->{"table"} =~ m[[/\\\x00]]
not length $args->{"table"} $args->{"table"} =~ m[[/\\\x00]]
Truth table66%2/3
not(A) or B
A= not length $args->{"table"}B= $args->{"table"} =~ m[[/\\\x00]]
ABresult
1X1
011
000
562  
563 # Silently sanitize table names derived from file stems: replace characters
564 # that are illegal in SQL identifiers (hyphens, dots, spaces, etc.) with
565 # underscores. A leading digit is prefixed with '_'. The original name is
566 # kept in _raw_table for filesystem lookup (dbname) so the actual file is
567 # still found; the sanitized name is used only as the internal D::A
568 # identifier and ephemeral package name.
569 1275 my $raw_table = $args->{table};
570 1275 (my $safe_table = $raw_table) =~ s/[^A-Za-z0-9_]/_/g;
571 1275 ▶ $safe_table = '_' . $safe_table if $safe_table =~ /\A[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $safe_table =~ /\A[0-9]/ 1 1274 1275
572  
573 1275 ▶ croak _fmt('error_table_name_invalid', $raw_table)
Branch: 1 branch
BranchTrueFalseTotal
unless $safe_table =~ /$TABLE_NAME_RE/ 0 1275 1275
574 unless $safe_table =~ $TABLE_NAME_RE;
575  
576 my $self = bless {
577 _directory => $args->{directory},
578 _table => $safe_table,
579 _raw_table => $raw_table,
580 _host => $args->{host},
581 _file_ext => $args->{file_ext},
582 _internal_table => $args->{internal_table},
583 _column_order => $args->{column_order},
584 _i18n => $args->{i18n},
585 _cache => $args->{cache},
586 _cache_ttl_url => $args->{cache_ttl_url},
587 1275 _db => undef,
588 }, $class;
589  
590 1275 $self->_init_backend();
591 1272 return $self;
592  
593 }
594  
595 # _new_from_url( $class, \%raw_params ) -> $self
596 #
597 # Alternate constructor path for URL-backed HTML tables.
598 # Validates the URL scheme, derives a display label from the URL path,
599 # then calls _init_url_backend to build the D::A in-memory table.
600 # Entry: $raw->{url} must be an http:// or https:// URL.
601 # $raw->{html_table_index} (optional, default 0): zero-based table index.
602 # $raw->{private_hosts} (optional): names that may resolve to private
603 # addresses (Database::BI::SafeUA).
604 # $raw->{i18n} (optional): Locale::Maketext-compatible object.
605 # Exit: Returns $self. Croaks on invalid URL scheme or backend init failure.
606 # Side Effects: None: the URL is fetched by the first read.
607 5 ▶sub _new_from_url :Protected {
Subroutine _new_from_url: called CC=6 SCAR=18.7
608 5 my ($class, $raw) = @_;
609  
610 5 ▶ my $url = $raw->{url} // croak _fmt('error_url_invalid', '');
Condition50%1/2
$raw->{"url"} // croak(_fmt("error_url_invalid", ""))
l!l
50
MC/DC0%0/1
$raw->{"url"} // croak(_fmt("error_url_invalid", ""))
$raw->{"url"}
Truth table50%1/2
A
A= $raw->{"url"}
Aresult
11
00
611 5 ▶ croak _fmt('error_url_invalid', $url)
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[\Ahttps?://]i 3 2 5
612 unless $url =~ m{\Ahttps?://}i;
613  
614 2 ▶ my $table_idx = $raw->{html_table_index} // 0;
Condition50%1/2
$raw->{"html_table_index"} // 0
l!l
20
MC/DC0%0/1
$raw->{"html_table_index"} // 0
$raw->{"html_table_index"}
Truth table50%1/2
A
A= $raw->{"html_table_index"}
Aresult
11
00
615 my $self = bless {
616 _url => $url,
617 _table => _url_label($url),
618 _i18n => $raw->{i18n},
619 _cache => $raw->{cache},
620 _cache_ttl_url => $raw->{cache_ttl_url} // '15 min',
621 _html_table_index => $table_idx,
622 2 ▶ _private_hosts => ref($raw->{private_hosts}) eq 'ARRAY' ? $raw->{private_hosts} : [],
Branch: 1 branch
BranchTrueFalseTotal
ref $raw->{'private_hosts'} eq 'ARRAY' ? : 2 0 2
Condition50%1/2
$raw->{"cache_ttl_url"} // "15 min"
l!l
20
MC/DC0%0/1
$raw->{"cache_ttl_url"} // "15 min"
$raw->{"cache_ttl_url"}
Truth table50%1/2
A
A= $raw->{"cache_ttl_url"}
Aresult
11
00
623 _id_col => undef,
624 _columns => undef,
625 _db => undef,
626 }, $class;
627  
628 2 $self->_init_url_backend($table_idx);
629 2 return $self;
630 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
631  
632 # _init_url_backend( $self, $table_index ) -> void
633 #
634 # Purpose: Construct the Database::Abstraction backend for URL/HTML-table mode.
635 # No request is made here. On the first read D::A fetches the page via
636 # LWP::UserAgent, parses it with
637 # HTML::TableExtract, and stores all rows (including headers from row 0)
638 # as an in-memory arrayref. Column order is not recoverable after
639 # construction (hash keys lose order), so _columns stays undef and
640 # _get_columns in the controller falls back to alphabetical sorting.
641 # Entry: $self->{_url} is a valid http(s) URL; $table_index is a non-negative int.
642 # Exit: Sets $self->{_db}. Croaks if the backend object cannot be built; a
643 # failure to fetch or parse the page comes from the first read instead.
644 4 ▶sub _init_url_backend :Protected {
Subroutine _init_url_backend: called CC=3 SCAR=11.0
645 4 my ($self, $table_index) = @_;
646 4 my $url = $self->{_url};
647  
648 4 require Database::Abstraction;
649  
650 # Reuse a single generic package for all URL-backed instances.
651 # The package name is irrelevant for the URL code path -- D::A branches on
652 # the presence of $self->{'url'}, not on the class name.
653 4 my $pkg = 'Database::BI::_DB::HtmlUrl';
654 {
655 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
656 4 ▶ push @{"${pkg}::ISA"}, 'Database::Abstraction'
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->isa("Database::Abstraction") 2 2 4
657 unless $pkg->isa('Database::Abstraction');
658 }
659  
660 # D::A fetches with the ua it is given: one that will not reach this
661 # machine or a private network, however the URL or its DNS is dressed up.
662 4 require Database::BI::SafeUA;
663 4 my $db = eval {
664 $pkg->new({
665 url => $url,
666 html_table_index => $table_index,
667 no_entry => 1,
668 4 ua => Database::BI::SafeUA->new(private_hosts => $self->{_private_hosts}),
669 })
670 };
671 4 ▶ croak $self->_msg('error_url_fetch', $url, $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
672  
673 3 $self->{_db} = $db;
674 3 return;
675 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
676  
677 # _new_from_redis( $class, \%raw_params ) -> $self
678 #
679 # Alternate constructor path for Redis-backed tables.
680 # Entry: $raw->{redis} must be a redis:// URL.
681 # $raw->{table} (required): table name -- becomes the Redis key prefix
682 # (keys in Redis must follow the pattern tablename:*).
683 # $raw->{i18n} (optional): Locale::Maketext-compatible object.
684 # Exit: Returns $self. Croaks on invalid URL or table name.
685 10 ▶sub _new_from_redis :Protected {
Subroutine _new_from_redis: called CC=9 SCAR=22.0
686 10 my ($class, $raw) = @_;
687  
688 10 ▶ my $redis_url = $raw->{redis} // croak _fmt('error_redis_invalid', '');
Condition100%2/2
$raw->{"redis"} // croak(_fmt("error_redis_invalid", ""))
l!l
91
MC/DC100%1/1
$raw->{"redis"} // croak(_fmt("error_redis_invalid", ""))
$raw->{"redis"}
Truth table100%2/2
A
A= $raw->{"redis"}
Aresult
11
00
689 9 ▶ croak _fmt('error_redis_invalid', $redis_url)
Branch: 1 branch
BranchTrueFalseTotal
unless $redis_url =~ m[\Aredis://]i 2 7 9
690 unless $redis_url =~ m{\Aredis://}i;
691  
692 7 ▶ my $table = $raw->{table} // croak _fmt('error_table_required');
Condition100%2/2
$raw->{"table"} // croak(_fmt("error_table_required"))
l!l
61
MC/DC100%1/1
$raw->{"table"} // croak(_fmt("error_table_required"))
$raw->{"table"}
Truth table100%2/2
A
A= $raw->{"table"}
Aresult
11
00
693 6 ▶ croak _fmt('error_table_name_invalid', $table)
Branch: 1 branch
BranchTrueFalseTotal
if not length $table or $table =~ m[[/\\\x00]] 4 2 6
Condition100%3/3
not length $table or $table =~ m[[/\\\x00]]
l!l&&r!l&&!r
132
MC/DC100%2/2
not length $table or $table =~ m[[/\\\x00]]
not length $table $table =~ m[[/\\\x00]]
Truth table100%3/3
not(A) or B
A= not length $tableB= $table =~ m[[/\\\x00]]
ABresult
1X1
011
000
694 if !length($table) || $table =~ m{[/\\\x00]};
695  
696 2 my $raw_table = $table;
697 2 (my $safe_table = $raw_table) =~ s/[^A-Za-z0-9_]/_/g;
698 2 ▶ $safe_table = '_' . $safe_table if $safe_table =~ /\A[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $safe_table =~ /\A[0-9]/ 1 1 2
699 2 ▶ croak _fmt('error_table_name_invalid', $raw_table)
Branch: 1 branch
BranchTrueFalseTotal
unless $safe_table =~ /$TABLE_NAME_RE/ 0 2 2
700 unless $safe_table =~ $TABLE_NAME_RE;
701  
702 my $self = bless {
703 _table => $safe_table,
704 _raw_table => $raw_table,
705 _redis_url => $redis_url,
706 _i18n => $raw->{i18n},
707 _cache => $raw->{cache},
708 2 ▶ _cache_ttl_url => $raw->{cache_ttl_url} // '15 min',
Condition50%1/2
$raw->{"cache_ttl_url"} // "15 min"
l!l
02
MC/DC0%0/1
$raw->{"cache_ttl_url"} // "15 min"
$raw->{"cache_ttl_url"}
Truth table50%1/2
A
A= $raw->{"cache_ttl_url"}
Aresult
11
00
709 _id_col => 'entry',
710 _columns => undef,
711 _db => undef,
712 }, $class;
713  
714 2 $self->_init_redis_backend();
715 2 return $self;
716 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
717  
718 # _init_redis_backend( $self ) -> void
719 #
720 # Purpose: Construct the Database::Abstraction backend for Redis mode.
721 # D::A connects to the Redis server lazily (on first data access).
722 # We pass redis_key => $raw_table so D::A uses the original key name
723 # (which may contain colons, hyphens, etc.) for Redis lookups, while
724 # the sanitized $table is used only as the Perl package-name suffix.
725 # no_entry => 1 requests array-style rows (no id-keyed hash).
726 # Entry: $self->{_redis_url} is a valid redis:// URL.
727 # $self->{_table} is a safe SQL identifier (sanitized key name).
728 # $self->{_raw_table} is the original Redis key name.
729 # Exit: Sets $self->{_db}. Croaks when D::A construction fails.
730 2 ▶sub _init_redis_backend :Protected {
Subroutine _init_redis_backend: called CC=3 SCAR=11.0
731 2 my $self = shift;
732 2 my $table = $self->{_table};
733  
734 2 require Database::Abstraction;
735  
736 2 my $pkg = 'Database::BI::_DB::' . ucfirst($table);
737 {
738 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
739 2 ▶ push @{"${pkg}::ISA"}, 'Database::Abstraction'
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->isa("Database::Abstraction") 1 1 2
740 unless $pkg->isa('Database::Abstraction');
741 }
742  
743 2 my $db = eval { $pkg->new(
744 database => $self->{_redis_url},
745 table => $table,
746 redis_key => $self->{_raw_table},
747 2 ) };
748 2 ▶ croak $self->_msg('error_backend_init', $table, $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
749  
750 1 $self->{_db} = $db;
751 1 return;
752 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
753  
754 # _mariadb_dsn( $server, \%settings ) -> ($dsn, $username, $password)
755 #
756 # Purpose: What connects to one entry of the mariadb_servers configuration.
757 # Nothing is connected here: Database::Join::Catalog does that, and
758 # Database::Abstraction for the rows. An error names the server; it
759 # never holds the password.
760 # Entry: $server is the entry's name, for messages. \%settings holds database
761 # and optionally host, port, username and password.
762 # Exit: The data source and credentials. Croaks on settings that are not
763 # valid, or when no MariaDB or MySQL driver is installed.
764 21 ▶sub _mariadb_dsn {
Subroutine _mariadb_dsn: called CC=10 SCAR=23.0
765 21 my ($server, $settings) = @_;
766  
767 21 ▶ croak _fmt('error_mariadb_unknown_server', $server) unless ref $settings eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" 2 19 21
768  
769 19 ▶ my $database = $settings->{database} // '';
Condition100%2/2
$settings->{"database"} // ""
l!l
181
MC/DC100%1/1
$settings->{"database"} // ""
$settings->{"database"}
Truth table100%2/2
A
A= $settings->{"database"}
Aresult
11
00
770 19 ▶ my $host = $settings->{host} // 'localhost';
Condition100%2/2
$settings->{"host"} // "localhost"
l!l
217
MC/DC100%1/1
$settings->{"host"} // "localhost"
$settings->{"host"}
Truth table100%2/2
A
A= $settings->{"host"}
Aresult
11
00
771 19 my $port = $settings->{port};
772 19 ▶ croak _fmt('error_mariadb_settings', $server, 'database') unless $database =~ $MARIADB_DATABASE_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $database =~ /$MARIADB_DATABASE_RE/ 3 16 19
773 16 ▶ croak _fmt('error_mariadb_settings', $server, 'host') unless $host =~ $MARIADB_HOST_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /$MARIADB_HOST_RE/ 2 14 16
774 14 ▶ croak _fmt('error_mariadb_settings', $server, 'port') if defined $port && $port !~ $MARIADB_PORT_RE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port and not $port =~ /$MARIADB_PORT_RE/ 2 12 14
Condition66%2/3
defined $port and not $port =~ /$MARIADB_PORT_RE/
!ll&&!rl&&r
1202
MC/DC50%1/2
defined $port and not $port =~ /$MARIADB_PORT_RE/
defined $port not $port =~ /$MARIADB_PORT_RE/
Truth table66%2/3
A and not(B)
A= defined $portB= not $port =~ /$MARIADB_PORT_RE/
ABresult
0X0
100
111
775  
776 # The driver is optional, so it is looked for only now.
777 12 require Database::Join::Catalog;
778 12 my $driver = Database::Join::Catalog->available_driver(qw(MariaDB mysql));
779 12 ▶ croak _fmt('error_mariadb_no_driver', $server) unless defined $driver;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $driver 1 11 12
780  
781 11 ▶ my $dsn = "dbi:${driver}:database=$database;host=$host"
Branch: 1 branch
BranchTrueFalseTotal
defined $port ? : 0 11 11
782 . (defined $port ? ";port=$port" : '')
783 . ';' . lc($driver) . "_connect_timeout=$MARIADB_CONNECT_TIMEOUT";
784 11 return ($dsn, @{$settings}{qw(username password)});
785 }
786  
787 # _new_from_mariadb( $class, \%raw_params ) -> $self
788 #
789 # Alternate constructor path for a table on a MariaDB or MySQL server.
790 # Entry: $raw->{mariadb} is the server's entry from mariadb_servers.
791 # $raw->{server} is that entry's name; $raw->{table} is the table.
792 # Exit: Returns $self. Croaks when the server or the table cannot be reached,
793 # so a caller finds out here and not from the first read.
794 # Note: Rows are not cached: a table's contents can change with nothing to say
795 # so, which is what a file's mtime says for a file.
796 9 ▶sub _new_from_mariadb :Protected {
Subroutine _new_from_mariadb: called CC=14 SCAR=26.8
797 9 my ($class, $raw) = @_;
798  
799 9 ▶ my $table = $raw->{table} // croak _fmt('error_table_required');
Condition100%2/2
$raw->{"table"} // croak(_fmt("error_table_required"))
l!l
81
MC/DC100%1/1
$raw->{"table"} // croak(_fmt("error_table_required"))
$raw->{"table"}
Truth table100%2/2
A
A= $raw->{"table"}
Aresult
11
00
800 8 ▶ croak _fmt('error_table_name_invalid', $table) unless $table =~ $TABLE_NAME_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $table =~ /$TABLE_NAME_RE/ 1 7 8
801 7 ▶ my $server = $raw->{server} // '';
Condition50%1/2
$raw->{"server"} // ""
l!l
70
MC/DC0%0/1
$raw->{"server"} // ""
$raw->{"server"}
Truth table50%1/2
A
A= $raw->{"server"}
Aresult
11
00
802 7 ▶ croak _fmt('error_mariadb_unknown_server', $server) unless $server =~ $TABLE_NAME_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $server =~ /$TABLE_NAME_RE/ 1 6 7
803  
804 6 my ($dsn, $username, $password) = _mariadb_dsn($server, $raw->{mariadb});
805  
806 # D::A's columns() is alphabetical; table_info gives the declared order.
807 6 my $info = eval {
808 6 Database::Join::Catalog->table_info(dsn => $dsn, username => $username, password => $password, table => $table);
809 };
810 6 ▶ unless ($info) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info) 1 5 6
811 1 my $err = $@;
812 1 ▶ croak _fmt('error_mariadb_table', $table, $server, "$err")
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->can("stage") and $err->stage eq "query" 1 0 1
Condition33%2/6
builtin::blessed($err) and $err->can("stage") and $err->stage eq "query"
!ll&&!rl&&r
001
builtin::blessed($err) and $err->can("stage")
!ll&&!rl&&r
001
MC/DC0%0/3
builtin::blessed($err) and $err->can("stage") and $err->stage eq "query"
builtin::blessed($err) $err->can("stage") $err->stage eq "query"
Truth table25%1/4
A and B and C
A= builtin::blessed($err)B= $err->can("stage")C= $err->stage eq "query"
ABCresult
0XX0
10X0
1100
1111
813 if blessed($err) && $err->can('stage') && $err->stage eq 'query';
814 0 croak _fmt('error_mariadb_connect', $server, "$err");
815 }
816 5 my $columns = $info->{columns};
817 5 my @primary = @{ $info->{primary_key} };
818  
819 # Two servers may each have an "orders".
820 5 my $pkg = 'Database::BI::_DB::Mariadb_' . $server . '_' . $table;
821 {
822 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
823 5 ▶ push @{"${pkg}::ISA"}, 'Database::Abstraction'
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->isa("Database::Abstraction") 1 4 5
824 unless $pkg->isa('Database::Abstraction');
825 }
826 5 require Database::Abstraction;
827 5 my $db = eval { $pkg->new(
828 dsn => $dsn,
829 username => $username,
830 password => $password,
831 table => $table,
832 no_entry => 1,
833 ) };
834 5 ▶ croak _fmt('error_backend_init', $table, $@) unless $db;
Branch: 1 branch
BranchTrueFalseTotal
unless $db 1 4 5
835  
836 return bless {
837 _table => $table,
838 _raw_table => $table,
839 _mariadb => $server,
840 # Pages taken with LIMIT need an order that does not change between them.
841 _order_col => (@primary == 1 && $primary[0] =~ $TABLE_NAME_RE) ? $primary[0] : undef,
842 _id_col => $primary[0] // $columns->[0],
843 _columns => $columns,
844 _i18n => $raw->{i18n},
845 4 ▶ _db => $db,
Branch: 1 branch
BranchTrueFalseTotal
@primary == 1 && $primary[0] =~ /$TABLE_NAME_RE/ ? : 1 3 4
Condition83%5/6
@primary == 1 and $primary[0] =~ /$TABLE_NAME_RE/
!ll&&!rl&&r
211
$primary[0] // $columns->[0]
l!l&&r!l&&!r
310
MC/DC100%2/2
@primary == 1 and $primary[0] =~ /$TABLE_NAME_RE/
@primary == 1 $primary[0] =~ /$TABLE_NAME_RE/
MC/DC0%0/2
$primary[0] // $columns->[0]
$primary[0] $columns->[0]
Truth table100%3/3
A and B
A= @primary == 1B= $primary[0] =~ /$TABLE_NAME_RE/
ABresult
0X0
100
111
Truth table66%2/3
A // B
A= $primary[0]B= $columns->[0]
ABresult
1X1
011
000
846 }, $class;
847 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
848  
849 # ---------------------------------------------------------------------------
850 # Protected initialisation
851 # ---------------------------------------------------------------------------
852  
853 # _csv_escape_char($path) -> '"' | '\\'
854 #
855 # Purpose: The escape character a delimited file is written with, chosen as
856 # Database::Abstraction (>= 0.56) chooses it, so this module parses a
857 # file as D::A will.
858 # Entry: $path names a CSV, TSV or PSV file.
859 # Exit: '\\' when the file holds a backslash before a quote anywhere (or
860 # will not open); else '"' when it holds two quotes in a row (RFC
861 # 4180's "Adeste ""Overture"""); else '\\'.
862 1064 ▶sub _csv_escape_char {
Subroutine _csv_escape_char: called CC=6 SCAR=18.0
863 1064 my ($path) = @_;
864 1064 local ($@, $!);
865 1064 local $/ = "\n";
866  
867 1064 my $fh;
868 8 ▶ { no autodie 'open'; open($fh, '<:raw', $path) or return '\\' }
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:raw", $path or return "\\" 1063 1 1064
869 1063 my $doubled = 0;
870 1063 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 7080 1063 8143
871 7080 ▶ if ($line =~ /\\"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\\"/) 0 7080 7080
872 0 close $fh;
873 0 return '\\';
874 }
875 7080 ▶ $doubled ||= ($line =~ /""/);
Condition100%3/3
$doubled ||= $line =~ /""/
l!l&&r!l&&!r
827070
MC/DC100%2/2
$doubled ||= $line =~ /""/
$doubled $line =~ /""/
Truth table100%3/3
A ||= B
A= $doubledB= $line =~ /""/
ABresult
1X1
011
000
876 }
877 1063 close $fh;
878 1063 ▶ return $doubled ? '"' : '\\';
Branch: 1 branch
BranchTrueFalseTotal
$doubled ? : 2 1061 1063
879 }
880  
881 # _best_id_column($fh, $sep, \@header_fields, $escape) -> $column_name | undef
882 #
883 # Purpose: The column Database::Abstraction should key rows on.
884 # Entry: $fh is open just after the header line; @header_fields is the header
885 # split and stripped, in file order (empty names included, so indexes
886 # match the data).
887 # Exit: The safe-identifier column with the fewest rows D::A would drop
888 # (an empty cell, or one starting with '#', which D::A reads as a
889 # comment), the earliest on a tie; undef when no header name is safe.
890 # Reads $fh to its end. The file is parsed as D::A's slurp parses it
891 # (with $escape, from _csv_escape_char), or a row like "Joe\"s Bar"
892 # would end the count there; a line that still will not parse is
893 # skipped, not the end.
894 1065 ▶sub _best_id_column {
Subroutine _best_id_column: called CC=15 SCAR=27.7
895 1065 my ($fh, $sep, $fields, $escape) = @_;
896  
897 1065 ▶ my @safe = grep { defined($fields->[$_]) && $fields->[$_] =~ $TABLE_NAME_RE } 0 .. $#{$fields};
Condition66%2/3
defined $fields->[$_] && $fields->[$_] =~ /$TABLE_NAME_RE/
!ll&&!rl&&r
0515910
MC/DC50%1/2
defined $fields->[$_] && $fields->[$_] =~ /$TABLE_NAME_RE/
defined $fields->[$_] $fields->[$_] =~ /$TABLE_NAME_RE/
Truth table66%2/3
A && B
A= defined $fields->[$_]B= $fields->[$_] =~ /$TABLE_NAME_RE/
ABresult
0X0
100
111
898 1065 ▶ return undef unless @safe;
Branch: 1 branch
BranchTrueFalseTotal
unless @safe 12 1053 1065
899  
900 1053 require Text::CSV;
901 1053 ▶ my $csv = Text::CSV->new({
Condition100%2/2
$escape // "\\"
l!l
104112
MC/DC100%1/1
$escape // "\\"
$escape
Truth table100%2/2
A
A= $escape
Aresult
11
00
902 binary => 1, sep_char => $sep, escape_char => $escape // '\\',
903 allow_loose_quotes => 1, allow_whitespace => 1,
904 });
905 1053 my %dropped = map { $_ => 0 } @safe;
906 1053 while (1) {
907 7055 my $at = tell($fh);
908 7055 my $row = $csv->getline($fh);
909 7055 ▶ if (!$row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 1053 6002 7055
910 # The end, or a line that will not parse: go on from the next one,
911 # unless nothing was read, which would loop for ever.
912 1053 ▶ last if $csv->eof() || eof($fh) || tell($fh) == $at;
Branch: 1 branch
BranchTrueFalseTotal
if $csv->eof or eof $fh or tell $fh == $at 1053 0 1053
Condition33%2/6
$csv->eof or eof $fh or tell $fh == $at
l!l&&r!l&&!r
105300
$csv->eof or eof $fh
l!l&&r!l&&!r
105300
MC/DC0%0/3
$csv->eof or eof $fh or tell $fh == $at
$csv->eof eof $fh tell $fh == $at
Truth table25%1/4
A or B or C
A= $csv->eofB= eof $fhC= tell $fh == $at
ABCresult
1XX1
01X1
0011
0000
913 0 next;
914 }
915 6002 ▶ next unless grep { defined($_) && length($_) } @{$row}; # a blank line is not a row
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_ and length $_;} @{$row;} 2 6000 6002
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
02334900
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
916 6000 for my $i (@safe) {
917 34781 my $cell = $row->[$i];
918 34781 ▶ $dropped{$i}++ if !defined($cell) || $cell !~ /\S/ || $cell =~ /\A\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cell && $cell =~ /\S/ or $cell =~ /\A\s*#/ 25 34756 34781
Condition100%6/6
not defined $cell && $cell =~ /\S/ or $cell =~ /\A\s*#/
l!l&&r!l&&!r
23234756
defined $cell && $cell =~ /\S/
!ll&&!rl&&r
22134758
MC/DC100%3/3
not defined $cell && $cell =~ /\S/ or $cell =~ /\A\s*#/
defined $cell $cell =~ /\S/ $cell =~ /\A\s*#/
Truth table100%4/4
not(A && B) or C
A= defined $cellB= $cell =~ /\S/C= $cell =~ /\A\s*#/
ABCresult
0XX1
10X1
1111
1100
919 }
920 }
921  
922 1053 ▶ my ($best) = sort { $dropped{$a} <=> $dropped{$b} || $a <=> $b } @safe;
Condition66%2/3
$dropped{$a} <=> $dropped{$b} || $a <=> $b
l!l&&r!l&&!r
1786420
MC/DC0%0/2
$dropped{$a} <=> $dropped{$b} || $a <=> $b
$dropped{$a} <=> $dropped{$b} $a <=> $b
Truth table66%2/3
A || B
A= $dropped{$a} <=> $dropped{$b}B= $a <=> $b
ABresult
1X1
011
000
923 1053 return $fields->[$best];
924 }
925  
926 # _detect_file_info( $dir, $table ) -> \%info
927 #
928 # Peek at the first header line of a CSV, TSV or PSV file and return a hashref:
929 # sep_char => field separator character (',' or '|')
930 # id => first column name (used as Database::Abstraction's id key)
931 # columns => arrayref of all column names in file order
932 #
933 # Two non-obvious defaults in Database::Abstraction make this necessary:
934 # 1. sep_char defaults to '!' -- so a plain CSV is read as one giant field
935 # per row, producing a single comma-joined string instead of columns.
936 # 2. id defaults to 'entry' -- the slurp filter greps on that column; if it
937 # doesn't exist every row is silently discarded.
938 # Returns an empty hashref for non-CSV/TSV/PSV/XLSX/SQLite formats (XML, etc.).
939 # For SQLite/.db files that can be opened, returns { sqlite_tables => [...] }.
940 1283 ▶sub _detect_file_info :Protected {
Subroutine _detect_file_info: called CC=53 SCAR=67.0
941 1283 my ($dir, $table, $want) = @_;
942  
943 # XLSX: D::A reads the rows. This parse is for what D::A cannot say: an
944 # empty workbook makes it croak, and its columns() is alphabetical, not the
945 # order of the sheet.
946 {
947 1283 my $path = File::Spec->catfile($dir, "$table.xlsx");
948 1283 ▶ if (-r $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $path) 0 1283 1283
949 0 my $ok = eval { require Spreadsheet::ParseXLSX; 1 };
950 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
951 0 my $parser = Spreadsheet::ParseXLSX->new;
952 0 my $wb = $parser->parse($path);
953 # The sheet D::A will read: the one named after the table, or
954 # failing that the first.
955 0 my $sheet_name = _safe_table_name($table);
956 0 ▶ my ($ws) = $wb ? grep { $_->get_name eq $sheet_name } $wb->worksheets : ();
Branch: 1 branch
BranchTrueFalseTotal
$wb ? : 0 0 0
957 0 ▶ $ws //= $wb ? $wb->worksheet(0) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$wb ? : 0 0 0
Condition0%0/3
$ws //= $wb ? $wb->worksheet(0) : undef
l!l&&r!l&&!r
000
MC/DC0%0/2
$ws //= $wb ? $wb->worksheet(0) : undef
$ws $wb ? $wb->worksheet(0) : undef
Truth table0%0/3
A //= B
A= $wsB= $wb ? $wb->worksheet(0) : undef
ABresult
1X1
011
000
958  
959 0 ▶ unless ($ws) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ws) 0 0 0
960 # Empty workbook or parse failure: sentinel so _init_backend
961 # skips D::A (which would fail with "(no error string)").
962 0 return { _file_is_empty => 1, file_size => -s $path };
963 }
964  
965 0 my ($rmin, $rmax) = $ws->row_range;
966 0 my ($cmin, $cmax) = $ws->col_range;
967  
968 # No rows at all (blank worksheet).
969 0 ▶ return { _file_is_empty => 1, file_size => -s $path }
Branch: 1 branch
BranchTrueFalseTotal
if $rmax < $rmin 0 0 0
970 if $rmax < $rmin;
971  
972 # Row 0 = column headers, spelt as D::A will key the rows. A
973 # column with no name stays out of the list, so it is not shown.
974 0 my @cols;
975 0 for my $c ($cmin .. $cmax) {
976 0 my $cell = $ws->get_cell($rmin, $c);
977 0 ▶ my $name = defined $cell ? ($cell->value // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cell ? : 0 0 0
Condition0%0/2
$cell->value // ""
l!l
00
MC/DC0%0/1
$cell->value // ""
$cell->value
Truth table0%0/2
A
A= $cell->value
Aresult
11
00
978 0 ▶ push @cols, $name if $name =~ /[^\s"]/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[^\s"]/ 0 0 0
979 }
980  
981 # No parseable column names.
982 0 ▶ return { _file_is_empty => 1, file_size => -s $path }
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 0 0 0
983 unless @cols;
984  
985 0 my ($safe_id) = grep { $_ =~ $TABLE_NAME_RE } @cols;
986  
987 # With no_entry D::A does not use the id for a workbook, so
988 # headers that are all unsafe ("First Name") need no id of their own.
989 return {
990 0 ▶ columns => \@cols,
Condition0%0/2
$safe_id // "entry"
l!l
00
MC/DC0%0/1
$safe_id // "entry"
$safe_id
Truth table0%0/2
A
A= $safe_id
Aresult
11
00
991 id => $safe_id // 'entry',
992 file_size => -s $path,
993 };
994 }
995 }
996 }
997  
998 1283 for my $ext (qw(csv psv tsv)) {
999 1759 my $path = File::Spec->catfile($dir, "$table.$ext");
1000 1759 ▶ next unless -r $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 707 1052 1759
1001 # "use autodie" makes open() die on failure, so "or next" would be dead
1002 # code. Disable autodie for this open so a vanishing file (race between
1003 # the -r probe and the open) results in a clean skip rather than a croak.
1004 1052 my $fh;
1005 8 ▶ { no autodie 'open'; open $fh, '<', $path or next }
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $path or next 1052 0 1052
1006 1052 my $line = <$fh>;
1007 # 0-byte file: no header, no rows. Return a sentinel so _init_backend
1008 # can skip Database::Abstraction entirely. If we let D::A see an empty
1009 # file it falls through to the DBD::CSV path, whose error handling on
1010 # an empty SELECT corrupts DBI's internal Errstr SV; on a DEBUGGING perl
1011 # (DBI 1.651 + perl 5.44.0) this triggers an XS assertion at cleanup.
1012 1052 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 1052 1052
1013 0 close $fh;
1014 0 return { _file_is_empty => 1, file_size => 0 };
1015 }
1016 1052 chomp $line;
1017 1052 $line =~ s/\r\z//; # strip CR from CRLF files before any split
1018 # A UTF-8 byte-order mark, which Excel's "CSV UTF-8" writes, is not
1019 # part of the first column's name. D::A would keep it there, so it
1020 # reads a copy without it (see _init_backend).
1021 1052 my $bom = $line =~ s/\A\xEF\xBB\xBF//;
1022  
1023 1052 my $sep;
1024 1052 ▶ if($ext eq 'psv') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ext eq 'psv') { } 2 1050 1052
elsif ($ext eq 'tsv') { } 6 1044 1050
1025 2 $sep = '|';
1026 } elsif($ext eq 'tsv') {
1027 6 $sep = "\t";
1028 } else {
1029 # Sniff the separator: Database::Abstraction uses '!' natively and
1030 # sometimes stores those files with a .csv extension. If splitting
1031 # on ',' yields a single field that itself contains '!', the real
1032 # separator is almost certainly '!'.
1033 1044 my @probe = split /,/, $line, -1;
1034 1044 ▶ $sep = (@probe == 1 && $line =~ /!/) ? '!' : ',';
Branch: 1 branch
BranchTrueFalseTotal
@probe == 1 && $line =~ /!/ ? : 0 1044 1044
Condition33%1/3
@probe == 1 and $line =~ /!/
!ll&&!rl&&r
104400
MC/DC0%0/2
@probe == 1 and $line =~ /!/
@probe == 1 $line =~ /!/
Truth table33%1/3
A and B
A= @probe == 1B= $line =~ /!/
ABresult
0X0
100
111
1035 }
1036  
1037 1052 my @fields = _split_csv_line($line, $sep);
1038 # Two separate substitutions: the /g on an anchored alternation wastes
1039 # O(N) engine cycles retrying \A (which can only match at position 0).
1040 1052 for (@fields) { s/\A[\s"]+//; s/[\s"]+\z// } # strip whitespace and quotes
1041 1052 ▶ pop @fields while @fields && !length $fields[-1]; # a trailing separator
Branch: 1 branch
BranchTrueFalseTotal
@fields and not length $fields[-1] and do { pop @fields } 0 1052 1052
Condition33%1/3
@fields and not length $fields[-1]
!ll&&!rl&&r
010520
MC/DC0%0/2
@fields and not length $fields[-1]
@fields not length $fields[-1]
Truth table33%1/3
A and not(B)
A= @fieldsB= not length $fields[-1]
ABresult
0X0
100
111
1042 1052 my @cols = grep { length } @fields;
1043  
1044 # A blank first line (e.g. a file containing only "\n") produces an
1045 # empty column list. Treat that the same as a 0-byte file: return the
1046 # _file_is_empty sentinel so _init_backend skips D::A entirely.
1047 # Attempting to construct D::A with id => undef and an empty columns
1048 # list would croak error_no_safe_id -- misleading for what is effectively
1049 # an empty file.
1050 1052 ▶ if (!@cols) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cols) 0 1052 1052
1051 0 close $fh;
1052 0 return { _file_is_empty => 1, file_size => -s $path };
1053 }
1054  
1055 # Database::Abstraction validates id against $SAFE_IDENTIFIER
1056 # (/\A[a-zA-Z_][a-zA-Z0-9_]*\z/) at construction time and uses it as
1057 # a row-existence sentinel: every data row must have a defined, non-#
1058 # value in the id column or D::A drops it (empty_is_undef => 1 makes
1059 # truly empty cells undef).
1060 #
1061 # Strategy: of the safe columns, the one with the fewest empty cells
1062 # across the whole file, the earliest on a tie. Choosing by the first
1063 # data row alone lost rows: a bank export whose first transaction had a
1064 # cheque number made Check the id, and D::A then dropped the 358 of 362
1065 # rows that had none. D::A reads the whole file anyway (max_slurp_size
1066 # is the file's size), so one more pass costs no more than that. With
1067 # no data rows, the first safe column in the header.
1068 1052 my $safe_id = _best_id_column($fh, $sep, \@fields, _csv_escape_char($path));
1069  
1070 # If no safe identifier was found in the header, check whether the first
1071 # row looks like data values rather than column names. A CSV exported
1072 # from a bank or accounting system often has no header row at all -- the
1073 # first line is already a transaction record. When that is the case,
1074 # synthesize safe column names by inferring the type of each value
1075 # (date, amount, description); _init_backend then gives D::A a copy of
1076 # the file with those names as its header.
1077 1052 ▶ unless (defined $safe_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $safe_id) 11 1041 1052
1078 11 ▶ if (_values_are_data_like(\@cols)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_values_are_data_like(\@cols)) 10 1 11
1079 10 my @synth = _synthesize_col_names(\@fields);
1080 10 close $fh;
1081 return {
1082 10 sep_char => $sep,
1083 id => $synth[0],
1084 columns => \@synth,
1085 _headerless => { path => $path, ext => $ext }, # its copy drops a BOM
1086 file_size => -s $path,
1087 };
1088 }
1089 }
1090  
1091 1042 close $fh;
1092  
1093 # Return file_size so _init_backend can pass it as max_slurp_size to
1094 # Database::Abstraction. Without this, files larger than D::A's default
1095 # 16 KB threshold go through the DBI/DBD::CSV path, which lowercases
1096 # column names and replaces spaces with underscores ("Account Number" ->
1097 # "account_number"). The slurp path (Text::xSV::Slurp) preserves the
1098 # original header names, so forcing it avoids the mismatch.
1099 return {
1100 1042 ▶ sep_char => $sep,
Branch: 1 branch
BranchTrueFalseTotal
$bom ? : 0 1042 1042
1101 id => $safe_id,
1102 columns => \@cols,
1103 ($bom ? (_bom => { path => $path, ext => $ext }) : ()),
1104 file_size => -s $path,
1105 };
1106 }
1107  
1108 # SQLite / Berkeley DB: peek at sqlite_master to discover the internal table
1109 # names. _init_backend uses this list to auto-select the correct table when
1110 # the filename stem (dbname) differs from the table name inside the file --
1111 # e.g. obituaries.sql whose internal table is called "deceased". If the
1112 # file is not a valid SQLite database (e.g. a Berkeley DB file) the eval
1113 # fails; for .db files we then check for BerkeleyDB magic so we can read
1114 # the data directly (or return an error sentinel) instead of letting D::A
1115 # fall through to its CSV path, which would read a large binary file as text.
1116 # .sqlite and .sqlite3 are common alternative SQLite extensions -- treated identically to .sql.
1117 231 my $non_sqlite_db; # set to path when a .db file fails the SQLite probe
1118 231 for my $ext (qw(sql sqlite sqlite3 db)) {
1119 873 my $path = File::Spec->catfile($dir, "$table.$ext");
1120 873 ▶ next unless -r $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 645 228 873
1121 228 my $tables = eval {
1122 228 require Database::Join::Catalog;
1123 228 Database::Join::Catalog->tables(file => $path);
1124 };
1125 # defined $tables means the eval succeeded (even an empty list is valid)
1126 228 ▶ return { sqlite_tables => ($tables // []), file_size => -s $path }
Branch: 1 branch
BranchTrueFalseTotal
if defined $tables 228 0 228
Condition50%1/2
$tables // []
l!l
2280
MC/DC0%0/1
$tables // []
$tables
Truth table50%1/2
A
A= $tables
Aresult
11
00
1127 if defined $tables;
1128 0 ▶ $non_sqlite_db = $path if $ext eq 'db';
Branch: 1 branch
BranchTrueFalseTotal
if $ext eq "db" 0 0 0
1129 0 last; # file found but not SQLite -- do not try the other ext
1130 }
1131  
1132 # A .db file that is not SQLite: check for BerkeleyDB magic bytes.
1133 # BerkeleyDB Hash/Btree/Queue files begin with a 32-bit magic number either
1134 # at offset 0 (big-endian files) or at offset 12 (little-endian files).
1135 # If found, try to open via DB_File under a 10-second alarm so a version-
1136 # incompatible file does not block the server for minutes.
1137 3 ▶ if (defined $non_sqlite_db) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $non_sqlite_db) 0 3 3
1138 0 my $magic_ok = 0;
1139 0 ▶ if (open(my $fh, '<:raw', $non_sqlite_db)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Database::BI::Model::DataSource::open(my $fh, "<:raw", $non_sqlite_db)) 0 0 0
1140 0 my $buf = '';
1141 0 ▶ if (read($fh, $buf, 4) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Database::BI::Model::DataSource::read($fh, \$buf, 4) == 4) 0 0 0
1142 0 my %MAGIC = map { $_ => 1 }
1143 (0x00061561, 0x00053162, 0x00042253, 0x00052444);
1144 0 ▶ $magic_ok = 1 if $MAGIC{unpack('N', $buf)} || $MAGIC{unpack('V', $buf)};
Branch: 1 branch
BranchTrueFalseTotal
if $MAGIC{unpack "N", $buf} or $MAGIC{unpack "V", $buf} 0 0 0
Condition0%0/3
$MAGIC{unpack "N", $buf} or $MAGIC{unpack "V", $buf}
l!l&&r!l&&!r
000
MC/DC0%0/2
$MAGIC{unpack "N", $buf} or $MAGIC{unpack "V", $buf}
$MAGIC{unpack "N", $buf} $MAGIC{unpack "V", $buf}
Truth table0%0/3
A or B
A= $MAGIC{unpack "N", $buf}B= $MAGIC{unpack "V", $buf}
ABresult
1X1
011
000
1145 }
1146 0 ▶ unless ($magic_ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($magic_ok) 0 0 0
1147 0 ▶ if (seek $fh, 12, 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Database::BI::Model::DataSource::seek($fh, 12, 0)) 0 0 0
1148 0 my $b12 = '';
1149 0 ▶ if (read($fh, $b12, 4) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Database::BI::Model::DataSource::read($fh, \$b12, 4) == 4) 0 0 0
1150 0 my $hex = substr(unpack('H*', $b12), 0, 4);
1151 0 ▶ $magic_ok = 1 if $hex eq '6115' || $hex eq '1561';
Branch: 1 branch
BranchTrueFalseTotal
if $hex eq 6115 or $hex eq 1561 0 0 0
Condition0%0/3
$hex eq 6115 or $hex eq 1561
l!l&&r!l&&!r
000
MC/DC0%0/2
$hex eq 6115 or $hex eq 1561
$hex eq 6115 $hex eq 1561
Truth table0%0/3
A or B
A= $hex eq 6115B= $hex eq 1561
ABresult
1X1
011
000
1152 }
1153 }
1154 }
1155 0 close $fh;
1156 }
1157 0 ▶ if ($magic_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic_ok) 0 0 0
1158 0 my $fsize = -s $non_sqlite_db;
1159 # BerkeleyDB magic confirmed. Check that DB_File can open it,
1160 # so that D::A never reads a file it cannot as CSV text,
1161 # but only for reasonably-sized files (<= 100 MB). Opening a
1162 # multi-GB BerkeleyDB file with an incompatible library version
1163 # causes multi-minute disk I/O that cannot be interrupted with
1164 # alarm() (XS I/O in libdb-5.x ignores SIGALRM via SA_RESTART).
1165 # For large files, return the unsupported sentinel immediately.
1166 0 Readonly my $BDB_MAX_BYTES => 100 * 1024 * 1024; # 100 MB
1167 0 ▶ if (defined $fsize && $fsize > $BDB_MAX_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fsize and $fsize > $BDB_MAX_BYTES) 0 0 0
Condition0%0/3
defined $fsize and $fsize > $BDB_MAX_BYTES
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fsize and $fsize > $BDB_MAX_BYTES
defined $fsize $fsize > $BDB_MAX_BYTES
Truth table0%0/3
A and B
A= defined $fsizeB= $fsize > $BDB_MAX_BYTES
ABresult
0X0
100
111
1168 0 return { _unknown_binary_db => 1, file_size => $fsize };
1169 }
1170 0 my $opens = eval {
1171 0 require DB_File;
1172 0 my %bdb;
1173 # Capture the tie object so we can undef it before untie --
1174 # DB_File warns "untie while N inner references" if the object
1175 # reference count is > 0 when untie fires.
1176 0 my $db_obj = tie %bdb, 'DB_File', $non_sqlite_db,
1177 Fcntl::O_RDONLY(), 0644, $DB_File::DB_HASH;
1178 0 ▶ return 0 unless $db_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $db_obj 0 0 0
1179 0 undef $db_obj;
1180 0 untie %bdb;
1181 0 1;
1182 };
1183 0 ▶ if ($opens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opens) 0 0 0
1184 return {
1185 0 columns => ['entry', 'value'],
1186 id => 'entry',
1187 file_size => $fsize,
1188 };
1189 }
1190 # tie failed: return a sentinel so _init_backend can show a proper
1191 # error instead of letting D::A read the binary file as CSV text.
1192 0 return { _unknown_binary_db => 1, file_size => $fsize };
1193 }
1194 }
1195  
1196 # JSON comes last, as it does in D::A's own search: a table.csv beside
1197 # table.json is the file D::A opens.
1198 {
1199 3 my $path = File::Spec->catfile($dir, "$table.json");
1200 0 ▶ if (-r $path && !grep { -f File::Spec->catfile($dir, "$table.$_") } @NON_JSON_EXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $path and not grep({-f "File::Spec"->catfile($dir, "$table.$_");} @NON_JSON_EXT)) 0 3 3
Condition33%1/3
-r $path and not grep({-f "File::Spec"->catfile($dir, "$table.$_");} @NON_JSON_EXT)
!ll&&!rl&&r
300
MC/DC0%0/2
-r $path and not grep({-f "File::Spec"->catfile($dir, "$table.$_");} @NON_JSON_EXT)
-r $path not grep({-f "File::Spec"->catfile($dir, "$table.$_");} @NON_JSON_EXT)
Truth table33%1/3
A and not(B)
A= -r $pathB= not grep({-f "File::Spec"->catfile($dir, "$table.$_");} @NON_JSON_EXT)
ABresult
0X0
100
111
1201 0 my $json = _json_table($path, $want);
1202 return { _file_is_empty => 1, file_size => -s $path, _json_lists => $json->{lists} }
1203 0 ▶ unless @{ $json->{rows} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$json->{"rows"};} 0 0 0
1204 0 my ($safe_id) = grep { $_ =~ $TABLE_NAME_RE } @{ $json->{columns} };
1205 return {
1206 columns => $json->{columns},
1207 id => $safe_id // 'entry',
1208 _json => $json,
1209 _json_lists => $json->{lists},
1210 0 ▶ };
Condition0%0/2
$safe_id // "entry"
l!l
00
MC/DC0%0/1
$safe_id // "entry"
$safe_id
Truth table0%0/2
A
A= $safe_id
Aresult
11
00
1211 }
1212 }
1213 3 return {};
1214 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1215  
1216 # _json_read( $path ) -> ($text, $data)
1217 #
1218 # Purpose: A JSON file's bytes and what they parse to.
1219 # Exit: ('', undef) for a zero-byte or blank file. Croaks
1220 # error_json_invalid when the text is not JSON.
1221 0 ▶sub _json_read {
Subroutine _json_read: not called CC=5 SCAR=34.0
1222 0 my $path = shift;
1223 0 local ($@, $!);
1224 0 my $text = do {
1225 0 open my $fh, '<:raw', $path;
1226 0 local $/;
1227 0 my $t = <$fh>;
1228 0 close $fh;
1229 0 ▶ $t // '';
Condition0%0/2
$t // ""
l!l
00
MC/DC0%0/1
$t // ""
$t
Truth table0%0/2
A
A= $t
Aresult
11
00
1230 };
1231 0 ▶ return ('', undef) if $text !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\S/ 0 0 0
1232  
1233 0 require Mojo::JSON;
1234 0 my $data;
1235 0 ▶ unless (eval { $data = Mojo::JSON::decode_json($text); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $data = Mojo::JSON::decode_json($text); 1 } }) 0 0 0
1236 0 ▶ (my $why = $@ // '') =~ s/\s+at \S+ line \d+\.?\s*\z//;
Condition0%0/2
$@ // ""
l!l
00
MC/DC0%0/1
$@ // ""
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
1237 0 croak _fmt('error_json_invalid', $path, $why);
1238 }
1239 0 return ($text, $data);
1240 }
1241  
1242 # _json_list_keys( $text, $data ) -> @keys
1243 #
1244 # Purpose: The top-level keys of a JSON object that hold a list of objects, in
1245 # the order the file gives them: each is a table.
1246 # Notes: A list holding anything but objects is not a table. An empty list
1247 # counts only when no list has rows, so { "items": [], "rows": [...] }
1248 # is one table.
1249 0 ▶sub _json_list_keys {
Subroutine _json_list_keys: not called CC=4 SCAR=30.0
1250 0 my ($text, $data) = @_;
1251 0 ▶ return () unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 0 0
1252 my @lists = grep {
1253 0 my $v = $data->{$_};
1254 0 ▶ ref $v eq 'ARRAY' && !grep { ref $_ ne 'HASH' } @{$v}
Condition0%0/3
ref $v eq 'ARRAY' && !grep({ref $_ ne 'HASH';} @{$v;})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $v eq 'ARRAY' && !grep({ref $_ ne 'HASH';} @{$v;})
ref $v eq 'ARRAY' !grep({ref $_ ne 'HASH';} @{$v;})
Truth table0%0/3
A && not(B)
A= ref $v eq 'ARRAY'B= !grep({ref $_ ne 'HASH';} @{$v;})
ABresult
0X0
100
111
1255 0 } @{ _json_key_order($text, 1) };
1256 0 my @full = grep { @{ $data->{$_} } } @lists;
1257 0 ▶ return @full ? @full : @lists;
Branch: 1 branch
BranchTrueFalseTotal
@full ? : 0 0 0
1258 }
1259  
1260 # _json_columns( $text, $rows, $depth, $list ) -> \@names
1261 #
1262 # Purpose: The columns of rows read from a JSON text, in the order the text
1263 # first names them, then any it did not (sorted).
1264 0 ▶sub _json_columns {
Subroutine _json_columns: not called CC=1 SCAR=6.9
1265 0 my ($text, $rows, $depth, $list) = @_;
1266 0 my @columns = @{ _json_key_order($text, $depth, $list) };
1267 0 my %named = map { $_ => 1 } @columns;
1268 0 push @columns, sort grep { !$named{$_}++ } map { keys %{$_} } @{$rows};
1269 0 return \@columns;
1270 }
1271  
1272 # _json_table( $path, $want ) -> { rows, columns, list, lists }
1273 #
1274 # Purpose: Read a JSON file as a table, for D::A to read a flat copy of.
1275 # Entry: $path is a readable .json file. $want, when defined, names the
1276 # list to read (internal_table).
1277 # Exit: rows: flat hashes. columns: in the order the file first names
1278 # them. list: the key the rows were under (undef when none).
1279 # lists: every list of objects in the file (empty when the file is
1280 # not an object holding lists). A zero-byte or blank file is no rows.
1281 # Croaks error_json_invalid, error_json_no_rows or
1282 # error_json_list_not_found.
1283 # Notes: Three shapes are tables:
1284 # [ {row}, ... ] each object a row
1285 # { "findings": [ {row}, ... ] } each list of objects under a key
1286 # a table, the first the default;
1287 # other values (a count, a date)
1288 # are left out
1289 # { "a1": {row}, "b2": {row} } each key a row, kept in a column
1290 # "id" ("key" when "id" is taken)
1291 # A cell holding an object or a list becomes its compact JSON text,
1292 # true and false the words, null an empty cell. The column order is
1293 # read from the text: a parsed object has no order.
1294 0 ▶sub _json_table {
Subroutine _json_table: not called CC=21 SCAR=61.4
1295 0 my ($path, $want) = @_;
1296 0 my ($text, $data) = _json_read($path);
1297 0 ▶ unless (length $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $text) 0 0 0
1298 0 ▶ croak _fmt('error_json_list_not_found', $path, $want) if defined $want;
Branch: 1 branch
BranchTrueFalseTotal
if defined $want 0 0 0
1299 0 return { rows => [], columns => [], list => undef, lists => [] };
1300 }
1301  
1302 0 my @lists = _json_list_keys($text, $data);
1303 0 ▶ if (defined $want) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $want) 0 0 0
1304 croak _fmt('error_json_list_not_found', $path, $want)
1305 0 ▶ unless grep { $_ eq $want } @lists;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $want;} @lists 0 0 0
1306 }
1307  
1308 0 my ($rows, $list, $key_col, $depth);
1309 0 ▶ if (ref $data eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
elsif (@lists) { } 0 0 0
elsif (ref $data eq 'HASH' and %{$data;} and not grep({ref $_ ne 'HASH';} values %{$data;})) { } 0 0 0
1310 0 ▶ croak _fmt('error_json_no_rows', $path) if grep { ref $_ ne 'HASH' } @{$data};
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "HASH";} @{$data;} 0 0 0
1311 0 ($rows, $depth) = ($data, 2);
1312 } elsif (@lists) {
1313 0 ▶ $list = $want // $lists[0];
Condition0%0/3
$want // $lists[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$want // $lists[0]
$want $lists[0]
Truth table0%0/3
A // B
A= $wantB= $lists[0]
ABresult
1X1
011
000
1314 0 ($rows, $depth) = ($data->{$list}, 3);
1315 0 ▶ } elsif (ref $data eq 'HASH' && %{$data} && !grep { ref $_ ne 'HASH' } values %{$data}) {
Condition0%0/6
ref $data eq "HASH" and %{$data;} and not grep({ref $_ ne "HASH";} values %{$data;})
!ll&&!rl&&r
000
ref $data eq "HASH" and %{$data;}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $data eq "HASH" and %{$data;} and not grep({ref $_ ne "HASH";} values %{$data;})
ref $data eq "HASH" %{$data;} not grep({ref $_ ne "HASH";} values %{$data;})
Truth table0%0/4
A and B and not(C)
A= ref $data eq "HASH"B= %{$data;}C= not grep({ref $_ ne "HASH";} values %{$data;})
ABCresult
0XX0
10X0
1100
1111
1316 0 $depth = 2;
1317 0 my @inner = map { keys %{$_} } values %{$data};
1318 0 ▶ $key_col = (grep { $_ eq 'id' } @inner) ? 'key' : 'id';
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq 'id';} @inner) ? : 0 0 0
1319 0 ▶ $key_col .= '_' while grep { $_ eq $key_col } @inner;
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $key_col;} @inner and do { $key_col .= '_' } 0 0 0
1320 0 $rows = [ map { { %{ $data->{$_} }, $key_col => $_ } } @{ _json_key_order($text, 1) } ];
1321 } else {
1322 0 croak _fmt('error_json_no_rows', $path);
1323 }
1324  
1325 0 my $columns = _json_columns($text, $rows, $depth, $list);
1326 0 ▶ @{$columns} = ($key_col, grep { $_ ne $key_col } @{$columns}) if defined $key_col;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key_col 0 0 0
1327  
1328 0 my @flat;
1329 0 for my $row (@{$rows}) {
1330 0 my %cells;
1331 0 ▶ while (my ($k, $v) = each %{$row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$row;}) 0 0 0
1332 0 ▶ if (blessed($v) && $v->isa('JSON::PP::Boolean')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($v) and $v->isa('JSON::PP::Boolean')) { } 0 0 0
elsif (ref $v) { } 0 0 0
Condition0%0/3
builtin::blessed($v) and $v->isa("JSON::PP::Boolean")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($v) and $v->isa("JSON::PP::Boolean")
builtin::blessed($v) $v->isa("JSON::PP::Boolean")
Truth table0%0/3
A and B
A= builtin::blessed($v)B= $v->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
1333 0 ▶ $v = $v ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
1334 } elsif (ref $v) {
1335 0 $v = Mojo::JSON::to_json($v);
1336 }
1337 0 $cells{$k} = $v;
1338 }
1339 0 push @flat, \%cells;
1340 }
1341 0 return { rows => \@flat, columns => $columns, list => $list, lists => \@lists };
1342 }
1343  
1344 # _json_key_order( $text, $depth, $under ) -> \@keys
1345 #
1346 # Purpose: The keys of the objects $depth levels down in a JSON text, in the
1347 # order they first appear (a parsed object keeps none).
1348 # Entry: $text is valid JSON (the caller has parsed it). $depth counts the
1349 # object itself: a row of a top-level list is 2, a row of a list under
1350 # a key 3, a key of the top-level object 1. $under, when defined,
1351 # keeps only what is under that top-level key.
1352 # Exit: The keys, each once, decoded.
1353 0 ▶sub _json_key_order {
Subroutine _json_key_order: not called CC=20 SCAR=60.4
1354 0 my ($text, $depth, $under) = @_;
1355 0 my (@stack, @order, %seen, $top);
1356 0 ▶ while ($text =~ /\G\s*(?:("(?:[^"\\]|\\.)*")|([{}\[\]:,])|[^\s{}\[\]:,"]+)/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G\s*(?:("(?:[^"\\]|\\.)*")|([{}\[\]:,])|[^\s{}\[\]:,"]+)/cgs) 0 0 0
1357 0 ▶ if (defined $1) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $1) { } 0 0 0
elsif (defined $2) { } 0 0 0
1358 0 my $frame = $stack[-1];
1359 0 ▶ next unless $frame && $frame->{object} && $frame->{want_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $frame and $frame->{"object"} and $frame->{"want_key"} 0 0 0
Condition0%0/6
$frame and $frame->{"object"} and $frame->{"want_key"}
!ll&&!rl&&r
000
$frame and $frame->{"object"}
!ll&&!rl&&r
000
MC/DC0%0/3
$frame and $frame->{"object"} and $frame->{"want_key"}
$frame $frame->{"object"} $frame->{"want_key"}
Truth table0%0/4
A and B and C
A= $frameB= $frame->{"object"}C= $frame->{"want_key"}
ABCresult
0XX0
10X0
1100
1111
1360 0 $frame->{want_key} = 0;
1361 0 my $key = Mojo::JSON::decode_json("[$1]")->[0];
1362 0 ▶ $top = $key if @stack == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @stack == 1 0 0 0
1363 0 ▶ next unless @stack == $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack == $depth 0 0 0
1364 0 ▶ next if defined $under && !(defined $top && $top eq $under);
Branch: 1 branch
BranchTrueFalseTotal
if defined $under and not defined $top && $top eq $under 0 0 0
Condition0%0/6
defined $under and not defined $top && $top eq $under
!ll&&!rl&&r
000
defined $top && $top eq $under
!ll&&!rl&&r
000
MC/DC0%0/3
defined $under and not defined $top && $top eq $under
defined $under defined $top $top eq $under
Truth table0%0/4
A and not(B && C)
A= defined $underB= defined $topC= $top eq $under
ABCresult
0XX0
1110
10X1
1101
1365 0 ▶ push @order, $key unless $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$key}++ 0 0 0
1366 } elsif (defined $2) {
1367 0 my $c = $2;
1368 0 ▶ if ($c eq '{') {
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq '{') { } 0 0 0
elsif ($c eq '[') { } 0 0 0
elsif ($c eq '}' or $c eq ']') { } 0 0 0
elsif ($c eq ',' and @stack and $stack[-1]{'object'}) { } 0 0 0
1369 0 push @stack, { object => 1, want_key => 1 };
1370 } elsif ($c eq '[') {
1371 0 push @stack, { object => 0 };
1372 ▶ } elsif ($c eq '}' || $c eq ']') {
Condition0%0/3
$c eq "}" or $c eq "]"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "}" or $c eq "]"
$c eq "}" $c eq "]"
Truth table0%0/3
A or B
A= $c eq "}"B= $c eq "]"
ABresult
1X1
011
000
1373 0 pop @stack;
1374 ▶ } elsif ($c eq ',' && @stack && $stack[-1]{object}) {
Condition0%0/6
$c eq "," and @stack and $stack[-1]{"object"}
!ll&&!rl&&r
000
$c eq "," and @stack
!ll&&!rl&&r
000
MC/DC0%0/3
$c eq "," and @stack and $stack[-1]{"object"}
$c eq "," @stack $stack[-1]{"object"}
Truth table0%0/4
A and B and C
A= $c eq ","B= @stackC= $stack[-1]{"object"}
ABCresult
0XX0
10X0
1100
1111
1375 0 $stack[-1]{want_key} = 1;
1376 }
1377 }
1378 }
1379 0 return \@order;
1380 }
1381  
1382 # _values_are_data_like( \@vals ) -> bool
1383 #
1384 # Return true when the values look like actual data (dates, numbers, free text)
1385 # _sniff_data_ext( $path ) -> $ext
1386 #
1387 # Peek at the content of $path and return the standard file extension that best
1388 # describes its format. Used when a remote file has a non-standard extension
1389 # (e.g. ".log") and we need to rename it so that _detect_file_info and
1390 # Database::Abstraction can discover it automatically.
1391 #
1392 # Detection order:
1393 # 1. SQLite magic bytes ("SQLite format 3") -> 'db'
1394 # 2. XML preamble ("<?xml" or "<" as first non-space character) -> 'xml'
1395 # 2a. "[" or "{" as first non-space character -> 'json'
1396 # 3. First data line contains tabs -> 'tsv'
1397 # 4. First data line contains pipes -> 'psv'
1398 # 5. Fallback -> 'csv'
1399 8 ▶sub _sniff_data_ext {
Subroutine _sniff_data_ext: called CC=10 SCAR=23.2
1400 8 my $path = $_[0];
1401 8 ▶ open(my $fh, '<:raw', $path) or return 'csv';
Branch: 1 branch
BranchTrueFalseTotal
&Database::BI::Model::DataSource::open(my $fh, "<:raw", $path) or return "csv" 8 0 8
1402 8 my $header = '';
1403 8 read $fh, $header, 20;
1404 8 close $fh;
1405 8 ▶ return 'db' if $header =~ /\ASQLite format/;
Branch: 1 branch
BranchTrueFalseTotal
if $header =~ /\ASQLite format/ 1 7 8
1406 7 ▶ return 'xml' if $header =~ /\A\s*<\?xml/i;
Branch: 1 branch
BranchTrueFalseTotal
if $header =~ /\A\s*<\?xml/i 1 6 7
1407 6 ▶ return 'xml' if $header =~ /\A\s*</;
Branch: 1 branch
BranchTrueFalseTotal
if $header =~ /\A\s*</ 1 5 6
1408 5 ▶ return 'json' if $header =~ /\A\s*[\[{]/;
Branch: 1 branch
BranchTrueFalseTotal
if $header =~ /\A\s*[\[{]/ 0 5 5
1409 5 ▶ open(my $lh, '<', $path) or return 'csv';
Branch: 1 branch
BranchTrueFalseTotal
&Database::BI::Model::DataSource::open(my $lh, "<", $path) or return "csv" 5 0 5
1410 5 my $line = <$lh>;
1411 5 close $lh;
1412 5 ▶ return 'csv' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 5 5
1413 5 ▶ return 'tsv' if $line =~ /\t/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\t/ 1 4 5
1414 4 ▶ return 'psv' if $line =~ /\|/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\|/ 1 3 4
1415 3 return 'csv';
1416 }
1417  
1418 # _values_are_data_like( \@vals ) -> bool
1419 #
1420 # Returns true when the values look like a row of real data (dates / numbers)
1421 # rather than column headers. Used to detect header-less CSV files where the
1422 # first line is a data row. At least one value must match a date or numeric
1423 # pattern -- a row of plain hyphenated identifiers (e.g. "First-Name") is NOT
1424 # considered data-like.
1425 17 ▶sub _values_are_data_like {
Subroutine _values_are_data_like: called CC=6 SCAR=17.9
1426 17 my $vals = $_[0];
1427 17 for my $v (@{$vals}) {
1428 23 ▶ return 1 if $v =~ /\A\d{4}-\d{2}-\d{2}\z/; # YYYY-MM-DD
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\A\d{4}-\d{2}-\d{2}\z/ 11 12 23
1429 12 ▶ return 1 if $v =~ /\A\d{1,2}\/\d{1,2}\/\d{4}\z/; # M/D/YYYY or D/M/YYYY
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ m[\A\d{1,2}/\d{1,2}/\d{4}\z] 1 11 12
1430 11 ▶ return 1 if $v =~ /\A[+\-]\d[\d,]*(?:\.\d+)?\z/; # signed numeric (e.g. -1,075.13)
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\A[+\-]\d[\d,]*(?:\.\d+)?\z/ 1 10 11
1431 10 ▶ return 1 if $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/; # accounting negative (e.g. (75.13))
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/ 1 9 10
1432 }
1433 3 return 0;
1434 }
1435  
1436 # _unsafe_sort( @args ) -> (\%rest, $col, $dir, $limit, $offset) | ()
1437 #
1438 # Purpose: Recognise a selectall_arrayref call that sorts on a column D::A will
1439 # not sort on, such as "First Name".
1440 # Entry: The call's arguments: a flat list or one hashref (Database::Join's
1441 # spill path), or anything else, which is not ours to take apart.
1442 # Exit: The remaining criteria, the column, ASC or DESC, and the page. Empty
1443 # for every other call.
1444 969 ▶sub _unsafe_sort {
Subroutine _unsafe_sort: called CC=11 SCAR=24.0
1445 969 my @args = @_;
1446 969 ▶ my $opts = (@args == 1 && ref $args[0] eq 'HASH') ? { %{ $args[0] } }
Branch: 2 branches
BranchTrueFalseTotal
@args == 1 && ref $args[0] eq 'HASH' ? : 41 928 969
@args && @args % 2 == 0 ? : 19 909 928
Condition100%6/6
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
926241
@args and @args % 2 == 0
!ll&&!rl&&r
907219
MC/DC100%2/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
MC/DC100%2/2
@args and @args % 2 == 0
@args @args % 2 == 0
Truth table100%3/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= @argsB= @args % 2 == 0
ABresult
0X0
100
111
1447 : (@args && @args % 2 == 0) ? { @args }
1448 : undef;
1449 969 ▶ return unless $opts && defined $opts->{sort_by};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts and defined $opts->{"sort_by"} 960 9 969
Condition100%3/3
$opts and defined $opts->{"sort_by"}
!ll&&!rl&&r
909519
MC/DC100%2/2
$opts and defined $opts->{"sort_by"}
$opts defined $opts->{"sort_by"}
Truth table100%3/3
A and B
A= $optsB= defined $opts->{"sort_by"}
ABresult
0X0
100
111
1450 my ($col, $dir) = ref $opts->{sort_by} eq 'ARRAY'
1451 9 ▶ ? @{ $opts->{sort_by} }[0, 1] : ($opts->{sort_by}, undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'sort_by'} eq 'ARRAY' ? : 5 4 9
1452 9 ▶ return unless defined $col && $col !~ $DA_SORTABLE_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col and not $col =~ /$DA_SORTABLE_RE/ 4 5 9
Condition100%3/3
defined $col and not $col =~ /$DA_SORTABLE_RE/
!ll&&!rl&&r
135
MC/DC100%2/2
defined $col and not $col =~ /$DA_SORTABLE_RE/
defined $col not $col =~ /$DA_SORTABLE_RE/
Truth table100%3/3
A and not(B)
A= defined $colB= not $col =~ /$DA_SORTABLE_RE/
ABresult
0X0
100
111
1453 5 my ($limit, $offset) = delete @{$opts}{qw(limit offset)};
1454 5 delete $opts->{sort_by};
1455 5 ▶ return ($opts, $col, uc($dir // 'ASC'), $limit, $offset);
Condition100%2/2
$dir // "ASC"
l!l
23
MC/DC100%1/1
$dir // "ASC"
$dir
Truth table100%2/2
A
A= $dir
Aresult
11
00
1456 }
1457  
1458 # _sorted_page( \@rows, $col, $dir, $limit, $offset ) -> \@rows
1459 #
1460 # Purpose: Do what D::A would have done for an _unsafe_sort call: sort as
1461 # strings, then take the page.
1462 # Notes: exists, because SQL-backed rows are locked hashes and a read of a
1463 # column they do not have would croak. An unknown column does not sort.
1464 # A new array, because the rows may be D::A's own cached list.
1465 11 ▶sub _sorted_page {
Subroutine _sorted_page: called CC=14 SCAR=26.7
1466 11 my ($rows, $col, $dir, $limit, $offset) = @_;
1467 11 ▶ my @page = @{ $rows // [] };
Condition100%2/2
$rows // []
l!l
101
MC/DC100%1/1
$rows // []
$rows
Truth table100%2/2
A
A= $rows
Aresult
11
00
1468 11 ▶ if (@page && exists $page[0]{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@page and exists $page[0]{$col}) 7 4 11
Condition100%3/3
@page and exists $page[0]{$col}
!ll&&!rl&&r
137
MC/DC100%2/2
@page and exists $page[0]{$col}
@page exists $page[0]{$col}
Truth table100%3/3
A and B
A= @pageB= exists $page[0]{$col}
ABresult
0X0
100
111
1469 @page = $dir eq 'DESC'
1470 3 ▶ ? sort { ($b->{$col} // '') cmp ($a->{$col} // '') } @page
Condition50%2/4
$Database::BI::Model::DataSource::b->{$col} // ""
l!l
30
$Database::BI::Model::DataSource::a->{$col} // ""
l!l
30
MC/DC0%0/1
$Database::BI::Model::DataSource::b->{$col} // ""
$Database::BI::Model::DataSource::b->{$col}
MC/DC0%0/1
$Database::BI::Model::DataSource::a->{$col} // ""
$Database::BI::Model::DataSource::a->{$col}
Truth table50%1/2
A
A= $Database::BI::Model::DataSource::b->{$col}
Aresult
11
00
Truth table50%1/2
A
A= $Database::BI::Model::DataSource::a->{$col}
Aresult
11
00
1471 7 ▶ : sort { ($a->{$col} // '') cmp ($b->{$col} // '') } @page;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'DESC' ? : 1 6 7
Condition50%2/4
$Database::BI::Model::DataSource::a->{$col} // ""
l!l
180
$Database::BI::Model::DataSource::b->{$col} // ""
l!l
180
MC/DC0%0/1
$Database::BI::Model::DataSource::a->{$col} // ""
$Database::BI::Model::DataSource::a->{$col}
MC/DC0%0/1
$Database::BI::Model::DataSource::b->{$col} // ""
$Database::BI::Model::DataSource::b->{$col}
Truth table50%1/2
A
A= $Database::BI::Model::DataSource::a->{$col}
Aresult
11
00
Truth table50%1/2
A
A= $Database::BI::Model::DataSource::b->{$col}
Aresult
11
00
1472 }
1473 11 ▶ splice @page, 0, $offset if $offset && $offset =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $offset and $offset =~ /\A\d+\z/ 2 9 11
Condition100%3/3
$offset and $offset =~ /\A\d+\z/
!ll&&!rl&&r
812
MC/DC100%2/2
$offset and $offset =~ /\A\d+\z/
$offset $offset =~ /\A\d+\z/
Truth table100%3/3
A and B
A= $offsetB= $offset =~ /\A\d+\z/
ABresult
0X0
100
111
1474 11 ▶ splice @page, $limit if defined $limit && $limit =~ /\A\d+\z/ && $limit < @page;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and $limit =~ /\A\d+\z/ and $limit < @page 4 7 11
Condition83%5/6
defined $limit and $limit =~ /\A\d+\z/ and $limit < @page
!ll&&!rl&&r
704
defined $limit and $limit =~ /\A\d+\z/
!ll&&!rl&&r
614
MC/DC66%2/3
defined $limit and $limit =~ /\A\d+\z/ and $limit < @page
defined $limit $limit =~ /\A\d+\z/ $limit < @page
Truth table75%3/4
A and B and C
A= defined $limitB= $limit =~ /\A\d+\z/C= $limit < @page
ABCresult
0XX0
10X0
1100
1111
1475 11 return \@page;
1476 }
1477  
1478 # _split_csv_line( $line, $sep ) -> @fields
1479 #
1480 # One line of a delimited file as its fields, so that a separator inside quotes
1481 # does not end a field. A line that does not parse (a quoted field carrying on
1482 # over the next line) is split on the separator instead: this is only used to
1483 # look at the shape of a file.
1484 1058 ▶sub _split_csv_line {
Subroutine _split_csv_line: called CC=3 SCAR=11.0
1485 1058 my ($line, $sep) = @_;
1486 1058 require Text::CSV;
1487 1058 my $csv = Text::CSV->new({ binary => 1, sep_char => $sep, allow_loose_quotes => 1 });
1488 1058 ▶ return $csv->fields if $csv && $csv->parse($line);
Branch: 1 branch
BranchTrueFalseTotal
if $csv and $csv->parse($line) 1057 1 1058
Condition66%2/3
$csv and $csv->parse($line)
!ll&&!rl&&r
011057
MC/DC50%1/2
$csv and $csv->parse($line)
$csv $csv->parse($line)
Truth table66%2/3
A and B
A= $csvB= $csv->parse($line)
ABresult
0X0
100
111
1489 1 return split /\Q$sep\E/, $line, -1;
1490 }
1491  
1492 # _synthesize_col_names( \@vals ) -> @names
1493 #
1494 # Infer a safe SQL identifier for each positional value by examining its
1495 # content: ISO dates become "Date", numeric/currency values become "Amount",
1496 # and free text becomes "Description". Duplicate types are disambiguated with
1497 # a numeric suffix (Date, Date2, Date3, ...).
1498 15 ▶sub _synthesize_col_names {
Subroutine _synthesize_col_names: called CC=7 SCAR=19.5
1499 15 my $vals = $_[0];
1500 15 my %type_count;
1501 my @names;
1502 15 for my $v (@{$vals}) {
1503 43 my $type;
1504 43 ▶ if ($v =~ /\A\d{4}-\d{2}-\d{2}\z/ || $v =~ /\A\d{1,2}\/\d{1,2}\/\d{4}\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v =~ /\A\d{4}-\d{2}-\d{2}\z/ or $v =~ m[\A\d{1,2}/\d{1,2}/\d{4}\z]) { } 14 29 43
elsif ($v =~ /\A[+\-]?\d[\d,]*(?:\.\d+)?\z/ or $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/) { } 14 15 29
Condition100%3/3
$v =~ /\A\d{4}-\d{2}-\d{2}\z/ or $v =~ m[\A\d{1,2}/\d{1,2}/\d{4}\z]
l!l&&r!l&&!r
13129
MC/DC100%2/2
$v =~ /\A\d{4}-\d{2}-\d{2}\z/ or $v =~ m[\A\d{1,2}/\d{1,2}/\d{4}\z]
$v =~ /\A\d{4}-\d{2}-\d{2}\z/ $v =~ m[\A\d{1,2}/\d{1,2}/\d{4}\z]
Truth table100%3/3
A or B
A= $v =~ /\A\d{4}-\d{2}-\d{2}\z/B= $v =~ m[\A\d{1,2}/\d{1,2}/\d{4}\z]
ABresult
1X1
011
000
1505 14 $type = 'Date';
1506 ▶ } elsif ($v =~ /\A[+\-]?\d[\d,]*(?:\.\d+)?\z/ || $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/) {
Condition66%2/3
$v =~ /\A[+\-]?\d[\d,]*(?:\.\d+)?\z/ or $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/
l!l&&r!l&&!r
14015
MC/DC50%1/2
$v =~ /\A[+\-]?\d[\d,]*(?:\.\d+)?\z/ or $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/
$v =~ /\A[+\-]?\d[\d,]*(?:\.\d+)?\z/ $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/
Truth table66%2/3
A or B
A= $v =~ /\A[+\-]?\d[\d,]*(?:\.\d+)?\z/B= $v =~ /\A\(\d[\d,]*(?:\.\d+)?\)\z/
ABresult
1X1
011
000
1507 14 $type = 'Amount';
1508 } else {
1509 15 $type = 'Description';
1510 }
1511 43 $type_count{$type}++;
1512 43 ▶ push @names, $type_count{$type} == 1 ? $type : $type . $type_count{$type};
Branch: 1 branch
BranchTrueFalseTotal
$type_count{$type} == 1 ? : 38 5 43
1513 }
1514 15 return @names;
1515 }
1516  
1517 # _copy_with_header( $self, \%headerless, \@names, $sep, $stem ) -> ($dir, $id, $size)
1518 #
1519 # Purpose: Give D::A a file it can read for one that has no header: a copy in a
1520 # temporary directory, under the same name, starting with @names.
1521 # Entry: \%headerless is _detect_file_info's { path, ext }.
1522 # Exit: The temporary directory, the column to use as D::A's id and the
1523 # copy's size. Croaks error_backend_init if the file does not parse.
1524 # Notes: Rows are parsed, not copied as text, so each has one value per name:
1525 # a short row is padded, and a long one's extra fields go back into the
1526 # last value, so an unquoted comma in a description is not lost.
1527 # D::A drops a row whose id is empty or starts with '#', so the id is
1528 # the column with the fewest such values, and normally has none.
1529 # The copy is read and written with the escape the file uses
1530 # (_csv_escape_char), so D::A, which chooses the same way, reads the
1531 # copy with it too.
1532 12 ▶sub _copy_with_header :Protected {
Subroutine _copy_with_header: called CC=14 SCAR=26.4
1533 12 my ($self, $headerless, $names, $sep, $stem) = @_;
1534 12 require File::Temp;
1535 12 require Text::CSV;
1536  
1537 12 my $tmp = File::Temp->newdir(CLEANUP => 1);
1538 12 my $copy = File::Spec->catfile("$tmp", "$stem.$headerless->{ext}");
1539 12 my $escape = _csv_escape_char($headerless->{path});
1540 12 my $in = Text::CSV->new({ binary => 1, sep_char => $sep, allow_loose_quotes => 1, escape_char => $escape });
1541 12 my $out = Text::CSV->new({ binary => 1, sep_char => $sep, eol => "\n", escape_char => $escape });
1542  
1543 12 open my $ifh, '<:raw', $headerless->{path};
1544 11 open my $ofh, '>:raw', $copy;
1545 11 $out->print($ofh, $names);
1546  
1547 11 my @unusable = (0) x @{$names};
1548 11 my $first = 1;
1549 11 ▶ while (my $row = $in->getline($ifh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $in->getline($ifh)) 26 11 37
1550 # A byte-order mark: Text::CSV gives it back decoded (U+FEFF), and the
1551 # rest of the row with it, or as its three bytes.
1552 26 ▶ $row->[0] =~ s/\A(?:\x{FEFF}|\xEF\xBB\xBF)// if $first-- > 0 && defined $row->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $first-- > 0 and defined $row->[0] 10 16 26
Condition66%2/3
$first-- > 0 and defined $row->[0]
!ll&&!rl&&r
16010
MC/DC50%1/2
$first-- > 0 and defined $row->[0]
$first-- > 0 defined $row->[0]
Truth table66%2/3
A and B
A= $first-- > 0B= defined $row->[0]
ABresult
0X0
100
111
1553 26 ▶ next if @{$row} == 1 && $row->[0] !~ /\S/; # a blank line
Branch: 1 branch
BranchTrueFalseTotal
if @{$row;} == 1 and not $row->[0] =~ /\S/ 2 24 26
Condition66%2/3
@{$row;} == 1 and not $row->[0] =~ /\S/
!ll&&!rl&&r
2402
MC/DC50%1/2
@{$row;} == 1 and not $row->[0] =~ /\S/
@{$row;} == 1 not $row->[0] =~ /\S/
Truth table66%2/3
A and not(B)
A= @{$row;} == 1B= not $row->[0] =~ /\S/
ABresult
0X0
100
111
1554 24 ▶ if (@{$row} > @{$names}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$row;} > @{$names;}) 2 22 24
1555 2 my @extra = splice @{$row}, @{$names} - 1;
1556 2 push @{$row}, join($sep, @extra);
1557 }
1558 24 push @{$row}, ('') x (@{$names} - @{$row});
1559 24 for (@{$row}) { s/\A\s+//; s/\s+\z// }
1560 24 $out->print($ofh, $row);
1561 24 for my $i (0 .. $#{$names}) {
1562 72 ▶ $unusable[$i]++ if $row->[$i] !~ /\S/ || $row->[$i] =~ /\A#/;
Branch: 1 branch
BranchTrueFalseTotal
if not $row->[$i] =~ /\S/ or $row->[$i] =~ /\A#/ 5 67 72
Condition100%3/3
not $row->[$i] =~ /\S/ or $row->[$i] =~ /\A#/
l!l&&r!l&&!r
4167
MC/DC100%2/2
not $row->[$i] =~ /\S/ or $row->[$i] =~ /\A#/
not $row->[$i] =~ /\S/ $row->[$i] =~ /\A#/
Truth table100%3/3
not(A) or B
A= not $row->[$i] =~ /\S/B= $row->[$i] =~ /\A#/
ABresult
1X1
011
000
1563 }
1564 }
1565 # eof alone is no proof: it is also true after an unterminated quote. Code
1566 # 2012 is the plain end of the file.
1567 11 my ($code, $why) = $in->error_diag;
1568 11 close $ifh;
1569 11 close $ofh;
1570 11 ▶ croak $self->_msg('error_backend_init', $stem, "cannot parse $headerless->{path}: $why")
Branch: 1 branch
BranchTrueFalseTotal
if $code and $code != $CSV_EOF 2 9 11
Condition66%2/3
$code and $code != $CSV_EOF
!ll&&!rl&&r
092
MC/DC50%1/2
$code and $code != $CSV_EOF
$code $code != $CSV_EOF
Truth table66%2/3
A and B
A= $codeB= $code != $CSV_EOF
ABresult
0X0
100
111
1571 if $code && $code != $CSV_EOF;
1572  
1573 9 ▶ my ($best) = sort { $unusable[$a] <=> $unusable[$b] || $a <=> $b } 0 .. $#{$names};
Condition66%2/3
$unusable[$a] <=> $unusable[$b] || $a <=> $b
l!l&&r!l&&!r
5210
MC/DC0%0/2
$unusable[$a] <=> $unusable[$b] || $a <=> $b
$unusable[$a] <=> $unusable[$b] $a <=> $b
Truth table66%2/3
A || B
A= $unusable[$a] <=> $unusable[$b]B= $a <=> $b
ABresult
1X1
011
000
1574 9 return ($tmp, $names->[$best], -s $copy);
1575 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1576  
1577 # _cache_key( $self ) -> $key | undef
1578 #
1579 # Purpose: Derive a stable cache key for this DataSource's full result set.
1580 # URL tables use a fixed key (TTL handles invalidation).
1581 # File tables encode the file's mtime in the key so a changed file
1582 # naturally produces a miss -- the stale entry is orphaned and evicts
1583 # passively when the CHI driver reclaims memory.
1584 # Entry: $self->{_cache} must be defined (caller checks this before calling).
1585 # Exit: Returns a non-empty string key, or undef when no key is derivable
1586 # (no URL, no file path on disk).
1587 983 ▶sub _cache_key :Protected {
Subroutine _cache_key: called CC=6 SCAR=18.0
1588 983 my $self = shift;
1589  
1590 983 ▶ if (defined $self->{_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_url"}) 5 978 983
1591 5 ▶ my $idx = $self->{_html_table_index} // 0;
Condition50%1/2
$self->{"_html_table_index"} // 0
l!l
50
MC/DC0%0/1
$self->{"_html_table_index"} // 0
$self->{"_html_table_index"}
Truth table50%1/2
A
A= $self->{"_html_table_index"}
Aresult
11
00
1592 # Include the table index in the key: a different index on the same URL
1593 # selects a different table from the page and must not share a cache entry.
1594 5 return 'bi:url:' . $self->{_url} . ':' . $idx;
1595 }
1596  
1597 978 ▶ if (defined $self->{_file_path} && -f $self->{_file_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_file_path"} and -f $self->{"_file_path"}) 976 2 978
Condition100%3/3
defined $self->{"_file_path"} and -f $self->{"_file_path"}
!ll&&!rl&&r
11976
MC/DC100%2/2
defined $self->{"_file_path"} and -f $self->{"_file_path"}
defined $self->{"_file_path"} -f $self->{"_file_path"}
Truth table100%3/3
A and B
A= defined $self->{"_file_path"}B= -f $self->{"_file_path"}
ABresult
0X0
100
111
1598 976 my $mtime = (stat($self->{_file_path}))[9];
1599 return defined $mtime
1600 976 ▶ ? 'bi:file:' . $self->{_file_path} . ':' . $mtime
Branch: 1 branch
BranchTrueFalseTotal
defined $mtime ? : 976 0 976
1601 : undef;
1602 }
1603  
1604 2 return undef;
1605 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1606  
1607 # _init_backend( $self ) -> void
1608 #
1609 # Strategy: Database::Abstraction is designed as a base class where the
1610 # lowercased package name maps to the data file in the directory
1611 # (e.g. "Database::BI::_DB::Sales" -> data/sales.csv or data/sales.db).
1612 # We synthesise an ephemeral subclass at runtime so DataSource remains
1613 # fully table-agnostic and callers never need to touch Database::Abstraction
1614 # directly. In Phase 2, this method can be replaced with Database::Join
1615 # instantiation without any change to the public API.
1616 #
1617 # no_entry => 1: a BI viewer wants every row; we do not need O(1) keyed
1618 # lookups on a primary key. This stores data as an arrayref instead of a
1619 # hashref, which the fast-track path in selectall_arrayref returns directly.
1620 1275 ▶sub _init_backend :Protected {
Subroutine _init_backend: called CC=53 SCAR=46.2
1621 1275 my $self = shift;
1622 1275 my $table = $self->{_table}; # sanitized: used for pkg name and D::A table param
1623 1275 ▶ my $raw_table = $self->{_raw_table} // $table; # original: used for file lookup and dbname
Condition33%1/3
$self->{"_raw_table"} // $table
l!l&&r!l&&!r
127500
MC/DC0%0/2
$self->{"_raw_table"} // $table
$self->{"_raw_table"} $table
Truth table33%1/3
A // B
A= $self->{"_raw_table"}B= $table
ABresult
1X1
011
000
1624 1275 my $dir = $self->{_directory};
1625  
1626 # Remote file path (host is set): download via SFTP to a local temp directory,
1627 # then proceed with normal local processing on the downloaded copy.
1628 # Net::SFTP::Foreign is loaded lazily so non-remote usage has no extra deps.
1629 1275 ▶ if (defined $self->{_host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_host"}) 6 1269 1275
1630 6 ▶ eval { require Net::SFTP::Foreign }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::SFTP::Foreign } } or croak($self->_msg("error_backend_init", $table, "Net::SFTP::Foreign is not installed (required for remote file access)")) 6 0 6
1631 or croak $self->_msg('error_backend_init', $table,
1632 'Net::SFTP::Foreign is not installed (required for remote file access)');
1633 6 require File::Temp;
1634 # Split optional user@host into ($user, $host).
1635 my ($user, $host) = $self->{_host} =~ /\A([^@]+)\@(.+)\z/
1636 6 ▶ ? ($1, $2) : (undef, $self->{_host});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_host'} =~ /\A([^\@]+)\@(.+)\z/ ? : 1 5 6
1637 6 my $tmpdir = File::Temp->newdir(CLEANUP => 1);
1638 6 my @remote_exts = qw(csv tsv psv xlsx xls sql sqlite sqlite3 db xml json);
1639 # Try the caller-supplied extension first (e.g. .log), then fall back.
1640 6 ▶ if (defined $self->{_file_ext}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_file_ext"}) 6 0 6
1641 6 my $hint = lc $self->{_file_ext};
1642 6 @remote_exts = ($hint, grep { $_ ne $hint } @remote_exts);
1643 }
1644 # One SFTP connection shared across all extension attempts.
1645 6 ▶ my $sftp = eval { Net::SFTP::Foreign->new($host,
Branch: 1 branch
BranchTrueFalseTotal
defined $user ? : 1 5 6
1646 defined $user ? (user => $user) : (),
1647 timeout => 10,
1648 ) };
1649 6 ▶ croak $self->_msg('error_backend_init', $table,
Branch: 2 branches
BranchTrueFalseTotal
unless defined $sftp and not $sftp->error 0 6 6
$sftp ? : 0 0 0
Condition16%1/6
defined $sftp and not $sftp->error
!ll&&!rl&&r
006
$@ || ($sftp ? $sftp->error : 'unknown')
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $sftp and not $sftp->error
defined $sftp not $sftp->error
MC/DC0%0/2
$@ || ($sftp ? $sftp->error : 'unknown')
$@ ($sftp ? $sftp->error : 'unknown')
Truth table33%1/3
A and not(B)
A= defined $sftpB= not $sftp->error
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $@B= ($sftp ? $sftp->error : 'unknown')
ABresult
1X1
011
000
1650 "could not connect to $host via SFTP: " . ($@ || ($sftp ? $sftp->error : 'unknown')))
1651 unless defined $sftp && !$sftp->error;
1652 6 my $fetched_ext;
1653 6 for my $ext (@remote_exts) {
1654 17 my $remote_path = "$dir/$raw_table.$ext";
1655 17 my $local_file = File::Spec->catfile("$tmpdir", "$raw_table.$ext");
1656 17 $sftp->get($remote_path, $local_file);
1657 # Verify the file landed on disk; sftp->error may be set on ENOENT.
1658 17 ▶ next if $sftp->error || !-f $local_file || !-s $local_file;
Branch: 1 branch
BranchTrueFalseTotal
if $sftp->error or not -f $local_file or not -s $local_file 12 5 17
Condition66%4/6
$sftp->error or not -f $local_file or not -s $local_file
l!l&&r!l&&!r
1205
$sftp->error or not -f $local_file
l!l&&r!l&&!r
0125
MC/DC33%1/3
$sftp->error or not -f $local_file or not -s $local_file
$sftp->error not -f $local_file not -s $local_file
Truth table50%2/4
A or not(B) or not(C)
A= $sftp->errorB= not -f $local_fileC= not -s $local_file
ABCresult
1XX1
01X1
0011
0000
1659 5 $fetched_ext = $ext;
1660 5 last;
1661 }
1662 6 ▶ croak $self->_msg('error_backend_init', $table,
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fetched_ext 1 5 6
1663 "could not fetch any supported file from $self->{_host}:$dir/$raw_table.*")
1664 unless defined $fetched_ext;
1665 5 $self->{_remote_tmpdir} = $tmpdir; # prevents cleanup until $self is destroyed
1666  
1667 # If the downloaded file has a non-standard extension (e.g. .log), rename
1668 # it to one that _detect_file_info and D::A can discover automatically.
1669 # We do this by sniffing the first bytes/line for format signatures.
1670 5 my %KNOWN_EXT = map { $_ => 1 }
1671 qw(csv tsv psv xlsx xls sql sqlite sqlite3 db xml json);
1672 5 ▶ unless ($KNOWN_EXT{lc $fetched_ext}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($KNOWN_EXT{lc $fetched_ext}) 1 4 5
1673 1 my $orig_file = File::Spec->catfile("$tmpdir", "$raw_table.$fetched_ext");
1674 1 my $std_ext = _sniff_data_ext($orig_file);
1675 1 ▶ if ($std_ext ne lc $fetched_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($std_ext ne lc $fetched_ext) 1 0 1
1676 1 my $new_file = File::Spec->catfile("$tmpdir", "$raw_table.$std_ext");
1677 8 ▶ { no autodie; rename $orig_file, $new_file }
Subroutine BEGIN: called CC=1 SCAR=0.0
1678 1 $fetched_ext = $std_ext;
1679 }
1680 }
1681  
1682 5 $dir = "$tmpdir"; # switch to local temp dir for all subsequent processing
1683 }
1684  
1685 1274 require Database::Abstraction;
1686  
1687 1274 my $pkg = 'Database::BI::_DB::' . ucfirst($table);
1688 {
1689 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1690 1274 ▶ push @{"${pkg}::ISA"}, 'Database::Abstraction'
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->isa("Database::Abstraction") 74 1200 1274
1691 unless $pkg->isa('Database::Abstraction');
1692 }
1693  
1694 1274 my $info = _detect_file_info($dir, $raw_table, $self->{_internal_table});
1695  
1696 # Probe for the actual file on disk so _cache_key can compute its mtime.
1697 # This runs before the early-return paths so even empty files get a path.
1698 1274 for my $e (qw(csv tsv psv sql sqlite sqlite3 xml db xlsx xls json)) {
1699 2825 my $p = File::Spec->catfile($dir, "$raw_table.$e");
1700 2825 ▶ if (-f $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $p) 1272 1553 2825
1701 1272 $self->{_file_path} = File::Spec->rel2abs($p);
1702 1272 last;
1703 }
1704 }
1705  
1706 # A JSON file's lists of objects are its tables, as a SQLite file's are.
1707 $self->{_sqlite_tables} = $info->{_json_lists}
1708 0 ▶ if $info->{_json_lists} && @{ $info->{_json_lists} };
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"_json_lists"} and @{$info->{"_json_lists"};} 0 1274 1274
Condition33%1/3
$info->{"_json_lists"} and @{$info->{"_json_lists"};}
!ll&&!rl&&r
127400
MC/DC0%0/2
$info->{"_json_lists"} and @{$info->{"_json_lists"};}
$info->{"_json_lists"} @{$info->{"_json_lists"};}
Truth table33%1/3
A and B
A= $info->{"_json_lists"}B= @{$info->{"_json_lists"};}
ABresult
0X0
100
111
1709  
1710 # BerkeleyDB file detected by magic bytes, but tie() failed (version
1711 # incompatibility -- e.g. the file uses BDB format 9 but the installed
1712 # libdb only supports an older format). Croak now so D::A never falls
1713 # through to its CSV path and reads the entire binary file as text.
1714 1274 ▶ if ($info->{_unknown_binary_db}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"_unknown_binary_db"}) 0 1274 1274
1715 0 croak $self->_msg('error_unsupported_db', $raw_table);
1716 }
1717  
1718 # 0-byte file: skip D::A/DBI entirely. D::A on an empty file falls through
1719 # to DBD::CSV, whose error handling corrupts DBI's Errstr SV and triggers an
1720 # XS assertion failure at cleanup on DEBUGGING perls (DBI 1.651, perl 5.44).
1721 1274 ▶ if ($info->{_file_is_empty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"_file_is_empty"}) 0 1274 1274
1722 0 $self->{_columns} = [];
1723 0 $self->{_id_col} = 'entry';
1724 0 $self->{_file_is_empty} = 1;
1725 0 return;
1726 }
1727  
1728 # Header-less CSV/PSV: D::A needs a header, so it reads a copy that has one.
1729 # $self->{_file_path} stays the real file, whose mtime is what counts.
1730 1274 ▶ if (my $headerless = $info->{_headerless}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $headerless = $info->{"_headerless"}) 9 1265 1274
1731 my ($tmp, $id, $size) = $self->_copy_with_header(
1732 9 $headerless, $info->{columns}, $info->{sep_char}, $raw_table);
1733 8 $self->{_headerless_tmpdir} = $tmp; # prevents cleanup until $self is destroyed
1734 8 $info->{id} = $id;
1735 8 $info->{file_size} = $size;
1736 8 $dir = "$tmp";
1737 }
1738  
1739 # A byte-order mark: D::A reads a copy without it, or the first column's
1740 # name would start with it and match nothing a reader types.
1741 # $self->{_file_path} stays the real file, whose mtime is what counts.
1742 1273 ▶ if (my $bom = delete $info->{_bom}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bom = delete $info->{"_bom"}) 0 1273 1273
1743 0 require File::Temp;
1744 0 my $tmp = File::Temp->newdir(CLEANUP => 1);
1745 0 my $copy = File::Spec->catfile("$tmp", "$raw_table.$bom->{ext}");
1746 0 open my $ifh, '<:raw', $bom->{path};
1747 0 open my $ofh, '>:raw', $copy;
1748 0 read $ifh, my $mark, 3;
1749 0 ▶ while (read $ifh, my $chunk, 65_536) { print {$ofh} $chunk }
Branch: 1 branch
BranchTrueFalseTotal
if (&Database::BI::Model::DataSource::read($ifh, \my $chunk, 65536)) 0 0 0
1750 0 close $ifh;
1751 0 close $ofh;
1752 0 $self->{_bom_tmpdir} = $tmp; # prevents cleanup until $self is destroyed
1753 0 $info->{file_size} = -s $copy;
1754 0 $dir = "$tmp";
1755 }
1756  
1757 # JSON: D::A reads a flat copy, a list of rows, whatever shape the file
1758 # had (see _json_table). $self->{_file_path} stays the real file.
1759 1273 ▶ if (my $json = delete $info->{_json}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = delete $info->{"_json"}) 0 1273 1273
1760 0 require File::Temp;
1761 0 require Mojo::JSON;
1762 0 my $tmp = File::Temp->newdir(CLEANUP => 1);
1763 0 my $copy = File::Spec->catfile("$tmp", "$raw_table.json");
1764 0 open my $ofh, '>:raw', $copy;
1765 0 print {$ofh} Mojo::JSON::encode_json($json->{rows});
1766 0 close $ofh;
1767 0 $self->{_json_tmpdir} = $tmp; # prevents cleanup until $self is destroyed
1768 0 $self->{_json_list} = $json->{list};
1769 0 $dir = "$tmp";
1770 }
1771  
1772 # _detect_file_info returns undef for id when every column header contains
1773 # characters that are not safe SQL identifiers (spaces, hyphens, etc.).
1774 # Falling back to the D::A default ('entry') would silently return 0 rows
1775 # since no 'entry' column exists. Croak with a human-readable message.
1776 # This guard applies only to CSV/TSV/PSV/SQLite/XML: the header-less and XLSX
1777 # paths always give an id.
1778 croak $self->_msg('error_no_safe_id', $table)
1779 1273 ▶ if exists $info->{columns} && !defined $info->{id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"columns"} and not defined $info->{"id"} 0 1273 1273
Condition66%2/3
exists $info->{"columns"} and not defined $info->{"id"}
!ll&&!rl&&r
23010430
MC/DC0%0/2
exists $info->{"columns"} and not defined $info->{"id"}
exists $info->{"columns"} not defined $info->{"id"}
Truth table66%2/3
A and not(B)
A= exists $info->{"columns"}B= not defined $info->{"id"}
ABresult
0X0
100
111
1780 1273 ▶ my $id_col = $info->{id} // 'entry';
Condition100%2/2
$info->{"id"} // "entry"
l!l
1043230
MC/DC100%1/1
$info->{"id"} // "entry"
$info->{"id"}
Truth table100%2/2
A
A= $info->{"id"}
Aresult
11
00
1781 1273 $self->{_id_col} = $id_col;
1782 1273 $self->{_columns} = $info->{columns}; # undef for SQLite/XML
1783  
1784 # D::A validates dbname as a SQL identifier and rejects names that contain
1785 # spaces or other characters that are illegal in SQL (e.g. "transactions for
1786 # Nigel"). This check fires at query time (inside selectall_arrayref) for
1787 # the DBI path used by XLSX, SQLite, and XML files -- after construction
1788 # succeeds -- so the error surfaces as error_fetch_failed, not error_backend_init.
1789 #
1790 # Fix: when the original filename stem was sanitized (raw_table != table),
1791 # create a temporary directory with a symlink that uses the safe name.
1792 # D::A opens the symlink, sees a space-free dbname, and builds valid SQL.
1793 # The temp-dir object is kept in $self so the symlink persists for the full
1794 # lifetime of this DataSource instance and is cleaned up automatically when
1795 # $self is destroyed.
1796 1273 my $dbname = $raw_table;
1797 1273 my $da_dir = $dir;
1798 1273 ▶ if ($raw_table ne $table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_table ne $table) 4 1269 1273
1799 4 my $safe_ext;
1800 4 for my $e (qw(xlsx xls db sql sqlite sqlite3 xml csv tsv psv json)) {
1801 34 ▶ $safe_ext = $e, last
Branch: 1 branch
BranchTrueFalseTotal
if -f "File::Spec"->catfile($dir, "$raw_table.$e") 2 32 34
1802 if -f File::Spec->catfile($dir, "$raw_table.$e");
1803 }
1804 4 ▶ if (defined $safe_ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $safe_ext) 2 2 4
1805 2 require File::Temp;
1806 2 my $tmp = File::Temp->newdir(CLEANUP => 1);
1807 2 my $abs_src = File::Spec->rel2abs(
1808 File::Spec->catfile($dir, "$raw_table.$safe_ext"));
1809 2 my $link = File::Spec->catfile("$tmp", "$table.$safe_ext");
1810 {
1811 8 ▶ no autodie; # symlink failure gives our message, not autodie's
Subroutine BEGIN: called CC=1 SCAR=0.0
1812 2 ▶ symlink($abs_src, $link)
Branch: 1 branch
BranchTrueFalseTotal
symlink $abs_src, $link or croak($self->_msg("error_backend_init", $table, "cannot create safe-name symlink for '$raw_table.${safe_ext}': $!")) 2 0 2
1813 or croak $self->_msg('error_backend_init', $table,
1814 "cannot create safe-name symlink for '$raw_table.$safe_ext': $!");
1815 }
1816 2 $self->{_tmpdir} = $tmp; # prevents cleanup until $self is destroyed
1817 2 $dbname = $table;
1818 2 $da_dir = "$tmp";
1819 }
1820 }
1821  
1822 # SQLite: _detect_file_info probed sqlite_master and returned the internal
1823 # table names. When $dbname (the filename stem or its safe alias) is not
1824 # among those names, auto-select the first user table (or use the caller-
1825 # supplied internal_table) and create a symlink from <target>.<ext> to the
1826 # original file so D::A can issue SQL against the real table name.
1827 # D::A uses 'dbname' to find the file and 'table' for the SELECT statement,
1828 # so both must be set to the actual table name when a mismatch is corrected.
1829 1273 my $da_table = $table; # D::A 'table' param -- controls the SQL table name
1830 1273 ▶ if (exists $info->{sqlite_tables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info->{"sqlite_tables"}) 228 1045 1273
1831 228 my @tbls = @{ $info->{sqlite_tables} };
1832 228 ▶ croak $self->_msg('error_no_tables', $raw_table) unless @tbls;
Branch: 1 branch
BranchTrueFalseTotal
unless @tbls 0 228 228
1833  
1834 # Store the full table list so callers (e.g. the tables_view action)
1835 # can detect multi-table databases after construction.
1836 228 $self->{_sqlite_tables} = \@tbls;
1837  
1838 # Determine target table: caller-specified or auto-detected.
1839 228 my $desired = $self->{_internal_table};
1840 228 my $target;
1841 228 ▶ if (defined $desired) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desired) { } 3 225 228
1842 3 my ($found) = grep { $_ eq $desired } @tbls;
1843 3 ▶ croak $self->_msg('error_table_not_found', $raw_table, $desired)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 1 2 3
1844 unless defined $found;
1845 2 ▶ if ($desired !~ $TABLE_NAME_RE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($desired =~ /$TABLE_NAME_RE/) 0 2 2
1846 0 my @others = grep { $_ =~ $TABLE_NAME_RE } @tbls;
1847 0 ▶ croak $self->_msg('error_table_name_unsafe', $desired, $raw_table,
Branch: 1 branch
BranchTrueFalseTotal
@others ? : 0 0 0
1848 (@others ? $self->_msg('error_table_name_others', join(', ', @others)) : ''),
1849 $desired, _safe_table_name($desired));
1850 }
1851 2 $target = $desired;
1852 } else {
1853 # D::A refuses a table name that is not a safe identifier, so the
1854 # first table it can open is chosen, not simply the first table.
1855 225 my @usable = grep { $_ =~ $TABLE_NAME_RE } @tbls;
1856 225 ▶ croak $self->_msg('error_no_usable_tables', $raw_table, join(', ', @tbls),
Branch: 1 branch
BranchTrueFalseTotal
unless @usable 0 225 225
1857 $tbls[0], _safe_table_name($tbls[0])) unless @usable;
1858 225 my ($match) = grep { $_ eq $dbname } @usable;
1859 225 ▶ $target = defined $match ? $match : $usable[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $match ? : 14 211 225
1860 }
1861  
1862 # When the target differs from dbname, create a symlink so D::A can
1863 # find the file by target name and issue SELECT * FROM <target>.
1864 # Always use a D::A-recognised SQLite extension (.sql, .sqlite, .sqlite3)
1865 # for the symlink name -- D::A does not use the DBI/SQLite backend for
1866 # .db files, so a customer.db symlink would be read as CSV. For the same
1867 # reason a SQLite .db file needs the symlink even when its table is named
1868 # after the file: orders.db holding a table called orders.
1869 227 my ($src_ext) = grep { -f File::Spec->catfile($dir, "$raw_table.$_") }
1870 qw(sql sqlite sqlite3 db);
1871 227 ▶ $src_ext //= 'sql';
Condition50%1/2
$src_ext //= "sql"
l!l
2270
MC/DC0%0/1
$src_ext //= "sql"
$src_ext
Truth table50%1/2
A
A= $src_ext
Aresult
11
00
1872 227 ▶ if ($target ne $dbname || $src_ext eq 'db') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target ne $dbname or $src_ext eq "db") 213 14 227
Condition66%2/3
$target ne $dbname or $src_ext eq "db"
l!l&&r!l&&!r
213014
MC/DC50%1/2
$target ne $dbname or $src_ext eq "db"
$target ne $dbname $src_ext eq "db"
Truth table66%2/3
A or B
A= $target ne $dbnameB= $src_ext eq "db"
ABresult
1X1
011
000
1873 213 ▶ my $link_ext = ($src_ext eq 'db') ? 'sql' : $src_ext;
Branch: 1 branch
BranchTrueFalseTotal
$src_ext eq 'db' ? : 206 7 213
1874 213 require File::Temp;
1875 213 my $tmp = File::Temp->newdir(CLEANUP => 1);
1876 213 my $abs_src = File::Spec->rel2abs(
1877 File::Spec->catfile($dir, "$raw_table.$src_ext"));
1878 213 my $link = File::Spec->catfile("$tmp", "$target.$link_ext");
1879 {
1880 8 ▶ no autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
1881 213 ▶ symlink($abs_src, $link)
Branch: 1 branch
BranchTrueFalseTotal
symlink $abs_src, $link or croak($self->_msg("error_backend_init", $raw_table, "cannot create table-name symlink for '$raw_table.${src_ext}': $!")) 213 0 213
1882 or croak $self->_msg('error_backend_init', $raw_table,
1883 "cannot create table-name symlink for '$raw_table.$src_ext': $!");
1884 }
1885 213 $self->{_tmpdir} = $tmp;
1886 213 $dbname = $target; # D::A uses this for the filename stem
1887 213 $da_table = $target; # D::A uses this for SELECT * FROM <table>
1888 213 $da_dir = "$tmp";
1889 }
1890 }
1891  
1892 1272 my $db = eval {
1893 $pkg->new({
1894 directory => $da_dir,
1895 table => $da_table,
1896 # D::A >= 0.41 uses the class-name suffix as dbname, not the table
1897 # parameter, so a package like Database::BI::_DB::Orders would look
1898 # for Orders.csv on a case-sensitive filesystem even when table =>
1899 # 'orders' is passed. Passing dbname explicitly keeps the filename
1900 # stem correct regardless of the package name or D::A version.
1901 # When the table name was sanitized (spaces, hyphens -> underscores),
1902 # $dbname is the safe name so D::A never sees illegal SQL characters,
1903 # and D::A finds the file via the symlink in $da_dir.
1904 dbname => $dbname,
1905 id => $id_col,
1906 no_entry => 1,
1907 defined($info->{sep_char}) ? (sep_char => $info->{sep_char}) : (),
1908 # Force the Text::xSV::Slurp path for CSV/TSV/PSV files: D::A's default
1909 # slurp threshold is 16 KB; larger files fall back to DBD::CSV, which
1910 # sanitizes column names (lowercases and replaces spaces with
1911 # underscores). Passing the actual file size ensures the slurp path
1912 # is always taken, so "Account Number" stays "Account Number" rather
1913 # than becoming "account_number".
1914 1272 ▶ defined($info->{file_size}) ? (max_slurp_size => $info->{file_size}) : (),
Branch: 2 branches
BranchTrueFalseTotal
defined $info->{'sep_char'} ? : 1043 229 1272
defined $info->{'file_size'} ? : 1270 2 1272
1915 });
1916 };
1917 1272 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1272 1272
1918 0 croak $self->_msg('error_backend_init', $table, $@);
1919 }
1920  
1921 1272 $self->{_db} = $db;
1922 1272 return;
1923 8 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1924  
1925 # ---------------------------------------------------------------------------
1926 # Public accessors
1927 # ---------------------------------------------------------------------------
1928  
1929 =head1 ACCESSORS
1930  
1931 =head2 table_name
1932  
1933 Returns the (lowercased) table name this instance was opened against.
1934  
1935 =head3 API SPECIFICATION
1936  
1937 =head4 INPUT
1938  
1939 None.
1940  
1941 =head4 OUTPUT
1942  
1943 Returns a C<SCALAR> string.
1944  
1945 =head3 MESSAGES
1946  
1947 None.
1948  
1949 =cut
1950  
1951 8 ▶sub table_name {
Subroutine table_name: called CC=1 SCAR=0.0
Pod documented
1952 8 my $self = shift;
1953 8 return $self->{_table};
1954 }
1955  
1956 =head2 sqlite_tables
1957  
1958 Returns an arrayref of internal table names when the backing file is a
1959 SQLite database, or of the lists of objects in a JSON file that is an object
1960 holding them (C<{"findings": [...], "hosts": [...]}>), or C<undef> for other
1961 sources. Set during C<_init_backend>.
1962  
1963 When the return value is a reference to a list with B<more than one
1964 element>, the caller should redirect the user to the schema browser
1965 (C</tables>) so they can pick which table to open, rather than
1966 silently opening the first one.
1967  
1968 Example:
1969  
1970 my $tbls = $source->sqlite_tables;
1971 if ($tbls && @{$tbls} > 1) {
1972 # multiple tables -- let the user choose
1973 print "Tables: ", join(', ', @{$tbls}), "\n";
1974 }
1975  
1976 =head3 API SPECIFICATION
1977  
1978 =head4 INPUT
1979  
1980 None.
1981  
1982 =head4 OUTPUT
1983  
1984 ARRAYREF of SCALAR # ['customer', 'product', 'sales'] for a 3-table SQLite file
1985 ARRAYREF of SCALAR # ['findings', 'hosts'] for a JSON file holding two lists
1986 undef # for CSV, TSV, PSV, XML, URL-backed, and a JSON list or keyed object
1987  
1988 A SQLite file's names are in the order returned by C<sqlite_master>
1989 (alphabetical), a JSON file's in the order the file gives them. It is set
1990 once during construction and never changes.
1991  
1992 =head3 MESSAGES
1993  
1994 None.
1995  
1996 =cut
1997  
1998 335 ▶sub sqlite_tables {
Subroutine sqlite_tables: called CC=1 SCAR=0.0
Pod documented
1999 335 my $self = shift;
2000 335 return $self->{_sqlite_tables};
2001 }
2002  
2003 =head2 split_path
2004  
2005 my ($file, $table) = Database::BI::Model::DataSource::split_path('/data/scan.json#hosts');
2006 # ('/data/scan.json', 'hosts')
2007  
2008 What a C<path:> spec names: a file and, after a C<#>, one of its tables (a
2009 SQLite file's table, or a JSON file's list). C<path:/data/store.db#sales> is
2010 the C<sales> table of C<store.db>, as C<table:store/sales> is when the file is
2011 in C<data_dir>. When the whole text names a file that exists, it is that
2012 file, so a file whose name has a C<#> in it still opens; and a remote path
2013 (C</../host/...>) is split the same way, since there is no file here to look
2014 for.
2015  
2016 =head3 API SPECIFICATION
2017  
2018 =head4 INPUT
2019  
2020 $path SCALAR what follows "path:" in a spec
2021  
2022 =head4 OUTPUT
2023  
2024 ($file, $table) $table undef when none is named; ('', undef) for undef
2025  
2026 =head3 MESSAGES
2027  
2028 None.
2029  
2030 =head3 FORMAL SPECIFICATION
2031  
2032 split_path == lambda p .
2033 if exists(p) then (p, undef)
2034 else if p = f ++ "#" ++ t /\ t /= "" /\ "#" not in t then (f, t)
2035 else (p, undef)
2036  
2037 =cut
2038  
2039 548 ▶sub split_path {
Subroutine split_path: called CC=4 SCAR=13.9
Pod documented
2040 548 my ($path) = @_;
2041 548 ▶ return ('', undef) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 548 548
2042 548 ▶ return ($path, undef) if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 518 30 548
2043 30 ▶ return ($1, $2) if $path =~ /\A(.+)#([^#\n]+)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\A(.+)#([^#\n]+)\z/s 2 28 30
2044 28 return ($path, undef);
2045 }
2046  
2047 =head2 json_lists
2048  
2049 my $lists = Database::BI::Model::DataSource->json_lists('/data/scan.json');
2050 # [ { name => 'findings', columns => ['detail', 'host', ...] },
2051 # { name => 'hosts', columns => ['base', 'host', ...] } ]
2052  
2053 The tables of a JSON file that is an object holding lists of objects: one
2054 entry per list, in the order the file gives them, with its columns in the
2055 order the file first names them. The schema browser (C</tables>) shows a
2056 card for each; C<< new(internal_table => $name) >> opens one.
2057  
2058 A list holding anything but objects is not a table, and an empty list counts
2059 only when no list has rows. Other values beside the lists (a count, a date,
2060 an object of totals) are not tables.
2061  
2062 =head3 API SPECIFICATION
2063  
2064 =head4 INPUT
2065  
2066 $class SCALAR the class (called as a class method)
2067 $path SCALAR a readable .json file
2068  
2069 =head4 OUTPUT
2070  
2071 ARRAYREF of HASHREF { name => SCALAR, columns => ARRAYREF of SCALAR }
2072 empty when the file holds no lists (a list of rows,
2073 an object of rows, a blank file)
2074  
2075 Croaks C<error_json_invalid> when the file is not JSON.
2076  
2077 =head3 MESSAGES
2078  
2079 C<error_json_invalid>.
2080  
2081 =head3 FORMAL SPECIFICATION
2082  
2083 json_lists == lambda path .
2084 let (text, data) = parse(path) in
2085 [ { name => k, columns => key_order(text, data.k) } | k <- list_keys(text, data) ]
2086  
2087 pre readable(path)
2088 post forall e in result . data.(e.name) : seq HASH
2089  
2090 =cut
2091  
2092 0 ▶sub json_lists {
Subroutine json_lists: not called CC=2 SCAR=17.9
Pod documented
2093 0 my ($class, $path) = @_;
2094 0 local ($@, $!);
2095 0 my ($text, $data) = _json_read($path);
2096 0 ▶ return [] unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
2097 return [ map {
2098 0 { name => $_, columns => _json_columns($text, $data->{$_}, 3, $_) }
2099 } _json_list_keys($text, $data) ];
2100 }
2101  
2102 =head2 columns
2103  
2104 Returns an arrayref of column names in file order, or C<undef> when no order
2105 is available.
2106  
2107 =over 4
2108  
2109 =item CSV / TSV / PSV / XLSX
2110  
2111 Column names come from the file header (first row). They are returned in
2112 the order they appear in the file. For header-less files (detected
2113 automatically) the synthesized names (C<Date>, C<Amount>, C<Description>,
2114 etc.) are returned in the same synthesized order.
2115  
2116 =item SQLite / XML
2117  
2118 Falls back to the underlying C<Database::Abstraction> object's C<columns()>
2119 method. This is useful when a C<DataSource> is passed directly to
2120 C<Database::Join> as a component database.
2121  
2122 =item URL-backed tables
2123  
2124 Always returns C<undef>. C<Database::Abstraction> fetches URL data lazily,
2125 so calling C<_db-E<gt>columns()> here would trigger a live network request even
2126 when the data came from the CHI cache. The controller derives column order
2127 from the data records instead (alphabetical fallback).
2128  
2129 =item Redis-backed tables
2130  
2131 Delegates to the underlying C<Database::Abstraction> C<columns()>, which
2132 triggers a lazy open of the Redis connection on first call and returns the
2133 column names derived from the first stored row (alphabetical order, includes
2134 C<entry>). This is required so that C<Database::Join> can validate the
2135 join column at construction time.
2136  
2137 =back
2138  
2139 Always check the return value before dereferencing:
2140  
2141 my $cols = $source->columns;
2142 if ($cols) {
2143 print join(', ', @{$cols}), "\n";
2144 } else {
2145 # URL-backed tables: derive columns from the first data row
2146 }
2147  
2148 =head3 API SPECIFICATION
2149  
2150 =head4 INPUT
2151  
2152 None.
2153  
2154 =head4 OUTPUT
2155  
2156 ARRAYREF of SCALAR # ['id', 'name', 'price', ...] -- original header order
2157 undef # SQLite, XML, URL-backed tables
2158  
2159 =head3 MESSAGES
2160  
2161 None.
2162  
2163 =cut
2164  
2165 1077 ▶sub columns {
Subroutine columns: called CC=10 SCAR=31.6
Pod documented
2166 1077 my $self = shift;
2167 1077 local ($@, $!); # the backend opens lazily, and uses both on the way
2168 1077 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
2169 1077 ▶ return $self->{_columns} if defined $self->{_columns};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_columns"} 1051 26 1077
2170 # A caller that knows the order (an extract, which records its file's) says
2171 # so; a SQLite backend would otherwise list the columns alphabetically.
2172 0 ▶ return [ @{ $self->{_column_order} } ] if $self->{_column_order} && @{ $self->{_column_order} };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_column_order"} and @{$self->{"_column_order"};} 0 26 26
Condition33%1/3
$self->{"_column_order"} and @{$self->{"_column_order"};}
!ll&&!rl&&r
2600
MC/DC0%0/2
$self->{"_column_order"} and @{$self->{"_column_order"};}
$self->{"_column_order"} @{$self->{"_column_order"};}
Truth table33%1/3
A and B
A= $self->{"_column_order"}B= @{$self->{"_column_order"};}
ABresult
0X0
100
111
2173 # URL-backed tables have no canonical column order at construction time;
2174 # calling _db->columns() would trigger a live HTTP request which may fail.
2175 # Return undef so _get_columns derives column names from fetched data.
2176 26 ▶ return undef if defined $self->{_url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_url"} 3 23 26
2177 # Redis-backed tables: delegate to _db->columns() which triggers the lazy
2178 # open() on first call. D::J requires a real column list at construction
2179 # time so returning undef here would make join validation always fail.
2180 # D::A builds its Redis column list from hgetall fields only and omits the
2181 # entry key (the suffix of the Redis key pattern tablename:entryid).
2182 # Prepend the id column so callers and D::J see a complete column list.
2183 23 ▶ if (defined $self->{_redis_url} && $self->{_db}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_redis_url"} and $self->{"_db"}) 0 23 23
Condition33%1/3
defined $self->{"_redis_url"} and $self->{"_db"}
!ll&&!rl&&r
2300
MC/DC0%0/2
defined $self->{"_redis_url"} and $self->{"_db"}
defined $self->{"_redis_url"} $self->{"_db"}
Truth table33%1/3
A and B
A= defined $self->{"_redis_url"}B= $self->{"_db"}
ABresult
0X0
100
111
2184 0 ▶ my $da_cols = $self->{_db}->columns() // [];
Condition0%0/2
$self->{"_db"}->columns // []
l!l
00
MC/DC0%0/1
$self->{"_db"}->columns // []
$self->{"_db"}->columns
Truth table0%0/2
A
A= $self->{"_db"}->columns
Aresult
11
00
2185 0 ▶ my $id = $self->{_id_col} // 'entry';
Condition0%0/2
$self->{"_id_col"} // "entry"
l!l
00
MC/DC0%0/1
$self->{"_id_col"} // "entry"
$self->{"_id_col"}
Truth table0%0/2
A
A= $self->{"_id_col"}
Aresult
11
00
2186 0 my @full = ($id, grep { $_ ne $id } @{$da_cols});
2187 0 $self->{_columns} = \@full;
2188 0 return $self->{_columns};
2189 }
2190 23 ▶ return $self->{_db} ? $self->{_db}->columns() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_db'} ? : 22 1 23
2191 }
2192  
2193 =head2 dbi_source
2194  
2195 Returns a hashref C<{ dbh =E<gt> $dbh, table =E<gt> $name }> for SQLite-backed
2196 instances, and C<undef> for everything else.
2197  
2198 C<Database::Join> 0.009.0 probes C<dbi_source> at join-construction time to
2199 decide whether it can take the zero-copy C<ATTACH> path (avoiding a row-by-row
2200 spill into a temporary SQLite file). Without this method, every C</join> on a
2201 SQLite-backed C<DataSource> uses the spill path regardless of row count.
2202  
2203 Returns C<undef> for:
2204  
2205 =over 4
2206  
2207 =item *
2208  
2209 0-byte files (C<_file_is_empty> set).
2210  
2211 =item *
2212  
2213 Redis-backed and URL-backed sources (no DBI handle).
2214  
2215 =item *
2216  
2217 Any backend that does not itself implement C<dbi_source>.
2218  
2219 =back
2220  
2221 =head3 API SPECIFICATION
2222  
2223 =head4 INPUT
2224  
2225 None.
2226  
2227 =head4 OUTPUT
2228  
2229 { dbh => $dbh_object, table => 'table_name' } # SQLite-backed
2230 undef # all other backends
2231  
2232 =head3 MESSAGES
2233  
2234 None.
2235  
2236 =cut
2237  
2238 206 ▶sub dbi_source {
Subroutine dbi_source: called CC=5 SCAR=16.8
Pod documented
2239 206 my $self = shift;
2240 206 ▶ return undef if $self->{_file_is_empty};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_file_is_empty"} 0 206 206
2241 206 ▶ return undef unless $self->{_db};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_db"} 0 206 206
2242 206 ▶ return undef unless $self->{_db}->can('dbi_source');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_db"}->can("dbi_source") 0 206 206
2243 206 my $result = eval { $self->{_db}->dbi_source() };
2244 206 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 206 206
2245 206 return $result;
2246 }
2247  
2248 =head2 aggregate
2249  
2250 Delegates to the inner C<Database::Abstraction> backend's C<aggregate()> method
2251 (requires C<Database::Abstraction> E<gt>= 0.49).
2252  
2253 Returns C<[]> for in-memory backends (headerless CSV, XLSX, empty files) and for
2254 URL-backed sources where aggregation is not available.
2255  
2256 Accepts the same C<by>, C<measures>, C<having>, C<sort_by>, C<limit>, C<offset>,
2257 and WHERE-criteria parameters as C<Database::Abstraction::aggregate()>.
2258  
2259 =head3 API SPECIFICATION
2260  
2261 =head4 INPUT
2262  
2263 by arrayref Column names to GROUP BY.
2264 measures arrayref Measure descriptors (see Database::Abstraction::aggregate).
2265 having hashref Optional HAVING criteria.
2266 sort_by string Optional ORDER BY column.
2267 limit integer Optional result limit.
2268 offset integer Optional result offset.
2269 (any other key) WHERE column criteria passed through to D::A.
2270  
2271 =head4 OUTPUT
2272  
2273 arrayref of hashrefs -- one row per group.
2274 [] -- in-memory / empty / URL-backed sources.
2275  
2276 =head3 MESSAGES
2277  
2278 None.
2279  
2280 =cut
2281  
2282 446 ▶sub aggregate {
Subroutine aggregate: called CC=4 SCAR=14.3
Pod documented
2283 446 my ($self, @args) = @_;
2284 446 local ($@, $!); # the backend opens lazily, and uses both on the way
2285 446 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
2286 446 ▶ return [] if $self->{_file_is_empty};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_file_is_empty"} 0 446 446
2287 446 ▶ return [] unless $self->{_db};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_db"} 0 446 446
2288 446 ▶ return [] unless $self->{_db}->can('aggregate');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_db"}->can("aggregate") 0 446 446
2289 446 return $self->{_db}->aggregate(@args);
2290 }
2291  
2292 =head2 pages_on_server
2293  
2294 Returns true when C<selectall_arrayref> with C<limit> and C<offset> runs on a
2295 database server without reading the whole table, as it does for MariaDB.
2296  
2297 C<dbi_source> cannot say this: it answers only for SQLite, because
2298 C<Database::Join> takes it to mean that a file can be attached.
2299  
2300 =head3 API SPECIFICATION
2301  
2302 =head4 INPUT
2303  
2304 None.
2305  
2306 =head4 OUTPUT
2307  
2308 SCALAR boolean
2309  
2310 =head3 MESSAGES
2311  
2312 None.
2313  
2314 =cut
2315  
2316 151 ▶sub pages_on_server {
Subroutine pages_on_server: called CC=2 SCAR=6.9
Pod documented
2317 151 my $self = shift;
2318 151 ▶ return defined $self->{_mariadb} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_mariadb'} ? : 5 146 151
2319 }
2320  
2321 =head2 mariadb_tables
2322  
2323 Class method. Lists the tables and views of one MariaDB or MySQL database, with
2324 their columns in the table's own order.
2325  
2326 my $tables = Database::BI::Model::DataSource->mariadb_tables('shop', {
2327 host => 'db.example.com', database => 'shop',
2328 username => 'bi', password => 'secret',
2329 });
2330  
2331 =head3 API SPECIFICATION
2332  
2333 =head4 INPUT
2334  
2335 $server SCALAR string the name of the server, for messages
2336 $settings HASHREF { database, host?, port?, username?, password? }
2337  
2338 =head4 OUTPUT
2339  
2340 ARRAYREF of { name => string, columns => [ string, ... ] }, sorted by name
2341  
2342 =head3 MESSAGES
2343  
2344 Croaks with C<error_mariadb_unknown_server>, C<error_mariadb_settings>,
2345 C<error_mariadb_no_driver> or C<error_mariadb_connect>.
2346  
2347 =cut
2348  
2349 5 ▶sub mariadb_tables {
Subroutine mariadb_tables: called CC=2 SCAR=6.9
Pod documented
2350 5 my ($class, $server, $settings) = @_;
2351 5 local ($@, $!);
2352 5 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
2353  
2354 5 my ($dsn, $username, $password) = _mariadb_dsn($server, $settings);
2355 4 my $tables = eval {
2356 4 Database::Join::Catalog->schema(dsn => $dsn, username => $username, password => $password);
2357 };
2358 4 ▶ croak _fmt('error_mariadb_connect', $server, "$@") unless $tables;
Branch: 1 branch
BranchTrueFalseTotal
unless $tables 3 1 4
2359 1 return $tables;
2360 }
2361  
2362 =head2 id_column
2363  
2364 Returns the name of the column used as the primary key and as
2365 C<Database::Abstraction>'s row-existence anchor.
2366  
2367 For file-backed tables this is the first column in the CSV/TSV/PSV header
2368 whose name is a valid SQL identifier (letters, digits, underscores only,
2369 not starting with a digit) B<and> whose value in the first data row is
2370 non-empty. C<DataSource> detects this automatically to avoid the
2371 C<Database::Abstraction> default of C<'entry'>, which would silently
2372 discard every row in a file that has no C<entry> column.
2373  
2374 Returns C<undef> for URL/HTML-table backends (no primary-key concept applies).
2375  
2376 =head3 API SPECIFICATION
2377  
2378 =head4 INPUT
2379  
2380 None.
2381  
2382 =head4 OUTPUT
2383  
2384 SCALAR string # e.g. 'id', 'date', 'sku' -- the first safe-identifier column
2385 undef # URL-backed (HTML table import) sources
2386  
2387 =head3 MESSAGES
2388  
2389 None.
2390  
2391 =cut
2392  
2393 3 ▶sub id_column {
Subroutine id_column: called CC=1 SCAR=0.0
Pod documented
2394 3 my $self = shift;
2395 3 return $self->{_id_col};
2396 }
2397  
2398 =head2 source_url
2399  
2400 Returns the source URL for URL/HTML-table-backed instances.
2401 Returns C<undef> for all file-backed instances (local files and SFTP-fetched
2402 remote files).
2403  
2404 Use this to distinguish a URL-backed C<DataSource> from a file-backed one
2405 when you need to decide whether to show a "re-fetch" button to the user.
2406  
2407 =head3 API SPECIFICATION
2408  
2409 =head4 INPUT
2410  
2411 None.
2412  
2413 =head4 OUTPUT
2414  
2415 SCALAR string # 'https://example.com/data.html' -- the original URL
2416 undef # file-backed (local or SFTP) sources
2417  
2418 =head3 MESSAGES
2419  
2420 None.
2421  
2422 =cut
2423  
2424 1 ▶sub source_url {
Subroutine source_url: called CC=1 SCAR=0.0
Pod documented
2425 1 my $self = shift;
2426 1 return $self->{_url};
2427 }
2428  
2429 =head2 updated
2430  
2431 Returns the Unix epoch timestamp (mtime) of the backing data file, or
2432 C<undef> for URL-backed tables and for sources whose file cannot be
2433 C<stat()>-ed.
2434  
2435 C<Database::Join> calls C<updated()> on each component database to
2436 detect whether its SQLite spill cache is stale. When a source file changes
2437 on disk, the mtime increases, the join detects the change, and re-runs the
2438 join rather than serving stale cached data.
2439  
2440 =head3 API SPECIFICATION
2441  
2442 =head4 INPUT
2443  
2444 None.
2445  
2446 =head4 OUTPUT
2447  
2448 SCALAR integer # Unix epoch seconds, e.g. 1728601234
2449 undef # URL-backed tables, or file cannot be stat()-ed
2450  
2451 =head3 MESSAGES
2452  
2453 None.
2454  
2455 =cut
2456  
2457 53 ▶sub updated {
Subroutine updated: called CC=5 SCAR=16.1
Pod documented
2458 53 my $self = shift;
2459 # The file itself comes first. D::A only learns an mtime when it opens the file,
2460 # which it does lazily, so before the first read it answers undef, and after it
2461 # goes on answering the time of that read however the file has changed since.
2462 53 ▶ return (stat($self->{_file_path}))[9] if defined $self->{_file_path} && -f $self->{_file_path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_file_path"} and -f $self->{"_file_path"} 50 3 53
Condition100%3/3
defined $self->{"_file_path"} and -f $self->{"_file_path"}
!ll&&!rl&&r
2150
MC/DC100%2/2
defined $self->{"_file_path"} and -f $self->{"_file_path"}
defined $self->{"_file_path"} -f $self->{"_file_path"}
Truth table100%3/3
A and B
A= defined $self->{"_file_path"}B= -f $self->{"_file_path"}
ABresult
0X0
100
111
2463 3 ▶ return $self->{_db}->updated() if $self->{_db} && $self->{_db}->can('updated');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_db"} and $self->{"_db"}->can("updated") 1 2 3
Condition66%2/3
$self->{"_db"} and $self->{"_db"}->can("updated")
!ll&&!rl&&r
201
MC/DC50%1/2
$self->{"_db"} and $self->{"_db"}->can("updated")
$self->{"_db"} $self->{"_db"}->can("updated")
Truth table66%2/3
A and B
A= $self->{"_db"}B= $self->{"_db"}->can("updated")
ABresult
0X0
100
111
2464 2 return undef;
2465 }
2466  
2467 =head2 schema
2468  
2469 Delegates to the inner C<Database::Abstraction> backend's C<schema()> method.
2470  
2471 The join routes compare the declared types of columns that appear in more than
2472 one joined table, and the panel keeps no schema of its own. Without this
2473 delegation a DataSource was schema-less, so every comparison found nothing and
2474 a TEXT/INTEGER mismatch across a join went unreported; the controller's
2475 C<_schema_type_warnings> uses this method to fill the schema-warnings banner.
2476  
2477 Returns a hashref of C<< column => { type => ..., nullable => ..., ... } >>.
2478 File formats that C<Database::Abstraction> reads as text (CSV, TSV, PSV, XML,
2479 XLSX) report C<TEXT>; SQLite reports the declared column types. Returns an
2480 empty hashref when the inner backend cannot provide a schema.
2481  
2482 =head3 API SPECIFICATION
2483  
2484 =head4 INPUT
2485  
2486 None.
2487  
2488 =head4 OUTPUT
2489  
2490 HASHREF column name => schema metadata hashref (possibly empty)
2491  
2492 =head3 MESSAGES
2493  
2494 None.
2495  
2496 =cut
2497  
2498 54 ▶sub schema {
Subroutine schema: called CC=5 SCAR=16.1
Pod documented
2499 54 my $self = shift;
2500 54 local ($@, $!); # the backend opens lazily, and uses both on the way
2501 54 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
2502  
2503 54 ▶ return {} unless $self->{_db} && $self->{_db}->can('schema');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_db"} and $self->{"_db"}->can("schema") 2 52 54
Condition100%3/3
$self->{"_db"} and $self->{"_db"}->can("schema")
!ll&&!rl&&r
1152
MC/DC100%2/2
$self->{"_db"} and $self->{"_db"}->can("schema")
$self->{"_db"} $self->{"_db"}->can("schema")
Truth table100%3/3
A and B
A= $self->{"_db"}B= $self->{"_db"}->can("schema")
ABresult
0X0
100
111
2504  
2505 52 my $schema = eval { $self->{_db}->schema() };
2506 52 ▶ return {} if $@ || ref($schema) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $schema ne "HASH" 3 49 52
Condition100%3/3
$@ or ref $schema ne "HASH"
l!l&&r!l&&!r
1249
MC/DC100%2/2
$@ or ref $schema ne "HASH"
$@ ref $schema ne "HASH"
Truth table100%3/3
A or B
A= $@B= ref $schema ne "HASH"
ABresult
1X1
011
000
2507  
2508 49 return $schema;
2509 }
2510  
2511 # ---------------------------------------------------------------------------
2512 # Public data-access methods
2513 # ---------------------------------------------------------------------------
2514  
2515 =head1 METHODS
2516  
2517 =head2 fetch_all
2518  
2519 Returns every record in the table as an arrayref of hashrefs (one hashref
2520 per row; keys are the column names).
2521  
2522 This is the primary data-retrieval method. It is equivalent to calling
2523 C<selectall_arrayref> with no arguments and normalising the result to an
2524 arrayref. Prefer C<selectall_arrayref> when passing to C<Database::Join>
2525 (it is the method that C<Database::Join> calls internally).
2526  
2527 Filtering (C<eq>, C<contains>, C<gt>, C<lt>, etc.) is applied at the
2528 controller layer by C<Dashboard::_apply_filter_spec> after C<fetch_all>
2529 returns. C<DataSource> itself is filter-unaware.
2530  
2531 Three fast-path shortcuts are checked before touching the DBI backend,
2532 in this order:
2533  
2534 =over 4
2535  
2536 =item 1.
2537  
2538 C<_file_is_empty> is set (0-byte or blank-first-line file) -- returns C<[]>
2539 immediately, no DBI connection.
2540  
2541 =item 2.
2542  
2543 A matching CHI cache entry exists -- returns the cached arrayref without
2544 calling C<Database::Abstraction>.
2545  
2546 =back
2547  
2548 =head3 API SPECIFICATION
2549  
2550 =head4 INPUT
2551  
2552 None. Filtering is applied at the controller layer, not inside C<DataSource>.
2553  
2554 =head4 OUTPUT
2555  
2556 ARRAYREF of HASHREF # one hashref per row; keys are column names
2557 [] # empty arrayref when the file is empty or has no data rows
2558  
2559 Croaks (via C<croak>) when the backend raises an exception. Emits a
2560 warning via C<carp> (non-fatal) when the result set is empty, so the
2561 caller can distinguish "opened successfully, no data" from a silent
2562 failure.
2563  
2564 =head3 MESSAGES
2565  
2566 error_fetch_failed -- backend threw an exception during retrieval
2567 warn_empty_result -- query succeeded but returned zero records
2568 warn_data_normalised -- backend returned a hashref; converted to arrayref
2569  
2570 =cut
2571  
2572 =head2 selectall_arrayref
2573  
2574 C<Database::Abstraction>-compatible alias that allows a C<DataSource> object
2575 to be passed directly to C<Database::Join> as a component database.
2576  
2577 When called with no arguments (the normal path for a full-table scan),
2578 results are served from the CHI cache when available. When called with
2579 arguments (e.g. C<limit =E<gt> N, offset =E<gt> M> for server-side
2580 pagination), the cache is bypassed -- narrowed results must not pollute
2581 the full-table cache entry.
2582  
2583 Three fast-path shortcuts are checked before touching the DBI backend:
2584  
2585 =over 4
2586  
2587 =item 1.
2588  
2589 C<_file_is_empty> is set -- returns C<[]> immediately.
2590  
2591 =item 2.
2592  
2593 The call sorts on a column whose name is not a safe identifier, such as
2594 C<First Name>. L<Database::Abstraction> ignores such a column, so this method
2595 fetches the rows and sorts and pages them itself, comparing as strings as
2596 L<Database::Abstraction> does.
2597  
2598 =item 3.
2599  
2600 A matching CHI cache entry exists for the full-table scan -- returns the
2601 cached arrayref.
2602  
2603 =back
2604  
2605 =head3 API SPECIFICATION
2606  
2607 =head4 INPUT
2608  
2609 Optional flat key/value list passed through to C<Database::Abstraction>:
2610  
2611 limit => N # return at most N rows (used by pagination)
2612 offset => M # skip the first M rows (used by pagination)
2613  
2614 When no arguments are given, the full table is returned (and cached).
2615  
2616 =head4 OUTPUT
2617  
2618 ARRAYREF of HASHREF # one hashref per row; keys are column names
2619 [] # empty arrayref when the file is empty or no rows match
2620  
2621 =head3 MESSAGES
2622  
2623 None. Errors from the DBI backend propagate as uncaught exceptions; wrap
2624 calls in C<eval> if you need to recover.
2625  
2626 =cut
2627  
2628 961 ▶sub selectall_arrayref {
Subroutine selectall_arrayref: called CC=20 SCAR=35.1
Pod documented
2629 961 my ($self, @args) = @_;
2630 961 local ($@, $!); # the backend opens lazily, and uses both on the way
2631 961 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
2632 961 ▶ return [] if $self->{_file_is_empty};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_file_is_empty"} 1 960 961
2633  
2634 # A server gives LIMIT rows in no promised order, so pages could overlap.
2635 960 ▶ if (defined $self->{_order_col} && @args && !ref $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_order_col"} and @args and not ref $args[0]) 0 960 960
Condition33%2/6
defined $self->{"_order_col"} and @args and not ref $args[0]
!ll&&!rl&&r
96000
defined $self->{"_order_col"} and @args
!ll&&!rl&&r
96000
MC/DC0%0/3
defined $self->{"_order_col"} and @args and not ref $args[0]
defined $self->{"_order_col"} @args not ref $args[0]
Truth table25%1/4
A and B and not(C)
A= defined $self->{"_order_col"}B= @argsC= not ref $args[0]
ABCresult
0XX0
10X0
1100
1111
2636 0 my %asked = @args;
2637 push @args, sort_by => [ $self->{_order_col}, 'asc' ]
2638 0 ▶ if exists $asked{limit} && !$asked{sort_by};
Branch: 1 branch
BranchTrueFalseTotal
if exists $asked{"limit"} and not $asked{"sort_by"} 0 0 0
Condition0%0/3
exists $asked{"limit"} and not $asked{"sort_by"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $asked{"limit"} and not $asked{"sort_by"}
exists $asked{"limit"} not $asked{"sort_by"}
Truth table0%0/3
A and not(B)
A= exists $asked{"limit"}B= not $asked{"sort_by"}
ABresult
0X0
100
111
2639 }
2640  
2641 # D::A ignores a sort column whose name is not a safe identifier.
2642 960 ▶ if (my ($rest, @page) = _unsafe_sort(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _unsafe_sort(@args)) 2 958 960
2643 2 return _sorted_page($self->selectall_arrayref(%{$rest}), @page);
2644 }
2645  
2646 # Cache only plain unfiltered calls -- Database::Join passes no args for the
2647 # full table scan; a non-empty @args means a narrowed query whose result must
2648 # not be mistaken for the full-table cache entry.
2649 958 my $cache = $self->{_cache};
2650 958 ▶ if ($cache && !@args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and not @args) 904 54 958
Condition100%3/3
$cache and not @args
!ll&&!rl&&r
846904
MC/DC100%2/2
$cache and not @args
$cache not @args
Truth table100%3/3
A and not(B)
A= $cacheB= not @args
ABresult
0X0
100
111
2651 904 my $key = $self->_cache_key;
2652 904 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 904 0 904
2653 904 my $hit = $cache->get($key);
2654 904 ▶ return $hit if defined $hit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hit 856 48 904
2655 }
2656 }
2657  
2658 102 my $data = $self->{_db}->selectall_arrayref(@args);
2659  
2660 # As in fetch_all: what a URL answers with need not be a table.
2661 croak $self->_msg('error_fetch_failed', $self->{_table}, $self->_msg('error_rows_not_records'))
2662 0 ▶ if defined $self->{_url} && ref $data eq 'ARRAY' && grep { ref $_ ne 'HASH' } @{$data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_url"} and ref $data eq "ARRAY" and grep {ref $_ ne "HASH";} @{$data;} 0 102 102
Condition33%2/6
defined $self->{"_url"} and ref $data eq "ARRAY" and grep {ref $_ ne "HASH";} @{$data;}
!ll&&!rl&&r
10200
defined $self->{"_url"} and ref $data eq "ARRAY"
!ll&&!rl&&r
10200
MC/DC0%0/3
defined $self->{"_url"} and ref $data eq "ARRAY" and grep {ref $_ ne "HASH";} @{$data;}
defined $self->{"_url"} ref $data eq "ARRAY" grep {ref $_ ne "HASH";} @{$data;}
Truth table25%1/4
A and B and C
A= defined $self->{"_url"}B= ref $data eq "ARRAY"C= grep {ref $_ ne "HASH";} @{$data;}
ABCresult
0XX0
10X0
1100
1111
2663  
2664 102 ▶ if ($cache && !@args && defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and not @args and defined $data) 48 54 102
Condition83%5/6
$cache and not @args and defined $data
!ll&&!rl&&r
54048
$cache and not @args
!ll&&!rl&&r
84648
MC/DC66%2/3
$cache and not @args and defined $data
$cache not @args defined $data
Truth table75%3/4
A and not(B) and C
A= $cacheB= not @argsC= defined $data
ABCresult
0XX0
10X0
1100
1111
2665 48 my $key = $self->_cache_key;
2666 48 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 48 0 48
2667 defined $self->{_url}
2668 ? $cache->set($key, $data, $self->{_cache_ttl_url})
2669 48 ▶ : $cache->set($key, $data);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_url'} ? : 0 48 48
2670 }
2671 }
2672  
2673 102 return $data;
2674 }
2675  
2676 32 ▶sub fetch_all {
Subroutine fetch_all: called CC=14 SCAR=26.5
Pod documented
2677 32 my $self = shift;
2678 32 local ($@, $!); # the backend opens lazily, and uses both on the way
2679 32 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
2680 32 my $table = $self->{_table};
2681  
2682 # 0-byte file: no backend was created; nothing to fetch.
2683 32 ▶ return [] if $self->{_file_is_empty};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_file_is_empty"} 0 32 32
2684  
2685 # Cache check: URL tables benefit from avoiding repeated HTTP round-trips;
2686 # file tables benefit from skipping disk I/O and CSV/TSV/PSV parsing.
2687 32 my $cache = $self->{_cache};
2688 32 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 18 14 32
2689 18 my $key = $self->_cache_key;
2690 18 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 18 0 18
2691 18 my $hit = $cache->get($key);
2692 18 ▶ return $hit if defined $hit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hit 12 6 18
2693 }
2694 }
2695  
2696 20 my $data = eval { $self->{_db}->selectall_hashref() };
2697 20 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 19 20
2698 1 croak $self->_msg('error_fetch_failed', $table, $@);
2699 }
2700  
2701 19 ▶ return [] unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 18 19
2702  
2703 # Strategy: Database::Abstraction can return either an arrayref (when the
2704 # underlying driver iterates rows) or a hashref keyed by primary key (when
2705 # it mirrors DBI's selectall_hashref semantics). We normalise to arrayref
2706 # here so every caller above this layer sees a uniform structure.
2707 18 ▶ if (ref $data eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 1 17 18
2708 1 carp $self->_msg('warn_data_normalised', $table);
2709 1 $data = [ values %{$data} ];
2710 }
2711  
2712 18 ▶ if (!@{$data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$data;}) 1 17 18
2713 1 carp $self->_msg('warn_empty_result', $table);
2714 }
2715  
2716 # A JSON URL may answer with an array of anything; every layer above reads
2717 # each row as a hash, and would die on the first scalar.
2718 croak $self->_msg('error_fetch_failed', $table, $self->_msg('error_rows_not_records'))
2719 18 ▶ if defined $self->{_url} && grep { ref $_ ne 'HASH' } @{$data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_url"} and grep {ref $_ ne "HASH";} @{$data;} 0 18 18
Condition66%2/3
defined $self->{"_url"} and grep {ref $_ ne "HASH";} @{$data;}
!ll&&!rl&&r
1710
MC/DC0%0/2
defined $self->{"_url"} and grep {ref $_ ne "HASH";} @{$data;}
defined $self->{"_url"} grep {ref $_ ne "HASH";} @{$data;}
Truth table66%2/3
A and B
A= defined $self->{"_url"}B= grep {ref $_ ne "HASH";} @{$data;}
ABresult
0X0
100
111
2720  
2721 # Cache store: URL tables use a TTL so stale pages expire automatically;
2722 # file tables encode mtime in the key so a changed file produces a natural
2723 # miss without any explicit invalidation.
2724 18 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 6 12 18
2725 6 my $key = $self->_cache_key;
2726 6 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 6 0 6
2727 defined $self->{_url}
2728 ? $cache->set($key, $data, $self->{_cache_ttl_url})
2729 6 ▶ : $cache->set($key, $data);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_url'} ? : 1 5 6
2730 }
2731 }
2732  
2733 18 return $data;
2734 }
2735  
2736 =head2 count
2737  
2738 Returns the total number of rows in the table.
2739  
2740 For empty-file sentinels it returns C<0> immediately. For all DBI-backed
2741 sources (SQLite, large CSV via DBD::CSV) it delegates to
2742 C<Database::Abstraction::count()>, which issues a
2743 C<SELECT COUNT(*) FROM E<lt>tableE<gt>> query against the file.
2744  
2745 The count of a file's table is remembered for the life of the process, keyed
2746 by the file, the table, and the file's modification time and size, so asking
2747 again costs nothing until the file changes. A URL or MariaDB source is counted
2748 on every call. The first count of a very large SQLite file still reads the
2749 whole table.
2750  
2751 =head3 API SPECIFICATION
2752  
2753 =head4 INPUT
2754  
2755 None.
2756  
2757 =head4 OUTPUT
2758  
2759 SCALAR non-negative integer # total row count, 0 for empty files
2760  
2761 =head3 MESSAGES
2762  
2763 None.
2764  
2765 =cut
2766  
2767 23 ▶sub count {
Subroutine count: called CC=6 SCAR=18.3
Pod documented
2768 23 my ($self) = @_;
2769 23 local ($@, $!); # the backend opens lazily, and uses both on the way
2770 23 local $/ = "\n"; # a caller's slurp mode (undef $/) read the file as one line: 0 rows
2771 23 ▶ return 0 if $self->{_file_is_empty};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_file_is_empty"} 0 23 23
2772  
2773 23 my $key = $self->_count_key;
2774 23 ▶ return $COUNT_MEMO{$key} if defined $key && exists $COUNT_MEMO{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and exists $COUNT_MEMO{$key} 13 10 23
Condition66%2/3
defined $key and exists $COUNT_MEMO{$key}
!ll&&!rl&&r
01013
MC/DC50%1/2
defined $key and exists $COUNT_MEMO{$key}
defined $key exists $COUNT_MEMO{$key}
Truth table66%2/3
A and B
A= defined $keyB= exists $COUNT_MEMO{$key}
ABresult
0X0
100
111
2775  
2776 10 my $n = $self->{_db}->count();
2777 10 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 10 0 10
2778 10 ▶ %COUNT_MEMO = () if keys(%COUNT_MEMO) >= $COUNT_MEMO_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if keys %COUNT_MEMO >= $COUNT_MEMO_MAX 0 10 10
2779 10 $COUNT_MEMO{$key} = $n;
2780 }
2781 10 return $n;
2782 }
2783  
2784 # _count_key( $self ) -> $key | undef
2785 #
2786 # What count() remembers its answer under: the file, the table in it, and the
2787 # file's mtime and size, so a changed file is counted again. undef (count every
2788 # time) for a URL or a database server, which have no file to stat.
2789 23 ▶sub _count_key {
Subroutine _count_key: called CC=7 SCAR=22.1
2790 23 my $self = shift;
2791 23 ▶ return undef if defined $self->{_url} || defined $self->{_mariadb};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_url"} or defined $self->{"_mariadb"} 0 23 23
Condition33%1/3
defined $self->{"_url"} or defined $self->{"_mariadb"}
l!l&&r!l&&!r
0023
MC/DC0%0/2
defined $self->{"_url"} or defined $self->{"_mariadb"}
defined $self->{"_url"} defined $self->{"_mariadb"}
Truth table33%1/3
A or B
A= defined $self->{"_url"}B= defined $self->{"_mariadb"}
ABresult
1X1
011
000
2792 23 my $file = $self->{_file_path};
2793 23 ▶ return undef unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 23 23
2794 23 my @st = stat $file;
2795 23 ▶ return undef unless @st;
Branch: 1 branch
BranchTrueFalseTotal
unless @st 0 23 23
2796 23 ▶ return join "\0", $file, $self->{_table} // '', $self->{_internal_table} // '', $st[9], $st[7];
Condition50%2/4
$self->{"_table"} // ""
l!l
230
$self->{"_internal_table"} // ""
l!l
023
MC/DC0%0/1
$self->{"_table"} // ""
$self->{"_table"}
MC/DC0%0/1
$self->{"_internal_table"} // ""
$self->{"_internal_table"}
Truth table50%1/2
A
A= $self->{"_table"}
Aresult
11
00
Truth table50%1/2
A
A= $self->{"_internal_table"}
Aresult
11
00
2797 }
2798  
2799 8 1;
2800  
2801 __END__
« blib/lib/Database/BI/Model/Boundary.pm ↑ Summary blib/lib/Database/BI/SafeUA.pm »