2019-07-24 18:29:03 +02:00
|
|
|
#ifndef LIGHTNING_WALLET_DB_COMMON_H
|
|
|
|
#define LIGHTNING_WALLET_DB_COMMON_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/autodata/autodata.h>
|
2019-08-28 14:56:19 +02:00
|
|
|
#include <ccan/list/list.h>
|
2019-07-24 18:29:03 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2019-07-25 18:19:16 +02:00
|
|
|
#include <stdbool.h>
|
2019-07-25 18:15:34 +02:00
|
|
|
|
2019-07-25 18:14:16 +02:00
|
|
|
/* For testing, we want to catch fatal messages. */
|
|
|
|
#ifndef db_fatal
|
|
|
|
#define db_fatal fatal
|
|
|
|
#endif
|
|
|
|
|
2019-07-25 18:15:34 +02:00
|
|
|
struct db {
|
|
|
|
char *filename;
|
|
|
|
const char *in_transaction;
|
|
|
|
|
|
|
|
/* DB-specific context */
|
|
|
|
void *conn;
|
|
|
|
|
|
|
|
/* The configuration, including translated queries for the current
|
|
|
|
* instance. */
|
|
|
|
const struct db_config *config;
|
|
|
|
|
|
|
|
const char **changes;
|
|
|
|
|
2019-08-28 14:56:19 +02:00
|
|
|
/* List of statements that have been created but not executed yet. */
|
|
|
|
struct list_head pending_statements;
|
2019-07-25 18:15:34 +02:00
|
|
|
char *error;
|
2019-10-21 09:10:24 +02:00
|
|
|
|
|
|
|
struct log *log;
|
2019-12-18 19:26:23 +01:00
|
|
|
|
|
|
|
/* Were there any modifying statements in the current transaction?
|
|
|
|
* Used to bump the data_version in the DB.*/
|
|
|
|
bool dirty;
|
2019-12-18 19:45:24 +01:00
|
|
|
|
|
|
|
/* The current DB version we expect to update if changes are
|
|
|
|
* committed. */
|
|
|
|
u32 data_version;
|
2019-07-25 18:15:34 +02:00
|
|
|
};
|
2019-07-24 18:29:03 +02:00
|
|
|
|
|
|
|
struct db_query {
|
|
|
|
const char *name;
|
|
|
|
const char *query;
|
|
|
|
|
|
|
|
/* How many placeholders are in the query (and how many will we have
|
|
|
|
to allocate when instantiating this query)? */
|
2019-07-25 18:19:16 +02:00
|
|
|
size_t placeholders;
|
2019-07-24 18:29:03 +02:00
|
|
|
|
2019-07-25 18:19:16 +02:00
|
|
|
/* Is this a read-only query? If it is there's no need to tell plugins
|
|
|
|
* about it. */
|
|
|
|
bool readonly;
|
2019-07-24 18:29:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum db_binding_type {
|
|
|
|
DB_BINDING_UNINITIALIZED = 0,
|
|
|
|
DB_BINDING_NULL,
|
|
|
|
DB_BINDING_BLOB,
|
|
|
|
DB_BINDING_TEXT,
|
|
|
|
DB_BINDING_UINT64,
|
|
|
|
DB_BINDING_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct db_binding {
|
|
|
|
enum db_binding_type type;
|
|
|
|
union {
|
2019-09-05 16:08:54 +02:00
|
|
|
s32 i;
|
2019-07-24 18:29:03 +02:00
|
|
|
u64 u64;
|
|
|
|
const char* text;
|
|
|
|
const u8 *blob;
|
|
|
|
} v;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct db_stmt {
|
2019-08-28 14:56:19 +02:00
|
|
|
/* Our entry in the list of pending statements. */
|
|
|
|
struct list_node list;
|
|
|
|
|
2019-07-25 18:19:16 +02:00
|
|
|
/* Database we are querying */
|
|
|
|
struct db *db;
|
|
|
|
|
2019-07-24 18:29:03 +02:00
|
|
|
/* Which SQL statement are we trying to execute? */
|
|
|
|
struct db_query *query;
|
|
|
|
|
|
|
|
/* Which parameters are we binding to the statement? */
|
|
|
|
struct db_binding *bindings;
|
|
|
|
|
|
|
|
/* Where are we calling this statement from? */
|
|
|
|
const char *location;
|
2019-07-25 18:19:16 +02:00
|
|
|
|
|
|
|
const char *error;
|
|
|
|
|
|
|
|
/* Pointer to DB-specific statement. */
|
|
|
|
void *inner_stmt;
|
2019-08-03 15:47:46 +02:00
|
|
|
|
|
|
|
bool executed;
|
2019-09-07 17:08:51 +02:00
|
|
|
|
|
|
|
int row;
|
2019-07-25 18:19:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct db_config {
|
|
|
|
const char *name;
|
|
|
|
struct db_query *queries;
|
|
|
|
size_t num_queries;
|
|
|
|
|
|
|
|
/* Function used to execute a statement that doesn't result in a
|
|
|
|
* response. */
|
|
|
|
bool (*exec_fn)(struct db_stmt *stmt);
|
|
|
|
|
|
|
|
/* Function to execute a query that will result in a response. */
|
|
|
|
bool (*query_fn)(struct db_stmt *stmt);
|
|
|
|
|
|
|
|
/* Function used to step forwards through query results. Returns
|
|
|
|
* `false` if there are no more rows to return. */
|
|
|
|
bool (*step_fn)(struct db_stmt *stmt);
|
|
|
|
|
|
|
|
bool (*begin_tx_fn)(struct db *db);
|
|
|
|
bool (*commit_tx_fn)(struct db *db);
|
|
|
|
|
|
|
|
/* The free function must make sure that any associated state stored
|
|
|
|
* in `stmt->inner_stmt` is freed correctly, setting the pointer to
|
|
|
|
* NULL after cleaning up. It will ultmately be called by the
|
|
|
|
* destructor of `struct db_stmt`, before clearing the db_stmt
|
|
|
|
* itself. */
|
|
|
|
void (*stmt_free_fn)(struct db_stmt *db_stmt);
|
2019-08-03 16:34:03 +02:00
|
|
|
|
|
|
|
/* Column access in a row. Only covers the primitives, others need to
|
|
|
|
* use these internally to translate (hence the non-allocating
|
|
|
|
* column_{text,blob}_fn since most other types want in place
|
|
|
|
* assignment. */
|
|
|
|
bool (*column_is_null_fn)(struct db_stmt *stmt, int col);
|
|
|
|
u64 (*column_u64_fn)(struct db_stmt *stmt, int col);
|
|
|
|
size_t (*column_bytes_fn)(struct db_stmt *stmt, int col);
|
|
|
|
const void *(*column_blob_fn)(struct db_stmt *stmt, int col);
|
|
|
|
const unsigned char *(*column_text_fn)(struct db_stmt *stmt, int col);
|
|
|
|
s64 (*column_int_fn)(struct db_stmt *stmt, int col);
|
2019-08-03 18:13:31 +02:00
|
|
|
|
2019-08-20 19:38:09 +02:00
|
|
|
u64 (*last_insert_id_fn)(struct db_stmt *stmt);
|
2019-08-03 18:13:31 +02:00
|
|
|
size_t (*count_changes_fn)(struct db_stmt *stmt);
|
2019-08-08 13:42:39 +02:00
|
|
|
|
|
|
|
bool (*setup_fn)(struct db *db);
|
|
|
|
void (*teardown_fn)(struct db *db);
|
2019-12-11 19:45:43 +01:00
|
|
|
|
|
|
|
u32 (*version)(struct db *db);
|
2019-07-24 18:29:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Provide a way for DB backends to register themselves */
|
|
|
|
AUTODATA_TYPE(db_backends, struct db_config);
|
|
|
|
|
2019-08-28 16:25:42 +02:00
|
|
|
/**
|
|
|
|
* Report a statement that changes the wallet
|
|
|
|
*
|
|
|
|
* Allows the DB driver to report an expanded statement during
|
|
|
|
* execution. Changes are queued up and reported to the `db_write` plugin hook
|
|
|
|
* upon committing.
|
|
|
|
*/
|
|
|
|
void db_changes_add(struct db_stmt *db_stmt, const char * expanded);
|
|
|
|
|
|
|
|
|
2019-07-24 18:29:03 +02:00
|
|
|
#endif /* LIGHTNING_WALLET_DB_COMMON_H */
|