C++17 RAII SDK (spyddb.hpp)

Header-only C++17 RAII wrapper with range-for iteration, std::optional accessors, and move semantics.

1 min read Official SDKs (12 Languages)

The official C++ driver (clients/cpp/include/spyddb.hpp) is a clean C++17 RAII wrapper over the C driver (spyddb.h): one protocol ABI, zero manual free calls, move-safe Result and PreparedStatement handles, range-for iteration, std::optional typed accessors, and exception-based error reporting (spyddb::Error).

1. Building & Linking#

Include spyddb.hpp, compile clients/c/src/spyddb.c, and link OpenSSL (-lssl -lcrypto):

sh
cc -O2 -c clients/c/src/spyddb.c -Iclients/c/include -o spyddb_c.o
c++ -O2 -std=c++17 -Wall -Wextra \
  -Iclients/c/include -Iclients/cpp/include \
  app.cpp spyddb_c.o -lssl -lcrypto -o app

2. Complete C++17 Example (Connect, Queries, Prepared, Transactions)#

cpp
#include "spyddb.hpp"
#include <iostream>
#include <string>

int main() {
    try {
        spyddb::Client client(
            "spydwire://app:YOUR_TOKEN@127.0.0.1:5433/default"
            "?tls=1&ca=docker/tls/ca.crt&server_name=localhost",
            10000 // timeout ms
        );

        client.ping();
        client.execute(
            "create collection if not exists metrics { id: u64 primary, service: text, latency: f64, ok: bool }"
        );

        // Parameterized write
        client.execute(
            "metrics | insert [{ id: 18446744073709551000, service: $1, latency: 12.4, ok: true }]",
            { "checkout" }
        );

        // Range-for over RAII Result rows with exact u64 preservation
        auto res = client.query("metrics | pick id, service, latency, ok | sort id");
        for (const auto &row : res) {
            auto id = row["id"].as_uint64();
            auto signed_fit = row["id"].as_int64(); // std::nullopt because u64 > INT64_MAX!
            auto service = row["service"].as_string().value_or("(null)");
            auto latency = row[2].as_double().value_or(0.0);
            auto ok = row[3].as_bool().value_or(false);

            std::cout << "id=" << (id ? std::to_string(*id) : "null")
                      << " fits_i64=" << signed_fit.has_value()
                      << " service=" << service
                      << " latency=" << latency
                      << " ok=" << ok << "\n";
        }

        // Prepared statement RAII lifecycle
        auto prep = client.prepare("metrics | where service == $1 | pick id, latency");
        auto filtered = client.query_prepared(prep, { "checkout" });
        std::cout << "Prepared hit rows: " << filtered.row_count() << "\n";
        client.close_prepared(prep);

        // Explicit transaction
        client.begin();
        try {
            client.execute("metrics | insert [{ id: 2, service: \"auth\", latency: 4.1, ok: true }]");
            client.commit();
        } catch (...) {
            client.rollback();
            throw;
        }

    } catch (const spyddb::Error &err) {
        std::cerr << "SpydDB error: " << err.what() << "\n";
        return 1;
    }
    return 0;
}

3. RAII Lifetime & Precision Guarantees#

  • spyddb::Result owns its native spyd_result *, disables copy, supports std::move, and releases memory deterministically on scope exit.
  • spyddb::Row borrows cells from its parent Result; do not let a Row outlive its Result.
  • spyddb::Value preserves exact engine digits via .text() and .kind(), exposing safe .as_int64(), .as_uint64(), .as_double(), .as_string(), .as_bool(), and .is_null(). Calling .as_int64() on a U64 exceeding INT64_MAX cleanly returns std::nullopt instead of wrapping.