PHP 8.1+ SDK

Pure PHP streams SPD2 + TLS driver with strict value accessors, prepared statements, and pooling.

1 min read Official SDKs (12 Languages)

The official PHP driver (spyddb/spyddb) implements SpydWire v2 (SPD2) binary framing over TCP or TLS 1.2/1.3 using native PHP streams (stream_socket_client, TCP_NODELAY), strict type accessors, prepared statements, interactive transactions, and connection pooling with zero external PHP extensions beyond OpenSSL/sockets.

1. Installation#

sh
composer require spyddb/spyddb

Requires PHP 8.1+.

2. Connecting, Querying & Exact Value Types#

php
<?php

declare(strict_types=1);

use SpydDB\Client;
use SpydDB\Error;

$dsn = 'spydwire://app:YOUR_TOKEN@127.0.0.1:5433/default'
     . '?tls=true&ca=docker/tls/ca.crt&server_name=localhost';

try {
    $client = new Client($dsn, timeoutMs: 10000);
    $client->ping();
    $client->useDatabase('default');

    // Parameterized insert & query
    $client->execute(
        'orders | insert [{ id: 18446744073709551000, customer: $1, amount: 149.50, paid: true }]',
        ['Ada Lovelace'],
    );

    $result = $client->query(
        'orders | where paid == $1 | pick id, customer, amount | take 20',
        [true],
    );

    foreach ($result as $row) {
        $id = $row->id;
        echo "Kind={$id->kind} Digits={$id->text()} Customer={$row->customer->string()}\n";
    }

    $client->close();
} catch (Error $e) {
    fwrite(STDERR, "SpydDB failure: {$e->getMessage()}\n");
}

3. Prepared Statements, Transactions & Pooling#

php
<?php

declare(strict_types=1);

use SpydDB\Client;
use SpydDB\Error;
use SpydDB\Pool;

$dsn = 'spydwire://app:YOUR_TOKEN@127.0.0.1:5433/default?tls=true&ca=docker/tls/ca.crt&server_name=localhost';
$client = new Client($dsn);

// 1. Prepared statements
$stmt = $client->prepare('orders | where customer == $1 | pick id, amount');
$res = $client->queryPrepared($stmt, ['Ada Lovelace']);
echo 'Prepared rows: ' . count($res) . "\n";
$client->closePrepared($stmt);

// 2. Interactive transaction
$client->begin();
try {
    $client->execute('accounts | where id == $1 | set { balance: balance_delta(balance, 0, 100000, -50) }', [1]);
    $client->execute('accounts | where id == $1 | set { balance: balance_delta(balance, 0, 100000, +50) }', [2]);
    $client->commit();
} catch (Error $e) {
    $client->rollback();
    throw $e;
}

// 3. Connection Pool
$pool = new Pool($dsn, maxOpen: 8, maxIdle: 4);
$total = $pool->query('orders | agg count() as n')->scalar()?->int();
echo "Order count: {$total}\n";
$pool->close();

4. Result, Row & Value API#

  • $result->rows() / foreach ($result as $row) / count($result): Implements IteratorAggregate & Countable; also exposes $result->first() and $result->scalar().
  • $row->columnName or $row->at($index): Lookup cells by name or 0-based position.
  • Value accessors:
    • $val->kind (U64, I64, Decimal, Text, Timestamp, Bool, Null, ...)
    • $val->text() — exact string representation (preserves u64 above PHP_INT_MAX and exact Decimal scale)
    • $val->int() — returns ?int (null if out of signed PHP int range)
    • $val->float(), $val->bool(), $val->string(), $val->dateTime() (DateTimeImmutable), $val->isNull()