1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
package DBIEasy::postgresql;
use strict;
use warnings;
use Data::Dumper;
use base 'DBIEasy';
my $queryTimeout = 300;
sub getDSN {
my $self = shift;
return (
'DBI:Pg:' .
'database=' . $self->dbname .
';host=' . $self->dbhost,
$self->dbuser, $self->dbpass
);
}
sub checkIfTableExists {
my $self = shift;
my $dbh = $self->dbh;
my $table = lc($self->TABLE);
my @tables = $dbh->tables(undef, undef, $table);
unless (grep {/^("?[^\.\s]+"?.)?"?$table"?$/} @tables) {
if ($self->SETUPTABLE) {
$self->_carp("Table '$table' doesn't exist, try to create it...\n");
my $createStmt = "CREATE TABLE `$table` (" . $self->CREATETABLE . ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
$dbh->do($createStmt) or $self->_croak($dbh->errstr);
my @tables = $dbh->tables(undef, undef, $table);
unless (grep {/^(`?$self->{dbname}`?.)?`?$table`?$/} @tables) {
$self->_croak("Sorry, after creating the Table '$table', it isn't there?");
return 0;
}
} else {
$self->_croak("Table '$table' doesn't exist!");
return 0;
}
}
return 1;
}
sub getPK {
my $self = shift;
my $dbh = $self->dbh;
my $table = lc($self->TABLE);
my @PKs = ();
my $sth = $dbh->prepare(qq{
SELECT
f.attname AS pk
FROM
pg_attribute f
JOIN pg_class c ON c.oid = f.attrelid
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY ( p.conkey )
WHERE
c.relkind = 'r'::char
AND p.contype = 'p'
AND c.relname = ?
AND f.attnum > 0
});
$sth->execute($table);
$self->_croak($dbh->errstr) if $dbh->errstr;
if ($sth) {
my $primKeys = $sth->fetchall_arrayref({});
foreach (@{$primKeys}) {
push @PKs, $_->{pk};
}
}
if ($#PKs == -1) {
$self->_croak("No Primary Keys in Table '$table'!");
} else {
return \@PKs;
}
}
sub getColoumns {
my $self = shift;
my $dbh = $self->dbh;
my $table = lc($self->TABLE);
my $sth = $dbh->prepare(qq{
SELECT
f.attname AS name
FROM
pg_attribute f
JOIN pg_class c ON c.oid = f.attrelid
WHERE
c.relkind = 'r'::char
AND c.relname = ?
AND f.attnum > 0
});
$sth->execute($table);
$self->_croak($dbh->errstr) if $dbh->errstr;
my $cols = $sth->fetchall_arrayref({});
if ($#{$cols} > -1) {
return map {$_->{name}} @{$cols};
} else {
$self->_carp("Sorry, no Columns found!");
return ();
}
}
sub checkForOldHandles {
my $self = shift;
my $dbh = shift;
my $superUser = ${$dbh->selectall_arrayref(qq{
SELECT usesuper
FROM pg_user
WHERE usename=?
}, { Slice => {} }, $self->dbuser)}[0];
# Killing old queries only makes sense if database user has superuser rights
return unless defined $superUser && $superUser->{usesuper};
my $sth_sh_proc = $dbh->prepare(q{
SELECT
EXTRACT(EPOCH FROM query_start)::integer as query_start,
procpid
FROM
pg_stat_activity
WHERE
current_query='idle in transaction'
});
my $sth_kill = $dbh->prepare("SELECT pg_cancel_backend(?)");
unless ($sth_sh_proc->execute()) {
_carp("Unable to execute show procs [" . $dbh->errstr() . "]");
return 0;
}
while (my $row = $sth_sh_proc->fetchrow_hashref()) {
my $queryAge = time - $row->{query_start};
if ($queryAge > $queryTimeout) {
my $id = $row->{procpid};
$sth_kill->execute($id);
}
}
return 1;
}
sub mkCaseSensitive {
my $self = shift;
my @whereStr = @_;
# postgresql is case sensitive by default
return @whereStr;
}
sub _log {
DBIEasy::_log(@_);
}
sub _carp {
DBIEasy::_carp(@_);
}
sub _croak {
DBIEasy::_croak(@_);
}
1;
# vim:ts=2:sts=2:sw=2
|