File: pgSession.pm

package info (click to toggle)
check-pgactivity 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 840 kB
  • sloc: perl: 10,820; sh: 10; makefile: 6
file content (58 lines) | stat: -rw-r--r-- 1,308 bytes parent folder | download
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
# This program is open source, licensed under the PostgreSQL License.
# For license terms, see the LICENSE file.
#
# Copyright (C) 2012-2025: Open PostgreSQL Monitoring Development Group

package pgSession;

use strict;
use warnings;
use version;

use Carp;

sub new {
    my $class = shift;
    my $node  = shift;
    my $db    = shift;
    my $self;

    $db = 'template1' unless defined $db;

    $self->{'timer'} = IPC::Run::timer(5);
    $self->{'delim'} = 'CHECK_PGA_PROMPT_DELIM=>';
    $self->{'in'}    = '';
    $self->{'out'}   = '';
    $self->{'proc'}  = $node->interactive_psql(
        $db, \$self->{'in'}, \$self->{'out'}, $self->{'timer'},
        extra_params=>[
            '--pset=pager',
            '--variable=PROMPT1='. $self->{'delim'}
        ]
    );

    return bless $self, $class;
}

sub query {
    my ($self, $q, $t) = @_;

    $self->{'out'} = '';
    $self->{'in'} = '';

    $self->{'timer'}->start($t);

    # wait for the prompt to appear
    $self->{'proc'}->pump until $self->{'out'} =~ $self->{'delim'};;

    # reset the output to forget the banner + prompt
    $self->{'out'} = '';

    # write and run the query (this echoes the query in $out :/)
    $self->{'in'}  .= "$q;\n";

    # push $in to the procs
    $self->{'proc'}->pump while length $self->{'in'};
}

1