Databases

CDB

A concise PostgreSQL interface for schemas, parameterized queries, and transactions.

C++23 mc/CDB.h
#include <mc/CDB.h>

Owns one PostgreSQL connection. Synchronize concurrent access externally, and use another connection for database work inside a row callback.

Parameters use $1, $2, and so on. Returned values own their contents; rows passed to query callbacks are borrowed for that call.

Auto-commit is initially enabled. Destruction rolls back pending work and never commits implicitly.

Jump to a declaration · 26

CDB

class CDB

Types, constants & data

using RowFunc = std::function<bool(const cvec&)>;
using MapFunc = std::function<bool(const cmap&)>;

Methods

CDB

explicit CDB(const cstr& connection);
CDB(const CDB&) = delete;

Opens a PostgreSQL connection from a connection string. Connection failure raises CError; one instance represents one connection.

createTable

void createTable(const cvar& definition);
void createTable(const cstr& cson);
void createTable(const char* cson);

Accepts a schema map or CSON text with name, columns, and optional primaryKey. Column entries specify name, type, and options such as nullable.

dropTable

void dropTable(const cstr& table, bool ifExists = false);

Drops the named table. ifExists suppresses a missing-table error; identifiers are quoted by the wrapper.

createIndex

void createIndex(const cstr& table, const cvar& definition);

Creates an index from the supplied definition for the named table. The definition supplies its name, columns, and optional uniqueness.

dropIndex

void dropIndex(const cstr& index, bool ifExists = false);

Drops a named index. ifExists permits a missing index without an error.

schema

cvar schema();
cvar schema(const cstr& table);

Returns all user-table descriptions, or one description/cnone for the named overload.

execute

uint64_t execute(const cstr& sql, const cvec& params = {});

Executes one parameterized command and returns affected rows. Use query/get methods for results and transaction methods for transaction control.

query

bool query(const cstr& sql, const cvec& params, const RowFunc& each);

Visits positional result rows. Return true to continue or false to stop; the method returns true only when exhausted.

queryMap

bool queryMap(const cstr& sql, const cvec& params, const MapFunc& each);

Visits named result rows under the same callback and completion rules as query().

get

cvar get(const cstr& sql, const cvec& params = {});

Returns the first row as a vector, or cnone when no row exists. Use ORDER BY if the selected first row matters.

getMap

cvar getMap(const cstr& sql, const cvec& params = {});

Returns the first row as a map, or cnone when no row exists.

insert

uint64_t insert(const cstr& table, const cvar& row);

Accepts one row map or a vector of row maps with identical keys for bulk insertion. Returns the row count.

update

uint64_t update(const cstr& table, const cvar& changes, const cvar& match);

Updates rows matching a nonempty map of native-column equality conditions, combined with AND.

erase

uint64_t erase(const cstr& table, const cvar& match);

Deletes rows matching a nonempty map of native-column equality conditions, combined with AND.

begin

void begin();

Starts an explicit transaction. A transaction must not already be active.

commit

void commit();

Commits the active transaction and returns the connection to an idle transaction state.

rollback

void rollback();

Discards changes in the active transaction. Use after failure when managing transactions explicitly.

setAutoCommit

void setAutoCommit(bool enabled);

Requires no active transaction. When disabled, the next operation starts a transaction automatically.

autoCommit

bool autoCommit() const;

Reports whether this connection automatically commits each operation. Use setAutoCommit() to control transaction grouping.

inTransaction

bool inTransaction() const;

Reports whether this connection currently has an active transaction, including one started automatically when auto-commit is disabled.

transaction

void transaction(const std::function<void(CDB&)>& body);

Commits when the callback returns normally and rolls back on exception. Do not nest or explicitly control the transaction inside the callback.