Implement retrieval by field name.
Showing
4 changed files
with
154 additions
and
4 deletions
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | 1 | /* GNU Mailutils -- a suite of utilities for electronic mail |
2 | Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. | 2 | Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public | 5 | modify it under the terms of the GNU Lesser General Public |
... | @@ -152,6 +152,7 @@ release_result (mu_sql_connection_t conn) | ... | @@ -152,6 +152,7 @@ release_result (mu_sql_connection_t conn) |
152 | { | 152 | { |
153 | struct mu_mysql_data *mp = conn->data; | 153 | struct mu_mysql_data *mp = conn->data; |
154 | mysql_free_result (mp->result); | 154 | mysql_free_result (mp->result); |
155 | mp->result = NULL; | ||
155 | return 0; | 156 | return 0; |
156 | } | 157 | } |
157 | 158 | ||
... | @@ -189,6 +190,27 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata) | ... | @@ -189,6 +190,27 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata) |
189 | return 0; | 190 | return 0; |
190 | } | 191 | } |
191 | 192 | ||
193 | static int | ||
194 | get_field_number (mu_sql_connection_t conn, const char *fname, size_t *fno) | ||
195 | { | ||
196 | struct mu_mysql_data *mp = conn->data; | ||
197 | MYSQL_FIELD *fields; | ||
198 | size_t nf, i; | ||
199 | |||
200 | if (!mp->result) | ||
201 | return MU_ERR_NO_RESULT; | ||
202 | |||
203 | fields = mysql_fetch_fields (mp->result); | ||
204 | nf = mysql_num_fields (mp->result); | ||
205 | for (i = 0; i < nf; i++) | ||
206 | if (strcmp (fname, fields[i].name) == 0) | ||
207 | { | ||
208 | *fno = i; | ||
209 | return 0; | ||
210 | } | ||
211 | return MU_ERR_NOENT; | ||
212 | } | ||
213 | |||
192 | static const char * | 214 | static const char * |
193 | errstr (mu_sql_connection_t conn) | 215 | errstr (mu_sql_connection_t conn) |
194 | { | 216 | { |
... | @@ -343,6 +365,7 @@ MU_DECL_SQL_DISPATCH_T(mysql) = { | ... | @@ -343,6 +365,7 @@ MU_DECL_SQL_DISPATCH_T(mysql) = { |
343 | num_tuples, | 365 | num_tuples, |
344 | num_columns, | 366 | num_columns, |
345 | get_column, | 367 | get_column, |
368 | get_field_number, | ||
346 | errstr, | 369 | errstr, |
347 | }; | 370 | }; |
348 | 371 | ... | ... |
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | 1 | /* GNU Mailutils -- a suite of utilities for electronic mail |
2 | Copyright (C) 2005 Free Software Foundation, Inc. | 2 | Copyright (C) 2005, 2007 Free Software Foundation, Inc. |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public | 5 | modify it under the terms of the GNU Lesser General Public |
... | @@ -34,6 +34,8 @@ struct mu_odbc_data | ... | @@ -34,6 +34,8 @@ struct mu_odbc_data |
34 | /* Result data: */ | 34 | /* Result data: */ |
35 | SQLHSTMT stmt; /* Statement being executed */ | 35 | SQLHSTMT stmt; /* Statement being executed */ |
36 | mu_list_t result; /* List of returned field values */ | 36 | mu_list_t result; /* List of returned field values */ |
37 | char **fnames; /* A list of field names */ | ||
38 | size_t fcount; | ||
37 | /* Error reporting: */ | 39 | /* Error reporting: */ |
38 | struct odbc_err_buffer | 40 | struct odbc_err_buffer |
39 | { | 41 | { |
... | @@ -189,6 +191,9 @@ release_result (mu_sql_connection_t conn) | ... | @@ -189,6 +191,9 @@ release_result (mu_sql_connection_t conn) |
189 | struct mu_odbc_data *dp = conn->data; | 191 | struct mu_odbc_data *dp = conn->data; |
190 | mu_list_do (dp->result, free_char_data, NULL); | 192 | mu_list_do (dp->result, free_char_data, NULL); |
191 | mu_list_destroy (&dp->result); | 193 | mu_list_destroy (&dp->result); |
194 | mu_argcv_free (dp->fcount, dp->fnames); | ||
195 | dp->fcount = 0; | ||
196 | dp->fnames = NULL; | ||
192 | return 0; | 197 | return 0; |
193 | } | 198 | } |
194 | 199 | ||
... | @@ -198,11 +203,15 @@ num_columns (mu_sql_connection_t conn, size_t *np) | ... | @@ -198,11 +203,15 @@ num_columns (mu_sql_connection_t conn, size_t *np) |
198 | struct mu_odbc_data *dp = conn->data; | 203 | struct mu_odbc_data *dp = conn->data; |
199 | SQLSMALLINT count; | 204 | SQLSMALLINT count; |
200 | 205 | ||
206 | if (dp->fcount == 0) | ||
207 | { | ||
201 | if (SQLNumResultCols (dp->stmt, &count) != SQL_SUCCESS) | 208 | if (SQLNumResultCols (dp->stmt, &count) != SQL_SUCCESS) |
202 | { | 209 | { |
203 | mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLNumResultCount"); | 210 | mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLNumResultCount"); |
204 | return MU_ERR_SQL; | 211 | return MU_ERR_SQL; |
205 | } | 212 | } |
213 | } | ||
214 | dp->fcount = count; | ||
206 | *np = count; | 215 | *np = count; |
207 | return 0; | 216 | return 0; |
208 | } | 217 | } |
... | @@ -247,6 +256,80 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata) | ... | @@ -247,6 +256,80 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata) |
247 | return 0; | 256 | return 0; |
248 | } | 257 | } |
249 | 258 | ||
259 | /* FIXME: untested */ | ||
260 | static int | ||
261 | get_field_number (mu_sql_connection_t conn, const char *fname, size_t *fno) | ||
262 | { | ||
263 | size_t count; | ||
264 | |||
265 | if (!dp->fnames) | ||
266 | { | ||
267 | int rc; | ||
268 | |||
269 | rc = num_columns (conn, &count); | ||
270 | if (rc) | ||
271 | return rc; | ||
272 | dp->fnames = calloc(count + 1, sizeof dp->fnames[0]); | ||
273 | if (!dp->fnames) | ||
274 | return ENOMEM; | ||
275 | for (i = 0; i < count; i++) | ||
276 | { | ||
277 | char *name | ||
278 | SQLRETURN ret; | ||
279 | SQLSMALLINT namelen; | ||
280 | |||
281 | ret = SQLDescribeCol (dp->stmt, | ||
282 | i + 1, | ||
283 | NULL, | ||
284 | 0, | ||
285 | &namelen, | ||
286 | NULL, | ||
287 | NULL, | ||
288 | NULL, | ||
289 | NULL); | ||
290 | |||
291 | if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) | ||
292 | { | ||
293 | mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLDescribeColl"); | ||
294 | return MU_ERR_SQL; | ||
295 | } | ||
296 | |||
297 | name = malloc (namelen + 1); | ||
298 | if (!name) | ||
299 | return ENOMEM; | ||
300 | |||
301 | dp->fnames[i] = name; | ||
302 | ret = SQLDescribeCol (dp->stmt, | ||
303 | i + 1, | ||
304 | name, | ||
305 | namelen + 1, | ||
306 | &namelen, | ||
307 | NULL, | ||
308 | NULL, | ||
309 | NULL, | ||
310 | NULL); | ||
311 | |||
312 | if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) | ||
313 | { | ||
314 | mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLDescribeColl"); | ||
315 | return MU_ERR_SQL; | ||
316 | } | ||
317 | } | ||
318 | dp->fnames[i] = NULL; | ||
319 | } | ||
320 | else | ||
321 | count = df->fcount; | ||
322 | for (i = 0; i < count; i++) | ||
323 | { | ||
324 | if (strcmp (fname, df->fnames) == 0) | ||
325 | { | ||
326 | *fno = i; | ||
327 | return 0; | ||
328 | } | ||
329 | } | ||
330 | return MU_ERR_NOENT; | ||
331 | } | ||
332 | |||
250 | #define DEFAULT_ERROR_BUFFER_SIZE 1024 | 333 | #define DEFAULT_ERROR_BUFFER_SIZE 1024 |
251 | 334 | ||
252 | static const char * | 335 | static const char * |
... | @@ -305,5 +388,6 @@ MU_DECL_SQL_DISPATCH_T(odbc) = { | ... | @@ -305,5 +388,6 @@ MU_DECL_SQL_DISPATCH_T(odbc) = { |
305 | num_tuples, | 388 | num_tuples, |
306 | num_columns, | 389 | num_columns, |
307 | get_column, | 390 | get_column, |
391 | get_field_number, | ||
308 | errstr, | 392 | errstr, |
309 | }; | 393 | }; | ... | ... |
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | 1 | /* GNU Mailutils -- a suite of utilities for electronic mail |
2 | Copyright (C) 2004, 2005 Free Software Foundation, Inc. | 2 | Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc. |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public | 5 | modify it under the terms of the GNU Lesser General Public |
... | @@ -124,6 +124,7 @@ release_result (mu_sql_connection_t conn) | ... | @@ -124,6 +124,7 @@ release_result (mu_sql_connection_t conn) |
124 | { | 124 | { |
125 | struct mu_pgsql_data *dp = conn->data; | 125 | struct mu_pgsql_data *dp = conn->data; |
126 | PQclear (dp->res); | 126 | PQclear (dp->res); |
127 | dp->res = NULL; | ||
127 | return 0; | 128 | return 0; |
128 | } | 129 | } |
129 | 130 | ||
... | @@ -157,6 +158,17 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata) | ... | @@ -157,6 +158,17 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata) |
157 | return 0; | 158 | return 0; |
158 | } | 159 | } |
159 | 160 | ||
161 | static int | ||
162 | get_field_number (mu_sql_connection_t conn, const char *fname, size_t *fno) | ||
163 | { | ||
164 | struct mu_pgsql_data *dp = conn->data; | ||
165 | if (!dp->res) | ||
166 | return MU_ERR_NO_RESULT; | ||
167 | if ((*fno = PQfnumber (dp->res, fname)) == -1) | ||
168 | return MU_ERR_NOENT; | ||
169 | return 0; | ||
170 | } | ||
171 | |||
160 | static const char * | 172 | static const char * |
161 | errstr (mu_sql_connection_t conn) | 173 | errstr (mu_sql_connection_t conn) |
162 | { | 174 | { |
... | @@ -178,6 +190,7 @@ MU_DECL_SQL_DISPATCH_T(postgres) = { | ... | @@ -178,6 +190,7 @@ MU_DECL_SQL_DISPATCH_T(postgres) = { |
178 | num_tuples, | 190 | num_tuples, |
179 | num_columns, | 191 | num_columns, |
180 | get_column, | 192 | get_column, |
193 | get_field_number, | ||
181 | errstr, | 194 | errstr, |
182 | }; | 195 | }; |
183 | 196 | ... | ... |
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | 1 | /* GNU Mailutils -- a suite of utilities for electronic mail |
2 | Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. | 2 | Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public | 5 | modify it under the terms of the GNU Lesser General Public |
... | @@ -364,6 +364,36 @@ mu_sql_get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, | ... | @@ -364,6 +364,36 @@ mu_sql_get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, |
364 | return SQL_F (conn, get_column) (conn, nrow, ncol, pdata); | 364 | return SQL_F (conn, get_column) (conn, nrow, ncol, pdata); |
365 | } | 365 | } |
366 | 366 | ||
367 | int | ||
368 | mu_sql_get_field (mu_sql_connection_t conn, size_t nrow, char *fname, | ||
369 | char **pdata) | ||
370 | { | ||
371 | int rc; | ||
372 | size_t fno; | ||
373 | |||
374 | if (!conn) | ||
375 | return EINVAL; | ||
376 | |||
377 | switch (conn->state) | ||
378 | { | ||
379 | case mu_sql_not_connected: | ||
380 | return MU_ERR_DB_NOT_CONNECTED; | ||
381 | |||
382 | case mu_sql_connected: | ||
383 | return MU_ERR_NO_QUERY; | ||
384 | |||
385 | case mu_sql_query_run: | ||
386 | return MU_ERR_NO_RESULT; | ||
387 | |||
388 | case mu_sql_result_available: | ||
389 | break; | ||
390 | } | ||
391 | |||
392 | rc = SQL_F (conn, get_field_number) (conn, fname, &fno); | ||
393 | if (rc == 0) | ||
394 | rc = SQL_F (conn, get_column) (conn, nrow, fno, pdata); | ||
395 | return rc; | ||
396 | } | ||
367 | 397 | ||
368 | const char * | 398 | const char * |
369 | mu_sql_strerror (mu_sql_connection_t conn) | 399 | mu_sql_strerror (mu_sql_connection_t conn) | ... | ... |
-
Please register or sign in to post a comment