diff options
Diffstat (limited to 'src/pq/pq_prepare.c')
-rw-r--r-- | src/pq/pq_prepare.c | 89 |
1 files changed, 39 insertions, 50 deletions
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c index fcf1cc793..e5970144d 100644 --- a/src/pq/pq_prepare.c +++ b/src/pq/pq_prepare.c | |||
@@ -26,68 +26,23 @@ | |||
26 | #include "pq.h" | 26 | #include "pq.h" |
27 | 27 | ||
28 | 28 | ||
29 | /** | ||
30 | * Create a `struct GNUNET_PQ_PreparedStatement`. | ||
31 | * | ||
32 | * @param name name of the statement | ||
33 | * @param sql actual SQL statement | ||
34 | * @param num_args number of arguments in the statement | ||
35 | * @return initialized struct | ||
36 | */ | ||
37 | struct GNUNET_PQ_PreparedStatement | 29 | struct GNUNET_PQ_PreparedStatement |
38 | GNUNET_PQ_make_prepare (const char *name, | 30 | GNUNET_PQ_make_prepare (const char *name, |
39 | const char *sql, | 31 | const char *sql) |
40 | unsigned int num_args) | ||
41 | { | 32 | { |
42 | struct GNUNET_PQ_PreparedStatement ps = { | 33 | struct GNUNET_PQ_PreparedStatement ps = { |
43 | .name = name, | 34 | .name = name, |
44 | .sql = sql, | 35 | .sql = sql |
45 | .num_arguments = num_args | ||
46 | }; | 36 | }; |
47 | 37 | ||
48 | return ps; | 38 | return ps; |
49 | } | 39 | } |
50 | 40 | ||
51 | 41 | ||
52 | /** | ||
53 | * Request creation of prepared statements @a ps from Postgres. | ||
54 | * | ||
55 | * @param db database to prepare the statements for | ||
56 | * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared | ||
57 | * statements. | ||
58 | * @return #GNUNET_OK on success, | ||
59 | * #GNUNET_SYSERR on error | ||
60 | */ | ||
61 | enum GNUNET_GenericReturnValue | 42 | enum GNUNET_GenericReturnValue |
62 | GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, | 43 | GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db, |
63 | const struct GNUNET_PQ_PreparedStatement *ps) | 44 | const struct GNUNET_PQ_PreparedStatement *ps) |
64 | { | 45 | { |
65 | if (db->ps != ps) | ||
66 | { | ||
67 | /* add 'ps' to list db->ps of prepared statements to run on reconnect! */ | ||
68 | unsigned int olen = 0; /* length of existing 'db->ps' array */ | ||
69 | unsigned int nlen = 0; /* length of 'ps' array */ | ||
70 | struct GNUNET_PQ_PreparedStatement *rps; /* combined array */ | ||
71 | |||
72 | if (NULL != db->ps) | ||
73 | while (NULL != db->ps[olen].name) | ||
74 | olen++; | ||
75 | while (NULL != ps[nlen].name) | ||
76 | nlen++; | ||
77 | rps = GNUNET_new_array (olen + nlen + 1, | ||
78 | struct GNUNET_PQ_PreparedStatement); | ||
79 | if (NULL != db->ps) | ||
80 | memcpy (rps, | ||
81 | db->ps, | ||
82 | olen * sizeof (struct GNUNET_PQ_PreparedStatement)); | ||
83 | memcpy (&rps[olen], | ||
84 | ps, | ||
85 | nlen * sizeof (struct GNUNET_PQ_PreparedStatement)); | ||
86 | GNUNET_free (db->ps); | ||
87 | db->ps = rps; | ||
88 | } | ||
89 | |||
90 | /* actually prepare statements */ | ||
91 | for (unsigned int i = 0; NULL != ps[i].name; i++) | 46 | for (unsigned int i = 0; NULL != ps[i].name; i++) |
92 | { | 47 | { |
93 | PGresult *ret; | 48 | PGresult *ret; |
@@ -100,7 +55,7 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, | |||
100 | ret = PQprepare (db->conn, | 55 | ret = PQprepare (db->conn, |
101 | ps[i].name, | 56 | ps[i].name, |
102 | ps[i].sql, | 57 | ps[i].sql, |
103 | ps[i].num_arguments, | 58 | 0, |
104 | NULL); | 59 | NULL); |
105 | if (PGRES_COMMAND_OK != PQresultStatus (ret)) | 60 | if (PGRES_COMMAND_OK != PQresultStatus (ret)) |
106 | { | 61 | { |
@@ -129,4 +84,38 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, | |||
129 | } | 84 | } |
130 | 85 | ||
131 | 86 | ||
87 | enum GNUNET_GenericReturnValue | ||
88 | GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, | ||
89 | const struct GNUNET_PQ_PreparedStatement *ps) | ||
90 | { | ||
91 | if (db->ps != ps) | ||
92 | { | ||
93 | /* add 'ps' to list db->ps of prepared statements to run on reconnect! */ | ||
94 | unsigned int olen = 0; /* length of existing 'db->ps' array */ | ||
95 | unsigned int nlen = 0; /* length of 'ps' array */ | ||
96 | struct GNUNET_PQ_PreparedStatement *rps; /* combined array */ | ||
97 | |||
98 | if (NULL != db->ps) | ||
99 | while (NULL != db->ps[olen].name) | ||
100 | olen++; | ||
101 | while (NULL != ps[nlen].name) | ||
102 | nlen++; | ||
103 | rps = GNUNET_new_array (olen + nlen + 1, | ||
104 | struct GNUNET_PQ_PreparedStatement); | ||
105 | if (NULL != db->ps) | ||
106 | memcpy (rps, | ||
107 | db->ps, | ||
108 | olen * sizeof (struct GNUNET_PQ_PreparedStatement)); | ||
109 | memcpy (&rps[olen], | ||
110 | ps, | ||
111 | nlen * sizeof (struct GNUNET_PQ_PreparedStatement)); | ||
112 | GNUNET_free (db->ps); | ||
113 | db->ps = rps; | ||
114 | } | ||
115 | |||
116 | return GNUNET_PQ_prepare_once (db, | ||
117 | ps); | ||
118 | } | ||
119 | |||
120 | |||
132 | /* end of pq/pq_prepare.c */ | 121 | /* end of pq/pq_prepare.c */ |