DBIx::Util::Schema - Utility routines related to database schema
This document describes version 0.002 of DBIx::Util::Schema (from Perl distribution DBIx-Util-Schema), released on 2020-05-06.
use DBIx::Util::Schema qw( table_exists column_exists list_tables list_columns list_indexes ); say "Database has table named 'foo'" if table_exists($dbh, "foo");
DBI already provides methods to query schema information, e.g. table_info(), column_info(), statistics_info(), but simple things like checking whether a table or a column exists is not straightforward or easy enough. This module provides convenience routines for those tasks.
table_info()
column_info()
statistics_info()
Currently only tested on SQLite, MySQL, and Postgres.
Usage:
column_exists($dbh, $table, $column) -> any
Alias for has_column().
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
$column* => str
Table column name.
$dbh* => obj
DBI database handle.
$table* => str
Table name.
Return value: (any)
has_all_columns($dbh, $table, $columns, ...) -> any
Check whether a table has all specified columns.
$columns* => array[str]
Table column names.
has_all_tables($dbh, $tables, ...) -> any
Check whether database has all specified tables.
$tables* => array[str]
Table names.
has_any_column($dbh, $table, $columns, ...) -> any
Check whether a table has at least one of specified columns.
has_any_table($dbh, $tables, ...) -> any
Check whether database has at least one of specified tables.
has_column($dbh, $table, $column) -> any
Check whether a table has a specified column.
has_table($dbh, $table) -> any
Check whether database has a certain table.
list_columns($dbh, $table) -> any
List columns of a table.
list_indexes($dbh, $table) -> any
List indexes for a table in a database.
SQLite notes: information is retrieved from DBI's table_info(). Autoindex for primary key is not listed using table_info(), but this function adds it by looking at sqlite_master table.
sqlite_master
MySQL notes: information is retrieved from statistics_info(). Note that a multi-column index is reported as separate rows by statistics_info(), one for each indexed column. But this function merges them and returns the list of columns in columns.
columns
list_tables($dbh) -> any
List table names in a database.
table_exists($dbh, $table) -> any
Alias for has_table().
Please visit the project's homepage at https://metacpan.org/release/DBIx-Util-Schema.
Source repository is at https://github.com/perlancar/perl-DBIx-Util-Schema.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Util-Schema
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Test::DBUnit currently has more methods.
DBI
perlancar <perlancar@cpan.org>
This software is copyright (c) 2020 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install DBIx::Util::Schema, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Util::Schema
CPAN shell
perl -MCPAN -e shell install DBIx::Util::Schema
For more information on module installation, please visit the detailed CPAN module installation guide.