141 lines
3.3 KiB
Perl
141 lines
3.3 KiB
Perl
package sqlConn;
|
|
|
|
# use DBIx::Log4perl;
|
|
# use DBI::Profile;
|
|
use Moo;
|
|
use MooX::HandlesVia;
|
|
use DBD::Pg;
|
|
use DDP;
|
|
|
|
use v5.26;
|
|
use feature 'signatures';
|
|
no warnings qw(experimental::signatures);
|
|
|
|
has 'db' => ( is => 'rw', required => 1 );
|
|
has 'dbh' => ( is => 'lazy' );
|
|
has 'sql' => ( is => 'rw', trigger => \&_build_results );
|
|
has 'bind' => (is => 'rw' );
|
|
has 'results' => ( is => 'lazy', );
|
|
has 'column_names' => ( is => 'ro', writer => '_set_column_names');
|
|
|
|
# my $logconf;
|
|
# my $log;
|
|
# if (-e 'N:\conversion\Automation\Packages') {
|
|
# $logconf = 'N:\conversion\Automation\Logs\log.conf';
|
|
# $log = 'generic';
|
|
# }
|
|
# else {
|
|
# $logconf = '/mnt/cvgserver2/conversion/Automation/Logs/log_linux.conf';
|
|
# $log = 'linux';
|
|
# }
|
|
#
|
|
# say "using $log logging: $logconf";
|
|
#
|
|
# Log::Log4perl->init( $logconf );
|
|
# my $logger = Log::Log4perl->get_logger($log);
|
|
|
|
my %db_conn = (
|
|
pc => {
|
|
database => 'nehantic_data',
|
|
host => 'nehantic-dev.cnxhiwukntah.us-east-1.rds.amazonaws.com',
|
|
# host => 'localhost',
|
|
port => '5432',
|
|
user => 'pc',
|
|
pass => 'ydY4&Hz4p4j4^h',
|
|
dsn => "DBI:Pg:dbname=",
|
|
},
|
|
superbase => {
|
|
database => 'superbase',
|
|
# host => 'nehantic-dev.cnxhiwukntah.us-east-1.rds.amazonaws.com',
|
|
host => 'localhost',
|
|
port => '5432',
|
|
user => 'superfly',
|
|
pass => 'C33ucme!',
|
|
dsn => "DBI:Pg:dbname=",
|
|
},
|
|
);
|
|
|
|
sub _build_dbh {
|
|
my $self = shift;
|
|
my $db = $self->db;
|
|
my $database = $db_conn{$db}->{'database'} // '';
|
|
my $host = $db_conn{$db}->{'host'} // '';
|
|
my $port = $db_conn{$db}->{'port'} // '';
|
|
my $user = $db_conn{$db}->{'user'};
|
|
my $password = $db_conn{$db}->{'pass'};
|
|
my $dsn = $db_conn{$db}->{'dsn'} . "$database;host=$host;port=$port";
|
|
|
|
my $dbh;
|
|
|
|
#if ($db eq 'qb') {
|
|
# say $db_conn{$db}->{'dsn'};
|
|
# $dbh = DBI->connect('dbi:ODBC:QB64') || $logger->logdie("couldn't connect to source database: $!");
|
|
#}
|
|
|
|
#elsif ($db eq 'infor') {
|
|
# say $db_conn{$db}->{'dsn'};
|
|
# $dbh = DBIx::Log4perl->connect($db_conn{$db}->{'dsn'}, $db_conn{$db}->{'user'}, $db_conn{$db}->{'pass'}) || $logger->logdie("couldn't connect to source database: $!");
|
|
# }
|
|
|
|
# elsif ($db_conn{$db}->{'conn_str'}) {
|
|
# $dbh = DBIx::Log4perl->connect($db_conn{$db}->{'conn_str'}, {
|
|
# 'PrintError' => 0,
|
|
# 'RaiseError' => 1,
|
|
# 'AutoCommit' => 1,
|
|
# });
|
|
# }
|
|
#
|
|
# else {
|
|
# $dbh = DBIx::Log4perl->connect($dsn, $user, $password, {
|
|
$dbh = DBI->connect($dsn, $user, $password, {
|
|
'PrintError' => 1,
|
|
'RaiseError' => 1,
|
|
'AutoCommit' => 1,
|
|
});
|
|
# }
|
|
|
|
$dbh->{Profile} = 0;
|
|
|
|
return $dbh;
|
|
}
|
|
|
|
sub _build_results {
|
|
my $self = shift;
|
|
my $dbh = $self->dbh;
|
|
my $sql = $self->sql;
|
|
my $sth = $dbh->prepare($sql);
|
|
my $bind;
|
|
|
|
if ($self->bind) {
|
|
$sth->execute($bind);
|
|
}
|
|
else {
|
|
$sth->execute();
|
|
}
|
|
|
|
$self->_set_column_names($sth->{NAME});
|
|
return $sth->fetchall_arrayref;
|
|
}
|
|
|
|
sub triggers ($self) {
|
|
my $sql = qq{
|
|
SELECT tgrelid::regclass triggername, t.tgname tablename
|
|
FROM pg_trigger t, pg_proc p, pg_class c
|
|
WHERE c.oid = t.tgrelid AND t.tgfoid=p.oid
|
|
AND c.relkind = 'r'
|
|
};
|
|
$self->sql($sql);
|
|
}
|
|
|
|
sub tables ($self) {
|
|
my $sql = qq{
|
|
SELECT table_schema || '.' || table_name "tablename"
|
|
FROM information_schema.tables
|
|
WHERE table_type = 'BASE TABLE'
|
|
AND table_schema NOT IN ('pg_catalog', 'information_schema')
|
|
};
|
|
$self->sql($sql);
|
|
}
|
|
|
|
#__PACKAGE__->meta()->make_immutable();
|