Database-BI

 view release on metacpan or  search on metacpan

lib/Database/BI/Model/DataSource.pm  view on Meta::CPAN


=head2 source_url

Returns the source URL for URL/HTML-table-backed instances.
Returns C<undef> for all file-backed instances (local files and SFTP-fetched
remote files).

Use this to distinguish a URL-backed C<DataSource> from a file-backed one
when you need to decide whether to show a "re-fetch" button to the user.

=head3 API SPECIFICATION

=head4 INPUT

None.

=head4 OUTPUT

    SCALAR string    # 'https://example.com/data.html' -- the original URL
    undef            # file-backed (local or SFTP) sources

=head3 MESSAGES

None.

=cut

sub source_url {
	my $self = shift;
	return $self->{_url};
}

=head2 updated

Returns the Unix epoch timestamp (mtime) of the backing data file, or
C<undef> for URL-backed tables and for sources whose file cannot be
C<stat()>-ed.

C<Database::Join> calls C<updated()> on each component database to
detect whether its SQLite spill cache is stale.  When a source file changes
on disk, the mtime increases, the join detects the change, and re-runs the
join rather than serving stale cached data.

=head3 API SPECIFICATION

=head4 INPUT

None.

=head4 OUTPUT

    SCALAR integer   # Unix epoch seconds, e.g. 1728601234
    undef            # URL-backed tables, or file cannot be stat()-ed

=head3 MESSAGES

None.

=cut

sub updated {
	my $self = shift;
	# The file itself comes first.  D::A only learns an mtime when it opens the file,
	# which it does lazily, so before the first read it answers undef, and after it
	# goes on answering the time of that read however the file has changed since.
	return (stat($self->{_file_path}))[9] if defined $self->{_file_path} && -f $self->{_file_path};
	return $self->{_db}->updated() if $self->{_db} && $self->{_db}->can('updated');
	return undef;
}

=head2 schema

Delegates to the inner C<Database::Abstraction> backend's C<schema()> method.

The join routes compare the declared types of columns that appear in more than
one joined table, and the panel keeps no schema of its own.  Without this
delegation a DataSource was schema-less, so every comparison found nothing and
a TEXT/INTEGER mismatch across a join went unreported; the controller's
C<_schema_type_warnings> uses this method to fill the schema-warnings banner.

Returns a hashref of C<< column => { type => ..., nullable => ..., ... } >>.
File formats that C<Database::Abstraction> reads as text (CSV, TSV, PSV, XML,
XLSX) report C<TEXT>; SQLite reports the declared column types.  Returns an
empty hashref when the inner backend cannot provide a schema.

=head3 API SPECIFICATION

=head4 INPUT

None.

=head4 OUTPUT

    HASHREF    column name => schema metadata hashref (possibly empty)

=head3 MESSAGES

None.

=cut

sub schema {
	my $self = shift;
	local ($@, $!);	# the backend opens lazily, and uses both on the way
	local $/ = "\n";	# a caller's slurp mode (undef $/) read the file as one line: 0 rows

	return {} unless $self->{_db} && $self->{_db}->can('schema');

	my $schema = eval { $self->{_db}->schema() };
	return {} if $@ || ref($schema) ne 'HASH';

	return $schema;
}

# ---------------------------------------------------------------------------
# Public data-access methods
# ---------------------------------------------------------------------------

=head1 METHODS

=head2 fetch_all



( run in 2.738 seconds using v1.01-cache-2.11-cpan-0e0213f3164 )