C Driver (spyddb.h)
OpenSSL TLS C driver with coalesced SPD2 frames, prepared statements, transactions, and cancellation.
1 min read Official SDKs (12 Languages)
The official C driver (clients/c) speaks SpydWire v2 (SPD2) canonical binary framing over TCP or OpenSSL TLS 1.2+ with TCP_NODELAY, single-buffer coalesced writes, 64 KiB read buffering, prepared statements, database switching, interactive transactions, and query cancellation.
1. Building & Linking#
Compile clients/c/src/spyddb.c directly into your project or link the object file with OpenSSL (-lssl -lcrypto):
sh
cc -O2 -Wall -Wextra -std=c11 \
-Iclients/c/include \
clients/c/src/spyddb.c your_app.c \
-lssl -lcrypto -o your_app2. Complete Walkthrough (Connect, Query, Prepare, Tx & Cleanup)#
c
#include "spyddb.h"
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *dsn =
"spydwire://app:YOUR_TOKEN@127.0.0.1:5433/default"
"?tls=1&ca=docker/tls/ca.crt&server_name=localhost";
spyd_client *client = spyd_open(dsn, 10000); /* 10s timeout */
if (!client || spyd_error(client)[0] != '\0') {
fprintf(stderr, "connect error: %s\n", spyd_error(client));
spyd_close(client);
return 1;
}
if (spyd_ping(client) != 0) {
fprintf(stderr, "ping failed: %s\n", spyd_error(client));
spyd_close(client);
return 1;
}
/* 1. Select target database & run DDL */
spyd_use_database(client, "default");
spyd_execute(client,
"create collection if not exists items { id: u64 primary, label: text }",
NULL, 0);
/* 2. Parameterized write */
const char *insert_params[] = { "server-rack-a1" };
int64_t affected = spyd_execute(client,
"items | insert [{ id: 18446744073709551000, label: $1 }]",
insert_params, 1);
printf("Inserted rows: %lld\n", (long long)affected);
/* 3. Prepared read */
spyd_prepared *stmt = spyd_prepare(client,
"items | where label == $1 | pick id, label");
if (stmt != NULL) {
const char *q_params[] = { "server-rack-a1" };
spyd_result *res = spyd_query_prepared(client, stmt, q_params, 1);
if (res != NULL) {
for (size_t r = 0; r < spyd_row_count(res); r++) {
const spyd_value *id = spyd_value_at(res, r, 0);
const spyd_value *label = spyd_value_at(res, r, 1);
if (!spyd_value_is_null(id)) {
printf("row[%zu] kind=%s id=%s label=%s\n",
r,
spyd_value_kind(id),
spyd_value_text(id),
spyd_value_is_null(label) ? "(null)" : spyd_value_text(label));
}
}
spyd_result_free(res);
}
spyd_close_prepared(client, stmt);
spyd_prepared_free(stmt);
}
/* 4. Interactive transaction (begin -> commit / rollback) */
if (spyd_begin(client) == 0) {
if (spyd_execute(client, "items | insert [{ id: 2, label: \"tmp\" }]", NULL, 0) < 0) {
spyd_rollback(client);
} else {
spyd_commit(client);
}
}
/* 5. Cancel an in-flight query from a second connection sharing user + tenant */
/* spyd_cancel(other_client, query_id); */
spyd_close(client);
return 0;
}3. Core C API Reference (spyddb.h)#
| Function | Purpose |
|---|---|
spyd_open(dsn, timeout_ms) | Connect & authenticate over TCP or OpenSSL TLS (timeout_ms == 0 disables timeout) |
spyd_error(client) | Retrieve human-readable last error (safe even when spyd_open failed) |
spyd_query(client, sql, params, count) | Execute read/query returning spyd_result * |
spyd_execute(client, sql, params, count) | Execute write/DDL returning affected count (or -1 on error) |
spyd_prepare / spyd_query_prepared / spyd_execute_prepared / spyd_close_prepared | Prepare, bind, execute, and release server-side plans |
spyd_use_database(client, database) | Switch active catalog/database on connection |
spyd_begin / spyd_commit / spyd_rollback | Interactive ACID transaction control |
spyd_value_kind / spyd_value_text / spyd_value_is_null | Inspect exact engine types and digits (preserves full u64 > $2^{53}$ and Decimal scale) |
spyd_result_free / spyd_prepared_free / spyd_close | Deterministic resource cleanup |