PDO::PARAM_INT and pgsql driver

From: Date: Sun, 14 Mar 2021 20:18:53 +0000
Subject: PDO::PARAM_INT and pgsql driver
Groups: php.internals 
Request: Send a blank email to internals+get-113521@lists.php.net to get a copy of this message
Hi internals,

I just stumbled upon what I consider to be a bug with the PDO pgsql driver.
*TL;DR: the driver treats parameters bound with PARAM_INT the same as
PARAM_STR.*

Take the following example:

```
$pdo = new PDO('pgsql:host=localhost;port=5432', 'postgres',
'postgres');

$statement = $pdo->prepare("
    SELECT ST_AsText(
        ST_Transform(
            ST_GeomFromText('POINT(0 0)', 2154),
            ?
        )
    )
");

$statement->bindValue(1, 4326, PDO::PARAM_INT);
$statement->execute();
```

This fails with the following message:

PDOException: SQLSTATE[XX000]: Internal error: 7 ERROR: could not parse
> proj string '4326'


This is because the pgsql driver seems to treat everything as PARAM_STR,
despite being explicitly requested to bind the value as PARAM_INT; the
placeholder is therefore replaced with the string '4326' instead of the
integer 4326.

The problem is, in PostGIS, the ST_Transform()
<https://postgis.net/docs/ST_Transform.html>
function has different
signatures with different behaviours depending on whether the second
parameter is an integer or a string.

As far as I can see, because of this issue, *there is no way to pass an
actual integer to ST_Transform()*, which forces me to use
PostgreSQL-specific cast syntax to get the behaviour I need, in a library
<https://github.com/brick/geo> I maintain
that's supposed to work with any
GIS-enabled database.

Is there any reason why the pgsql driver doesn't respect PDO::PARAM_STR?

I asked this question on StackOverflow
<https://stackoverflow.com/q/66625661/759866>,
and was pointed to the
following bug, which I'm not sure is directly related, but was closed as
"not a bug":

https://bugs.php.net/bug.php?id=50206

*Should this be requalified as a bug and be fixed?*

Thanks in advance for your consideration,

— Benjamin


Thread (6 messages)

« previous php.internals (#113521) next »