summaryrefslogtreecommitdiff
path: root/src/pq/pq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq.c')
-rw-r--r--src/pq/pq.c122
1 files changed, 61 insertions, 61 deletions
diff --git a/src/pq/pq.c b/src/pq/pq.c
index a581b4277..7e97c8f72 100644
--- a/src/pq/pq.c
+++ b/src/pq/pq.c
@@ -38,17 +38,17 @@
38 * @return postgres result 38 * @return postgres result
39 */ 39 */
40PGresult * 40PGresult *
41GNUNET_PQ_exec_prepared(PGconn *db_conn, 41GNUNET_PQ_exec_prepared (PGconn *db_conn,
42 const char *name, 42 const char *name,
43 const struct GNUNET_PQ_QueryParam *params) 43 const struct GNUNET_PQ_QueryParam *params)
44{ 44{
45 unsigned int len; 45 unsigned int len;
46 unsigned int i; 46 unsigned int i;
47 47
48 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
49 "Running prepared statement `%s' on %p\n", 49 "Running prepared statement `%s' on %p\n",
50 name, 50 name,
51 db_conn); 51 db_conn);
52 /* count the number of parameters */ 52 /* count the number of parameters */
53 len = 0; 53 len = 0;
54 for (i = 0; 0 != params[i].num_params; i++) 54 for (i = 0; 0 != params[i].num_params; i++)
@@ -71,41 +71,41 @@ GNUNET_PQ_exec_prepared(PGconn *db_conn,
71 off = 0; 71 off = 0;
72 soff = 0; 72 soff = 0;
73 for (i = 0; 0 != params[i].num_params; i++) 73 for (i = 0; 0 != params[i].num_params; i++)
74 {
75 const struct GNUNET_PQ_QueryParam *x = &params[i];
76
77 ret = x->conv (x->conv_cls,
78 x->data,
79 x->size,
80 &param_values[off],
81 &param_lengths[off],
82 &param_formats[off],
83 x->num_params,
84 &scratch[soff],
85 len - soff);
86 if (ret < 0)
74 { 87 {
75 const struct GNUNET_PQ_QueryParam *x = &params[i]; 88 for (off = 0; off < soff; off++)
76 89 GNUNET_free (scratch[off]);
77 ret = x->conv(x->conv_cls, 90 return NULL;
78 x->data,
79 x->size,
80 &param_values[off],
81 &param_lengths[off],
82 &param_formats[off],
83 x->num_params,
84 &scratch[soff],
85 len - soff);
86 if (ret < 0)
87 {
88 for (off = 0; off < soff; off++)
89 GNUNET_free(scratch[off]);
90 return NULL;
91 }
92 soff += ret;
93 off += x->num_params;
94 } 91 }
95 GNUNET_assert(off == len); 92 soff += ret;
96 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, 93 off += x->num_params;
97 "pq", 94 }
98 "Executing prepared SQL statement `%s'\n", 95 GNUNET_assert (off == len);
99 name); 96 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
100 res = PQexecPrepared(db_conn, 97 "pq",
101 name, 98 "Executing prepared SQL statement `%s'\n",
102 len, 99 name);
103 (const char **)param_values, 100 res = PQexecPrepared (db_conn,
104 param_lengths, 101 name,
105 param_formats, 102 len,
106 1); 103 (const char **) param_values,
104 param_lengths,
105 param_formats,
106 1);
107 for (off = 0; off < soff; off++) 107 for (off = 0; off < soff; off++)
108 GNUNET_free(scratch[off]); 108 GNUNET_free (scratch[off]);
109 return res; 109 return res;
110 } 110 }
111} 111}
@@ -118,14 +118,14 @@ GNUNET_PQ_exec_prepared(PGconn *db_conn,
118 * @param rs reult specification to clean up 118 * @param rs reult specification to clean up
119 */ 119 */
120void 120void
121GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs) 121GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
122{ 122{
123 unsigned int i; 123 unsigned int i;
124 124
125 for (i = 0; NULL != rs[i].conv; i++) 125 for (i = 0; NULL != rs[i].conv; i++)
126 if (NULL != rs[i].cleaner) 126 if (NULL != rs[i].cleaner)
127 rs[i].cleaner(rs[i].cls, 127 rs[i].cleaner (rs[i].cls,
128 rs[i].dst); 128 rs[i].dst);
129} 129}
130 130
131 131
@@ -141,32 +141,32 @@ GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
141 * #GNUNET_SYSERR if a result was invalid (non-existing field) 141 * #GNUNET_SYSERR if a result was invalid (non-existing field)
142 */ 142 */
143int 143int
144GNUNET_PQ_extract_result(PGresult *result, 144GNUNET_PQ_extract_result (PGresult *result,
145 struct GNUNET_PQ_ResultSpec *rs, 145 struct GNUNET_PQ_ResultSpec *rs,
146 int row) 146 int row)
147{ 147{
148 unsigned int i; 148 unsigned int i;
149 int ret; 149 int ret;
150 150
151 for (i = 0; NULL != rs[i].conv; i++) 151 for (i = 0; NULL != rs[i].conv; i++)
152 {
153 struct GNUNET_PQ_ResultSpec *spec;
154
155 spec = &rs[i];
156 ret = spec->conv (spec->cls,
157 result,
158 row,
159 spec->fname,
160 &spec->dst_size,
161 spec->dst);
162 if (GNUNET_OK != ret)
152 { 163 {
153 struct GNUNET_PQ_ResultSpec *spec; 164 GNUNET_PQ_cleanup_result (rs);
154 165 return GNUNET_SYSERR;
155 spec = &rs[i];
156 ret = spec->conv(spec->cls,
157 result,
158 row,
159 spec->fname,
160 &spec->dst_size,
161 spec->dst);
162 if (GNUNET_OK != ret)
163 {
164 GNUNET_PQ_cleanup_result(rs);
165 return GNUNET_SYSERR;
166 }
167 if (NULL != spec->result_size)
168 *spec->result_size = spec->dst_size;
169 } 166 }
167 if (NULL != spec->result_size)
168 *spec->result_size = spec->dst_size;
169 }
170 return GNUNET_OK; 170 return GNUNET_OK;
171} 171}
172 172