App-DBBrowser
view release on metacpan or search on metacpan
lib/App/DBBrowser/DB.pm view on Meta::CPAN
=head2 Required methods
=head3 new( $info, $opt )
The constructor method.
When C<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.
=head3 get_db_driver()
Returns the name of the C<DBI> database driver used by the plugin.
=head3 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 I<System data> is true, user databases and system databases are used, otherwise only the user databases
are used.
=head3 get_db_handle( $database )
Returns the database handle.
C<db-browser> expects a C<DBI> database handle with the attribute I<RaiseError> activated.
=head3 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;
=head2 Optional methods
=head4 get_schemas( $dbh, $database, $is_system_db )
C<$dbh> is the database handle returned by the method C<db_hanlde> and C<$database> is the database name. If
C<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 I<System data> is true, user schemas and system schemas are used, otherwise only the user schemas are used.
=head4 tables_info( $dbh, $schema, $is_system_schema )
C<$dbh> is the database handle and C<$schema> is the schema name. If C<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.
=head4 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;
( run in 0.447 second using v1.01-cache-2.11-cpan-39bf76dae61 )