aboutsummaryrefslogtreecommitdiff
path: root/src/sq
diff options
context:
space:
mode:
Diffstat (limited to 'src/sq')
-rw-r--r--src/sq/Makefile.am4
-rw-r--r--src/sq/sq.c20
-rw-r--r--src/sq/sq_exec.c111
-rw-r--r--src/sq/sq_prepare.c83
-rw-r--r--src/sq/sq_query_helper.c22
-rw-r--r--src/sq/sq_result_helper.c22
-rw-r--r--src/sq/test_sq.c20
7 files changed, 243 insertions, 39 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.c b/src/sq/sq.c
index 089ebf0ff..1281da82e 100644
--- a/src/sq/sq.c
+++ b/src/sq/sq.c
@@ -2,16 +2,18 @@
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify it under the 5 GNUnet is free software: you can redistribute it and/or modify it
6 terms of the GNU General Public License as published by the Free Software 6 under the terms of the GNU Affero General Public License as published
7 Foundation; either version 3, or (at your option) any later version. 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
8 9
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY 10 GNUnet is distributed in the hope that it will be useful, but
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 WITHOUT ANY WARRANTY; without even the implied warranty of
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 13 Affero General Public License for more details.
13 You should have received a copy of the GNU General Public License along with 14
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/ 17*/
16/** 18/**
17 * @file sq/sq.c 19 * @file sq/sq.c
diff --git a/src/sq/sq_exec.c b/src/sq/sq_exec.c
new file mode 100644
index 000000000..34a54dcca
--- /dev/null
+++ b/src/sq/sq_exec.c
@@ -0,0 +1,111 @@
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
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18/**
19 * @file sq/sq_exec.c
20 * @brief helper functions for executing SQL statements
21 * @author Christian Grothoff
22 */
23#include "platform.h"
24#include "gnunet_sq_lib.h"
25
26
27/**
28 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors are fatal.
29 *
30 * @param sql actual SQL statement
31 * @return initialized struct
32 */
33struct GNUNET_SQ_ExecuteStatement
34GNUNET_SQ_make_execute (const char *sql)
35 {
36 struct GNUNET_SQ_ExecuteStatement es = {
37 .sql = sql,
38 .ignore_errors = GNUNET_NO
39 };
40
41 return es;
42}
43
44
45
46/**
47 * Create a `struct GNUNET_SQ_ExecuteStatement` where errors should
48 * be tolerated.
49 *
50 * @param sql actual SQL statement
51 * @return initialized struct
52 */
53struct GNUNET_SQ_ExecuteStatement
54GNUNET_SQ_make_try_execute (const char *sql)
55{
56 struct GNUNET_SQ_ExecuteStatement es = {
57 .sql = sql,
58 .ignore_errors = GNUNET_YES
59 };
60
61 return es;
62}
63
64
65/**
66 * Request execution of an array of statements @a es from Postgres.
67 *
68 * @param dbh database to execute the statements over
69 * @param es #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
70 * statements.
71 * @return #GNUNET_OK on success (modulo statements where errors can be ignored)
72 * #GNUNET_SYSERR on error
73 */
74int
75GNUNET_SQ_exec_statements (sqlite3 *dbh,
76 const struct GNUNET_SQ_ExecuteStatement *es)
77{
78 for (unsigned int i=0;NULL != es[i].sql;i++)
79 {
80 char *emsg = NULL;
81
82 if (SQLITE_OK !=
83 sqlite3_exec (dbh,
84 es[i].sql,
85 NULL,
86 NULL,
87 &emsg))
88 {
89 if (es[i].ignore_errors)
90 {
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92 "Failed to run SQL `%s': %s\n",
93 es[i].sql,
94 emsg);
95 }
96 else
97 {
98 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99 "Failed to run SQL `%s': %s\n",
100 es[i].sql,
101 emsg);
102 sqlite3_free (emsg);
103 return GNUNET_SYSERR;
104 }
105 sqlite3_free (emsg);
106 }
107 }
108 return GNUNET_OK;
109}
110
111/* end of sq_exec */
diff --git a/src/sq/sq_prepare.c b/src/sq/sq_prepare.c
new file mode 100644
index 000000000..8c46302f2
--- /dev/null
+++ b/src/sq/sq_prepare.c
@@ -0,0 +1,83 @@
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
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18/**
19 * @file sq/sq_prepare.c
20 * @brief helper functions for executing SQL statements
21 * @author Christian Grothoff
22 */
23#include "platform.h"
24#include "gnunet_sq_lib.h"
25
26
27/**
28 * Create a `struct GNUNET_SQ_PrepareStatement`
29 *
30 * @param sql actual SQL statement
31 * @param pstmt where to store the handle
32 * @return initialized struct
33 */
34struct GNUNET_SQ_PrepareStatement
35GNUNET_SQ_make_prepare (const char *sql,
36 sqlite3_stmt **pstmt)
37{
38 struct GNUNET_SQ_PrepareStatement ps = {
39 .sql = sql,
40 .pstmt = pstmt
41 };
42
43 return ps;
44}
45
46
47
48/**
49 * Prepare all statements given in the (NULL,NULL)-terminated
50 * array at @a ps
51 *
52 * @param dbh database to use
53 * @param ps array of statements to prepare
54 * @return #GNUNET_OK on success
55 */
56int
57GNUNET_SQ_prepare (sqlite3 *dbh,
58 const struct GNUNET_SQ_PrepareStatement *ps)
59{
60 for (unsigned int i=0;NULL != ps[i].sql;i++)
61 {
62 const char *epos = NULL;
63 int ret;
64
65 if (SQLITE_OK !=
66 (ret = sqlite3_prepare_v2 (dbh,
67 ps[i].sql,
68 strlen (ps[i].sql),
69 ps[i].pstmt,
70 &epos)))
71 {
72 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
73 "Failed to prepare SQL `%s': error %d at %s\n",
74 ps[i].sql,
75 ret,
76 epos);
77 return GNUNET_SYSERR;
78 }
79 }
80 return GNUNET_OK;
81}
82
83/* end of sq_prepare.c */
diff --git a/src/sq/sq_query_helper.c b/src/sq/sq_query_helper.c
index 94a3a3f1c..5f15fcec1 100644
--- a/src/sq/sq_query_helper.c
+++ b/src/sq/sq_query_helper.c
@@ -2,16 +2,18 @@
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V. 3 Copyright (C) 2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify it under the 5 GNUnet is free software: you can redistribute it and/or modify it
6 terms of the GNU General Public License as published by the Free Software 6 under the terms of the GNU Affero General Public License as published
7 Foundation; either version 3, or (at your option) any later version. 7 by the Free Software Foundation, either version 3 of the License,
8 8 or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY 9
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 10 GNUnet is distributed in the hope that it will be useful, but
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 You should have received a copy of the GNU General Public License along with 13 Affero General Public License for more details.
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> 14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/ 17*/
16/** 18/**
17 * @file sq/sq_query_helper.c 19 * @file sq/sq_query_helper.c
diff --git a/src/sq/sq_result_helper.c b/src/sq/sq_result_helper.c
index f2986a053..ebbd4f814 100644
--- a/src/sq/sq_result_helper.c
+++ b/src/sq/sq_result_helper.c
@@ -3,16 +3,18 @@
3 This file is part of GNUnet 3 This file is part of GNUnet
4 Copyright (C) 2017 GNUnet e.V. 4 Copyright (C) 2017 GNUnet e.V.
5 5
6 GNUnet is free software; you can redistribute it and/or modify it under the 6 GNUnet is free software: you can redistribute it and/or modify it
7 terms of the GNU General Public License as published by the Free Software 7 under the terms of the GNU Affero General Public License as published
8 Foundation; either version 3, or (at your option) any later version. 8 by the Free Software Foundation, either version 3 of the License,
9 9 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY 10
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 GNUnet is distributed in the hope that it will be useful, but
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 You should have received a copy of the GNU General Public License along with 14 Affero General Public License for more details.
15 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> 15
16 You should have received a copy of the GNU Affero General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 18*/
17/** 19/**
18 * @file sq/sq_result_helper.c 20 * @file sq/sq_result_helper.c
diff --git a/src/sq/test_sq.c b/src/sq/test_sq.c
index 713809030..3d7685c6e 100644
--- a/src/sq/test_sq.c
+++ b/src/sq/test_sq.c
@@ -2,16 +2,18 @@
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2015, 2016, 2017 GNUnet e.V. 3 (C) 2015, 2016, 2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify it under the 5 GNUnet is free software: you can redistribute it and/or modify it
6 terms of the GNU General Public License as published by the Free Software 6 under the terms of the GNU Affero General Public License as published
7 Foundation; either version 3, or (at your option) any later version. 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
8 9
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY 10 GNUnet is distributed in the hope that it will be useful, but
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 WITHOUT ANY WARRANTY; without even the implied warranty of
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 13 Affero General Public License for more details.
13 You should have received a copy of the GNU General Public License along with 14
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/ 17*/
16/** 18/**
17 * @file sq/test_sq.c 19 * @file sq/test_sq.c