summaryrefslogtreecommitdiff
path: root/src/sq
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-05-09 17:33:04 +0200
committerChristian Grothoff <christian@grothoff.org>2018-05-09 17:33:04 +0200
commit8bb475af99260f1d107dbc8908268ae93960aa83 (patch)
tree1a7a1fc03424df841a6f977b137482439b09bc9f /src/sq
parent1f80a11e90ee982bffaae4685e281f75ee1c225d (diff)
downloadgnunet-8bb475af99260f1d107dbc8908268ae93960aa83.tar.gz
gnunet-8bb475af99260f1d107dbc8908268ae93960aa83.zip
implement new functions in libgnunetsq, clean up sqlite namestore plugin, implement flow control in namestore API and tests
Diffstat (limited to 'src/sq')
-rw-r--r--src/sq/Makefile.am4
-rw-r--r--src/sq/sq_exec.c109
-rw-r--r--src/sq/sq_prepare.c81
3 files changed, 193 insertions, 1 deletions
diff --git a/src/sq/Makefile.am b/src/sq/Makefile.am
index 119a94734..fb9364005 100644
--- a/src/sq/Makefile.am
+++ b/src/sq/Makefile.am
@@ -1,5 +1,5 @@
1# This Makefile.am is in the public domain 1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include 2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3 3
4if MINGW 4if MINGW
5 WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols 5 WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols
@@ -15,6 +15,8 @@ endif
15 15
16libgnunetsq_la_SOURCES = \ 16libgnunetsq_la_SOURCES = \
17 sq.c \ 17 sq.c \
18 sq_exec.c \
19 sq_prepare.c \
18 sq_query_helper.c \ 20 sq_query_helper.c \
19 sq_result_helper.c 21 sq_result_helper.c
20libgnunetsq_la_LIBADD = -lsqlite3 \ 22libgnunetsq_la_LIBADD = -lsqlite3 \
diff --git a/src/sq/sq_exec.c b/src/sq/sq_exec.c
new file mode 100644
index 000000000..c40b1fb75
--- /dev/null
+++ b/src/sq/sq_exec.c
@@ -0,0 +1,109 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2018 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/
16/**
17 * @file sq/sq_exec.c
18 * @brief helper functions for executing SQL statements
19 * @author Christian Grothoff
20 */
21#include "platform.h"
22#include "gnunet_sq_lib.h"
23
24
25/**
26 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors are fatal.
27 *
28 * @param sql actual SQL statement
29 * @return initialized struct
30 */
31struct GNUNET_SQ_ExecuteStatement
32GNUNET_SQ_make_execute (const char *sql)
33 {
34 struct GNUNET_SQ_ExecuteStatement es = {
35 .sql = sql,
36 .ignore_errors = GNUNET_NO
37 };
38
39 return es;
40}
41
42
43
44/**
45 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors should
46 * be tolerated.
47 *
48 * @param sql actual SQL statement
49 * @return initialized struct
50 */
51struct GNUNET_SQ_ExecuteStatement
52GNUNET_SQ_make_try_execute (const char *sql)
53{
54 struct GNUNET_SQ_ExecuteStatement es = {
55 .sql = sql,
56 .ignore_errors = GNUNET_YES
57 };
58
59 return es;
60}
61
62
63/**
64 * Request execution of an array of statements @a es from Postgres.
65 *
66 * @param dbh database to execute the statements over
67 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
68 * statements.
69 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
70 * #GNUNET_SYSERR on error
71 */
72int
73GNUNET_SQ_exec_statements (sqlite3 *dbh,
74 const struct GNUNET_SQ_ExecuteStatement *es)
75{
76 for (unsigned int i=0;NULL != es[i].sql;i++)
77 {
78 char *emsg = NULL;
79
80 if (SQLITE_OK !=
81 sqlite3_exec (dbh,
82 es[i].sql,
83 NULL,
84 NULL,
85 &emsg))
86 {
87 if (es[i].ignore_errors)
88 {
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "Failed to run SQL `%s': %s\n",
91 es[i].sql,
92 emsg);
93 }
94 else
95 {
96 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97 "Failed to run SQL `%s': %s\n",
98 es[i].sql,
99 emsg);
100 sqlite3_free (emsg);
101 return GNUNET_SYSERR;
102 }
103 sqlite3_free (emsg);
104 }
105 }
106 return GNUNET_OK;
107}
108
109/* end of sq_exec */
diff --git a/src/sq/sq_prepare.c b/src/sq/sq_prepare.c
new file mode 100644
index 000000000..db1047c75
--- /dev/null
+++ b/src/sq/sq_prepare.c
@@ -0,0 +1,81 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2018 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/
16/**
17 * @file sq/sq_prepare.c
18 * @brief helper functions for executing SQL statements
19 * @author Christian Grothoff
20 */
21#include "platform.h"
22#include "gnunet_sq_lib.h"
23
24
25/**
26 * Create a `struct GNUNET_SQ_PrepareStatement`
27 *
28 * @param sql actual SQL statement
29 * @param pstmt where to store the handle
30 * @return initialized struct
31 */
32struct GNUNET_SQ_PrepareStatement
33GNUNET_SQ_make_prepare (const char *sql,
34 sqlite3_stmt **pstmt)
35{
36 struct GNUNET_SQ_PrepareStatement ps = {
37 .sql = sql,
38 .pstmt = pstmt
39 };
40
41 return ps;
42}
43
44
45
46/**
47 * Prepare all statements given in the (NULL,NULL)-terminated
48 * array at @a ps
49 *
50 * @param dbh database to use
51 * @param ps array of statements to prepare
52 * @return #GNUNET_OK on success
53 */
54int
55GNUNET_SQ_prepare (sqlite3 *dbh,
56 const struct GNUNET_SQ_PrepareStatement *ps)
57{
58 for (unsigned int i=0;NULL != ps[i].sql;i++)
59 {
60 const char *epos = NULL;
61 int ret;
62
63 if (SQLITE_OK !=
64 (ret = sqlite3_prepare_v2 (dbh,
65 ps[i].sql,
66 strlen (ps[i].sql),
67 ps[i].pstmt,
68 &epos)))
69 {
70 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
71 "Failed to prepare SQL `%s': error %d at %s\n",
72 ps[i].sql,
73 ret,
74 epos);
75 return GNUNET_SYSERR;
76 }
77 }
78 return GNUNET_OK;
79}
80
81/* end of sq_prepare.c */