Commit cd83758b cd83758b07623cc60cf145f1b15f2984a55c3943 by Sergey Poznyakoff

Implement retrieval by field name.

1 parent d736cb3c
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -30,7 +30,7 @@
struct mu_mysql_data
{
MYSQL *mysql;
MYSQL_RES *result;
MYSQL_RES *result;
};
......@@ -152,6 +152,7 @@ release_result (mu_sql_connection_t conn)
{
struct mu_mysql_data *mp = conn->data;
mysql_free_result (mp->result);
mp->result = NULL;
return 0;
}
......@@ -189,6 +190,27 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata)
return 0;
}
static int
get_field_number (mu_sql_connection_t conn, const char *fname, size_t *fno)
{
struct mu_mysql_data *mp = conn->data;
MYSQL_FIELD *fields;
size_t nf, i;
if (!mp->result)
return MU_ERR_NO_RESULT;
fields = mysql_fetch_fields (mp->result);
nf = mysql_num_fields (mp->result);
for (i = 0; i < nf; i++)
if (strcmp (fname, fields[i].name) == 0)
{
*fno = i;
return 0;
}
return MU_ERR_NOENT;
}
static const char *
errstr (mu_sql_connection_t conn)
{
......@@ -343,6 +365,7 @@ MU_DECL_SQL_DISPATCH_T(mysql) = {
num_tuples,
num_columns,
get_column,
get_field_number,
errstr,
};
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2005 Free Software Foundation, Inc.
Copyright (C) 2005, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -33,7 +33,9 @@ struct mu_odbc_data
SQLHDBC dbc; /* DBC */
/* Result data: */
SQLHSTMT stmt; /* Statement being executed */
mu_list_t result; /* List of returned field values */
mu_list_t result; /* List of returned field values */
char **fnames; /* A list of field names */
size_t fcount;
/* Error reporting: */
struct odbc_err_buffer
{
......@@ -189,6 +191,9 @@ release_result (mu_sql_connection_t conn)
struct mu_odbc_data *dp = conn->data;
mu_list_do (dp->result, free_char_data, NULL);
mu_list_destroy (&dp->result);
mu_argcv_free (dp->fcount, dp->fnames);
dp->fcount = 0;
dp->fnames = NULL;
return 0;
}
......@@ -197,12 +202,16 @@ num_columns (mu_sql_connection_t conn, size_t *np)
{
struct mu_odbc_data *dp = conn->data;
SQLSMALLINT count;
if (SQLNumResultCols (dp->stmt, &count) != SQL_SUCCESS)
if (dp->fcount == 0)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLNumResultCount");
return MU_ERR_SQL;
if (SQLNumResultCols (dp->stmt, &count) != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLNumResultCount");
return MU_ERR_SQL;
}
}
dp->fcount = count;
*np = count;
return 0;
}
......@@ -247,6 +256,80 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata)
return 0;
}
/* FIXME: untested */
static int
get_field_number (mu_sql_connection_t conn, const char *fname, size_t *fno)
{
size_t count;
if (!dp->fnames)
{
int rc;
rc = num_columns (conn, &count);
if (rc)
return rc;
dp->fnames = calloc(count + 1, sizeof dp->fnames[0]);
if (!dp->fnames)
return ENOMEM;
for (i = 0; i < count; i++)
{
char *name
SQLRETURN ret;
SQLSMALLINT namelen;
ret = SQLDescribeCol (dp->stmt,
i + 1,
NULL,
0,
&namelen,
NULL,
NULL,
NULL,
NULL);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLDescribeColl");
return MU_ERR_SQL;
}
name = malloc (namelen + 1);
if (!name)
return ENOMEM;
dp->fnames[i] = name;
ret = SQLDescribeCol (dp->stmt,
i + 1,
name,
namelen + 1,
&namelen,
NULL,
NULL,
NULL,
NULL);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLDescribeColl");
return MU_ERR_SQL;
}
}
dp->fnames[i] = NULL;
}
else
count = df->fcount;
for (i = 0; i < count; i++)
{
if (strcmp (fname, df->fnames) == 0)
{
*fno = i;
return 0;
}
}
return MU_ERR_NOENT;
}
#define DEFAULT_ERROR_BUFFER_SIZE 1024
static const char *
......@@ -305,5 +388,6 @@ MU_DECL_SQL_DISPATCH_T(odbc) = {
num_tuples,
num_columns,
get_column,
get_field_number,
errstr,
};
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -124,6 +124,7 @@ release_result (mu_sql_connection_t conn)
{
struct mu_pgsql_data *dp = conn->data;
PQclear (dp->res);
dp->res = NULL;
return 0;
}
......@@ -157,6 +158,17 @@ get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata)
return 0;
}
static int
get_field_number (mu_sql_connection_t conn, const char *fname, size_t *fno)
{
struct mu_pgsql_data *dp = conn->data;
if (!dp->res)
return MU_ERR_NO_RESULT;
if ((*fno = PQfnumber (dp->res, fname)) == -1)
return MU_ERR_NOENT;
return 0;
}
static const char *
errstr (mu_sql_connection_t conn)
{
......@@ -178,6 +190,7 @@ MU_DECL_SQL_DISPATCH_T(postgres) = {
num_tuples,
num_columns,
get_column,
get_field_number,
errstr,
};
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
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,
return SQL_F (conn, get_column) (conn, nrow, ncol, pdata);
}
int
mu_sql_get_field (mu_sql_connection_t conn, size_t nrow, char *fname,
char **pdata)
{
int rc;
size_t fno;
if (!conn)
return EINVAL;
switch (conn->state)
{
case mu_sql_not_connected:
return MU_ERR_DB_NOT_CONNECTED;
case mu_sql_connected:
return MU_ERR_NO_QUERY;
case mu_sql_query_run:
return MU_ERR_NO_RESULT;
case mu_sql_result_available:
break;
}
rc = SQL_F (conn, get_field_number) (conn, fname, &fno);
if (rc == 0)
rc = SQL_F (conn, get_column) (conn, nrow, fno, pdata);
return rc;
}
const char *
mu_sql_strerror (mu_sql_connection_t conn)
......