Search code examples
phppdocassandracql3

Cassandra and PDO


I've installed the YACassandraPDO driver for PHP. I have a schema where one table has a average DOUBLE field and is using CQL3/Casssandra 2.0.

And attempted the following:

$stmt = $this->connection->prepare('INSERT INTO a (average) VALUES (:average)');
$stmt->bindValue(':average', 2.2);
$stmt->execute();

This cause a CQLSTATE[HY000] [2] Invalid STRING constant (2.2) for average of type double.

How should I go about getting this to work?

Seeing that PDO does not have any way of specifying types of a float/double (docs), it is starting to look like PDO is no-go unless I want my field types to all be of type TEXT which in turn has other undesirable implications.


Solution

  • It seems that it is expecting a string by default, as you have already found out. According to the documentation on github, and admittedly a little bit counterintuitively, you need to use the PARAM_INT for double, too. So try this instead:

    $stmt->bindValue(':average', 2.2, PDO::PARAM_INT);
    or maybe even
    $stmt->bindValue(':average', "2.2", PDO::PARAM_INT);
    

    The types are defined as follows:

    text        PDO::PARAM_STR 
    blob        PDO::PARAM_STR 
    varchar     PDO::PARAM_STR 
    uuid        PDO::PARAM_INT
    int         PDO::PARAM_INT
    bigint      PDO::PARAM_INT
    float       PDO::PARAM_INT
    double      PDO::PARAM_INT
    decimal     PDO::PARAM_INT
    

    See the relevant "binding" section in the driver's documentation on github for more information.

    Hope that actually works. I do not have a running php test environment to test this.