NAME

App::DBBrowser::DB - Database plugin documentation.

VERSION

Version 2.437_03

DESCRIPTION

A database plugin provides the database specific methods. App::DBBrowser considers a module whose name matches /^App::DBBrowser::DB::[^:']+\z/ and which is located in one of the @INC directories, as a database plugin.

The user can add an installed database plugin to the available plugins in the options menu (db-browser -h) by selecting Select plugins.

A suitable database plugin provides the methods mentioned in this documentation.

METHODS

Required methods

new( $info, $opt )

The constructor method.

When db-browser calls the plugin constructor it passes two arguments:

sub new {
    my ( $class, $info, $opt ) = @_;
    my $self = {
        info => $info,
        opt  => $opt
    };
    return bless $self, $class;
}

# $info->{app_dir}        -> path to the configuration directoriy of the app
# $info->{search}         -> true if C<db-browser> was called with the argument C<-s|--search>
# $opt->{G}{metadata}     -> Options/Sql/System data

Returns the created object.

get_db_driver()

Returns the name of the DBI database driver used by the plugin.

get_databases();

Returns two array references: the first reference refers to the array of user databases, the second to the array of the system databases. The second array reference is optional.

If the option System data is true, user databases and system databases are used, otherwise only the user databases are used.

get_db_handle( $database )

Returns the database handle.

db-browser expects a DBI database handle with the attribute RaiseError activated.

EXAMPLE

package App::DBBrowser::DB::MyPlugin;
use strict;
use DBI;

sub new {
    my ( $class ) = @_;
    return bless {}, $class;
}

sub get_db_driver {
    my ( $self ) = @_;
    return 'Pg';
}

sub get_db_handle {
    my ( $self, $db ) = @_;
    my $dbh = DBI->connect( "DBI:Pg:dbname=$db", 'user', 'password', {
        RaiseError => 1,
        PrintError => 0,
    });
    return $dbh;
}

sub get_databases {
    my ( $self ) = @_;
    return [ 'My_DB_1', 'My_DB_2' ];
}

1;

Optional methods

get_schemas( $dbh, $database, $is_system_db )

$dbh is the database handle returned by the method db_hanlde and $database is the database name. If is_system_db is true, then the database is a system database.

Returns the user schemas as an array reference and the system schemas as an array reference (if any).

If the option System data is true, user schemas and system schemas are used, otherwise only the user schemas are used.

tables_info( $dbh, $schema, $is_system_schema )

$dbh is the database handle and $schema is the schema name. If is_system_schema is true, then the schema is a system schema.

Returns three values:

The first value is a hash reference where the keys are the table keys (table names), and the values are array references containing the following elements: table category, table schema, table name, and table type.

The second value is an array reference containing the list of user table keys.

The third value is an array reference containing the list of system table keys.

EXAMPLE

sub tables_info {
    my ( $sf, $dbh, $schema, $is_system_schema ) = @_;
    my $sth = $dbh->table_info( undef, $schema, '%', '' );
    my $table_cat   = 'TABLE_CAT';
    my $table_schem = 'TABLE_SCHEM';
    my $table_name  = 'TABLE_NAME';
    my $table_type  = 'TABLE_TYPE';
    my @fields = ( $table_cat, $table_schem, $table_name, $table_type );
    my $info_tables = $sth->fetchall_arrayref( { map { $_ => 1 } @fields } );
    my ( @user_table_keys, @sys_table_keys );
    my $tables_info = {};

    for my $info_table ( @$info_tables ) {
        my $table_key = $info_table->{$table_name};
        if ( $is_system_schema ) {
            push @sys_table_keys, $table_key;
        }
        else {
            if ( $info_table->{$table_type} =~ /^SYSTEM/ ) {
                push @sys_table_keys, $table_key;
            }
            else {
                push @user_table_keys, $table_key;
            }
        }
        $tables_info->{$table_key} = [ @{$info_table}{@fields} ];
    }
    return $tables_info, [ sort @user_table_keys ], [ sort @sys_table_keys ];
}

AUTHOR

Matthäus Kiem <cuer2s@gmail.com>

LICENSE AND COPYRIGHT

Copyright 2012-2025 Matthäus Kiem.

THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For details, see the full text of the licenses in the file LICENSE.