GNUnet 0.27.0-17-g14611e095
 
Loading...
Searching...
No Matches
pq_prepare.c File Reference

functions to connect to libpq (PostGres) More...

#include "platform.h"
#include "pq.h"
Include dependency graph for pq_prepare.c:

Go to the source code of this file.

Functions

struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare (const char *name, const char *sql)
 Create a struct GNUNET_PQ_PreparedStatement.
 
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
 Request creation of prepared statements ps from Postgres, but do not automatically re-prepare the statement if we are disconnected from the database.
 
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_anon (struct GNUNET_PQ_Context *pg, const char *sql)
 Prepares SQL statement sql under no name ("") for connection pg.
 
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
 Request creation of prepared statements ps from Postgres.
 

Detailed Description

functions to connect to libpq (PostGres)

Author
Christian Grothoff

Definition in file pq_prepare.c.

Function Documentation

◆ GNUNET_PQ_make_prepare()

struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare ( const char *  name,
const char *  sql 
)

Create a struct GNUNET_PQ_PreparedStatement.

Parameters
namename of the statement
sqlactual SQL statement
Returns
initialized struct

Definition at line 30 of file pq_prepare.c.

32{
34 .name = name,
35 .sql = sql
36 };
37
38 return ps;
39}
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
static char * name
Name (label) of the records to list.
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
const char * sql
Actual SQL statement.

References name, ps, and GNUNET_PQ_PreparedStatement::sql.

Referenced by database_prepare(), database_setup(), init_connection(), and init_connection().

Here is the caller graph for this function:

◆ GNUNET_PQ_prepare_once()

enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_once ( struct GNUNET_PQ_Context db,
const struct GNUNET_PQ_PreparedStatement ps 
)

Request creation of prepared statements ps from Postgres, but do not automatically re-prepare the statement if we are disconnected from the database.

Parameters
dbdatabase to prepare the statements for
psGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared statements.
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 43 of file pq_prepare.c.

45{
46 for (unsigned int i = 0; NULL != ps[i].name; i++)
47 {
48 PGresult *ret;
49
51 "pq",
52 "Preparing SQL statement `%s' as `%s' on %p\n",
53 ps[i].sql,
54 ps[i].name,
55 db);
56 ret = PQprepare (db->conn,
57 ps[i].name,
58 ps[i].sql,
59 0,
60 NULL);
61 if (PGRES_COMMAND_OK != PQresultStatus (ret))
62 {
64 "pq",
65 "PQprepare (`%s' as `%s') failed with error: %s\n",
66 ps[i].sql,
67 ps[i].name,
68 PQerrorMessage (db->conn));
69 PQclear (ret);
70 ret = PQdescribePrepared (db->conn,
71 ps[i].name);
72 if (PGRES_COMMAND_OK != PQresultStatus (ret))
73 {
74 PQclear (ret);
75 return GNUNET_SYSERR;
76 }
78 "pq",
79 "Statement `%s' already known. Ignoring the issue in the hope that you are using connection pooling...\n",
80 ps[i].name);
81 }
82 PQclear (ret);
83 }
84 return GNUNET_OK;
85}
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_FS_DirectoryBuilder * db
#define GNUNET_log_from(kind, comp,...)
@ GNUNET_OK
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO

References db, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_from, GNUNET_OK, GNUNET_SYSERR, name, ps, ret, and GNUNET_PQ_PreparedStatement::sql.

Referenced by GNUNET_PQ_prepare_statements().

Here is the caller graph for this function:

◆ GNUNET_PQ_prepare_anon()

enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_anon ( struct GNUNET_PQ_Context pg,
const char *  sql 
)

Prepares SQL statement sql under no name ("") for connection pg.

Useful for prepared statements that should not be cached.

Parameters
pgdatabase handle
sqlactual SQL text
Returns
GNUNET_OK on success

Definition at line 89 of file pq_prepare.c.

91{
92 PGresult *ret;
93
95 "pq",
96 "Preparing SQL: %s\n",
97 sql);
98 ret = PQprepare (pg->conn,
99 "",
100 sql,
101 0,
102 NULL);
103 if (PGRES_COMMAND_OK != PQresultStatus (ret))
104 {
106 "pq",
107 "PQprepare (`%s') failed with error: %s\n",
108 sql,
109 PQerrorMessage (pg->conn));
110 PQclear (ret);
111 return GNUNET_SYSERR;
112 }
113 PQclear (ret);
114 return GNUNET_OK;
115}
PGconn * conn
Actual connection.
Definition pq.h:40

References GNUNET_PQ_Context::conn, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_log_from, GNUNET_OK, GNUNET_SYSERR, ret, and GNUNET_PQ_PreparedStatement::sql.

◆ GNUNET_PQ_prepare_statements()

enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements ( struct GNUNET_PQ_Context db,
const struct GNUNET_PQ_PreparedStatement ps 
)

Request creation of prepared statements ps from Postgres.

Parameters
dbdatabase to prepare the statements for
psGNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared statements.
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 119 of file pq_prepare.c.

121{
122 if (db->ps != ps)
123 {
124 /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
125 unsigned int nlen = 0; /* length of 'ps' array */
126 unsigned int xlen;
127 struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
128
129 while (NULL != ps[nlen].name)
130 nlen++;
131 xlen = nlen + db->ps_off;
132 if (xlen > db->ps_len)
133 {
134 xlen = 2 * xlen + 1;
135 rps = GNUNET_new_array (xlen,
137 if (NULL != db->ps)
138 memcpy (rps,
139 db->ps,
140 db->ps_off * sizeof (struct GNUNET_PQ_PreparedStatement));
141 GNUNET_free (db->ps);
142 db->ps_len = xlen;
143 db->ps = rps;
144 }
145 memcpy (&db->ps[db->ps_off],
146 ps,
147 nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
148 db->ps_off += nlen;
149 }
150
152 ps);
153}
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_once(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres, but do not automatically re-prepare the sta...
Definition pq_prepare.c:43

References db, GNUNET_free, GNUNET_new_array, GNUNET_PQ_prepare_once(), name, and ps.

Referenced by database_prepare(), and GNUNET_PQ_reconnect().

Here is the call graph for this function:
Here is the caller graph for this function: