Subject: Re: Using LibPq in TAP tests via FFI

From: Alvaro Herrera <[email-masked]>
Date: Mon, 17 Jun 2024 11:07:19 +0200

On 2024-Jun-16, Andrew Dunstan wrote:

> +sub query_oneval
> +{
> +	my $self = shift;
> +	my $sql = shift;
> +	my $missing_ok = shift; # default is not ok
> +	my $conn = $self->{conn};
> +	my $result = PQexec($conn, $sql);
> +	my $ok = $result && (PQresultStatus($result) == PGRES_TUPLES_OK);
> +	unless  ($ok)
> +	{
> +		PQclear($result) if $result;
> +		return undef;
> +	}
> +	my $ntuples = PQntuples($result);
> +	return undef if ($missing_ok && !$ntuples);
> +	my $nfields = PQnfields($result);
> +	die "$ntuples tuples != 1 or $nfields fields != 1"
> +	  if $ntuples != 1 || $nfields != 1;
> +	my $val = PQgetvalue($result, 0, 0);
> +	if ($val eq "")
> +	{
> +		$val = undef if PGgetisnull($result, 0, 0);
> +	}
> +	PQclear($result);
> +	return $val;
> +}

Hmm, here you use PGgetisnull, is that a typo for PQgetisnull?  If it
is, then I wonder why doesn't this fail in some obvious way?  Is this
part dead code maybe?

> +# return tuples like psql's -A -t mode.
> +
> +sub query_tuples
> +{
> +	my $self = shift;
> +	my @results;
> +	foreach my $sql (@_)
> +	{
> +		my $res = $self->query($sql);
> +		# join will render undef as an empty string here
> +		no warnings qw(uninitialized);
> +		my @tuples = map { join('|', @$_); } @{$res->{rows}};
> +		push(@results, join("\n",@tuples));
> +	}
> +	return join("\n",@results);
> +}

You made this function join the tuples from multiple queries together,
but the output format doesn't show anything for queries that return
empty.  I think this strategy doesn't cater for the case of comparing
results from multiple queries very well, because it might lead to sets
of queries that return empty result for different queries reported as
identical when they aren't.  Maybe add a separator line between the
results from each query, when there's more than one?  (Perhaps just
"join('--\n', @results)" in that last line does the trick?)

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"The Postgresql hackers have what I call a "NASA space shot" mentality.
 Quite refreshing in a world of "weekend drag racer" developers."
(Scott Marlowe)