CDB
Opens a PostgreSQL connection from a connection string. Connection failure raises CError; one instance represents one connection.
Databases
A concise PostgreSQL interface for schemas, parameterized queries, and transactions.
#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.
class CDBOpens a PostgreSQL connection from a connection string. Connection failure raises CError; one instance represents one connection.
~CDB();
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.
void dropTable(const cstr& table, bool ifExists = false);
Drops the named table. ifExists suppresses a missing-table error; identifiers are quoted by the wrapper.
Creates an index from the supplied definition for the named table. The definition supplies its name, columns, and optional uniqueness.
void dropIndex(const cstr& index, bool ifExists = false);
Drops a named index. ifExists permits a missing index without an error.
Returns all user-table descriptions, or one description/cnone for the named overload.
Executes one parameterized command and returns affected rows. Use query/get methods for results and transaction methods for transaction control.
Visits positional result rows. Return true to continue or false to stop; the method returns true only when exhausted.
Visits named result rows under the same callback and completion rules as query().
Returns the first row as a vector, or cnone when no row exists. Use ORDER BY if the selected first row matters.
Returns the first row as a map, or cnone when no row exists.
Accepts one row map or a vector of row maps with identical keys for bulk insertion. Returns the row count.
Updates rows matching a nonempty map of native-column equality conditions, combined with AND.
Deletes rows matching a nonempty map of native-column equality conditions, combined with AND.
void begin();
Starts an explicit transaction. A transaction must not already be active.
void commit();
Commits the active transaction and returns the connection to an idle transaction state.
void rollback();
Discards changes in the active transaction. Use after failure when managing transactions explicitly.
void setAutoCommit(bool enabled);
Requires no active transaction. When disabled, the next operation starts a transaction automatically.
bool autoCommit() const;
Reports whether this connection automatically commits each operation. Use setAutoCommit() to control transaction grouping.
bool inTransaction() const;
Reports whether this connection currently has an active transaction, including one started automatically when auto-commit is disabled.
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.