From bf613fa48a678dbc8a03dade6cad6f043e2e50f9 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 2 Sep 2019 18:32:15 +0200 Subject: [PATCH] postgres: Add postgres statement rewriting support Signed-off-by: Christian Decker --- devtools/sql-rewrite.py | 4 ++++ requirements.txt | 1 - wallet/Makefile | 8 ++++++-- wallet/db_postgres.c | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 wallet/db_postgres.c diff --git a/devtools/sql-rewrite.py b/devtools/sql-rewrite.py index 98d693bc2..c7458fbcb 100755 --- a/devtools/sql-rewrite.py +++ b/devtools/sql-rewrite.py @@ -10,8 +10,12 @@ class Sqlite3Rewriter(object): return query +class PostgresRewriter(Sqlite3Rewriter): + pass + rewriters = { "sqlite3": Sqlite3Rewriter(), + "postgres": PostgresRewriter(), } template = Template("""#ifndef LIGHTNINGD_WALLET_GEN_DB_${f.upper()} diff --git a/requirements.txt b/requirements.txt index 5cca28d2b..5e137d42e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -sqlparse==0.3.0 mako==1.0.14 mrkd==0.1.5 diff --git a/wallet/Makefile b/wallet/Makefile index 53993c5b4..2529a3888 100644 --- a/wallet/Makefile +++ b/wallet/Makefile @@ -12,6 +12,7 @@ WALLET_LIB_SRC := \ wallet/walletrpc.c WALLET_DB_DRIVERS := \ + wallet/db_postgres.c \ wallet/db_sqlite3.c WALLET_LIB_OBJS := $(WALLET_LIB_SRC:.c=.o) $(WALLET_DB_DRIVERS:.c=.o) @@ -29,7 +30,9 @@ check-source-bolt: $(WALLET_LIB_SRC:%=bolt-check/%) $(WALLET_LIB_HEADERS:%=bolt- clean: wallet-clean +# Each database driver depends on its rewritten statements. wallet/db_sqlite3.c: wallet/gen_db_sqlite3.c +wallet/db_postgres.c: wallet/gen_db_postgres.c # The following files contain SQL-annotated statements that we need to extact SQL_FILES := \ @@ -42,12 +45,13 @@ SQL_FILES := \ wallet/statements.po: $(SQL_FILES) xgettext -kNAMED_SQL -kSQL --add-location --no-wrap --omit-header -o $@ $(SQL_FILES) -wallet/gen_db_sqlite3.c: wallet/statements.po devtools/sql-rewrite.py - devtools/sql-rewrite.py wallet/statements.po sqlite3 > wallet/gen_db_sqlite3.c +wallet/gen_db_%.c: wallet/statements.po devtools/sql-rewrite.py + devtools/sql-rewrite.py wallet/statements.po $* > wallet/gen_db_$*.c wallet-clean: $(RM) $(WALLET_LIB_OBJS) $(RM) wallet/statements.po $(RM) wallet/gen_db_sqlite3.c + $(RM) wallet/gen_db_postgres.c include wallet/test/Makefile diff --git a/wallet/db_postgres.c b/wallet/db_postgres.c new file mode 100644 index 000000000..3cb6ea389 --- /dev/null +++ b/wallet/db_postgres.c @@ -0,0 +1,36 @@ +#include +#include "gen_db_postgres.c" +#include +#include +#include +#include + +#if HAVE_POSTGRES + +struct db_config db_postgres_config = { + .name = "postgres", + .queries = NULL, + .num_queries = DB_POSTGRES_QUERY_COUNT, + .exec_fn = NULL, + .query_fn = NULL, + .step_fn = NULL, + .begin_tx_fn = NULL, + .commit_tx_fn = NULL, + .stmt_free_fn = NULL, + + .column_is_null_fn = NULL, + .column_u64_fn = NULL, + .column_int_fn = NULL, + .column_bytes_fn = NULL, + .column_blob_fn = NULL, + .column_text_fn = NULL, + + .last_insert_id_fn = NULL, + .count_changes_fn = NULL, + .setup_fn = NULL, + .teardown_fn = NULL, +}; + +AUTODATA(db_backends, &db_postgres_config); + +#endif