| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::DB::DB2; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use DBI qw(); | ||
| 9 | |||
| 10 | use App::DBBrowser::Credentials; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ( $class, $info, $opt ) = @_; | ||
| 14 | my $sf = { | ||
| 15 | i => $info, | ||
| 16 | o => $opt | ||
| 17 | }; | ||
| 18 | bless $sf, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | sub get_db_driver { | ||
| 23 | my ( $sf ) = @_; | ||
| 24 | return 'DB2'; | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | sub get_db_handle { | ||
| 29 | my ( $sf, $db ) = @_; | ||
| 30 | my $cred = App::DBBrowser::Credentials->new( $sf->{i}, $sf->{o} ); | ||
| 31 | # DBD::DB2 - Data Source Names: | ||
| 32 | # Cataloged database connections can be done by passing the database alias, username, and password as parameters. | ||
| 33 | # This method does not allow entering the host name, port number, etc but will require you to catalog the database | ||
| 34 | # (local or remote) through DB2. | ||
| 35 | my $dsn = "dbi:DB2:$db"; | ||
| 36 | my $show_sofar = 'DB '. $db; | ||
| 37 | my $user = $cred->get_login( 'user', $show_sofar ); | ||
| 38 | $show_sofar .= "\n" . 'User: ' . $user if defined $user; | ||
| 39 | my $passwd = $cred->get_login( 'pass', $show_sofar ); | ||
| 40 | my $dbh = DBI->connect( $dsn, $user, $passwd, { | ||
| 41 | PrintError => 0, | ||
| 42 | RaiseError => 1, | ||
| 43 | AutoCommit => 1, | ||
| 44 | ShowErrorStatement => 1, | ||
| 45 | %{$sf->{o}{connect_attr}//{}}, | ||
| 46 | } ); | ||
| 47 | return $dbh; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub get_databases { | ||
| 52 | my ( $sf ) = @_; | ||
| 53 | return \@ARGV if @ARGV; | ||
| 54 | my $driver = $sf->get_db_driver(); | ||
| 55 | # data_sources returns a list of all cataloged databases. | ||
| 56 | my @cataloged_databases = DBI->data_sources( $driver ); | ||
| 57 | for ( @cataloged_databases ) { | ||
| 58 | s/^dbi:$driver://; | ||
| 59 | s/\s+\z//; | ||
| 60 | } | ||
| 61 | return [ @cataloged_databases ]; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |