Use a proper rcode for no more rows
[freeradius.git] / src / modules / rlm_sql / drivers / rlm_sql_db2 / rlm_sql_db2.c
index 6fe2126..7f922b3 100644 (file)
@@ -40,8 +40,8 @@ RCSID("$Id$")
 #include "rlm_sql.h"
 
 typedef struct rlm_sql_conn {
-       SQLHANDLE hdbc;
-       SQLHANDLE henv;
+       SQLHANDLE dbc_handle;
+       SQLHANDLE env_handle;
        SQLHANDLE stmt;
 } rlm_sql_db2_conn_t;
 
@@ -49,14 +49,12 @@ static int _sql_socket_destructor(rlm_sql_db2_conn_t *conn)
 {
        DEBUG2("rlm_sql_db2: Socket destructor called, closing socket");
 
-       if (conn->hdbc) {
-               SQLDisconnect(conn->hdbc);
-               SQLFreeHandle(SQL_HANDLE_DBC, conn->hdbc);
+       if (conn->dbc_handle) {
+               SQLDisconnect(conn->dbc_handle);
+               SQLFreeHandle(SQL_HANDLE_DBC, conn->dbc_handle);
        }
 
-       if (conn->henv) {
-               SQLFreeHandle(SQL_HANDLE_ENV, conn->henv);
-       }
+       if (conn->env_handle) SQLFreeHandle(SQL_HANDLE_ENV, conn->env_handle);
 
        return RLM_SQL_OK;
 }
@@ -69,9 +67,9 @@ static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *c
        MEM(conn = handle->conn = talloc_zero(handle, rlm_sql_db2_conn_t));
        talloc_set_destructor(conn, _sql_socket_destructor);
 
-       /* allocate handles */
-       SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(conn->henv));
-       SQLAllocHandle(SQL_HANDLE_DBC, conn->henv, &(conn->hdbc));
+       /* Allocate handles */
+       SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(conn->env_handle));
+       SQLAllocHandle(SQL_HANDLE_DBC, conn->env_handle, &(conn->dbc_handle));
 
        /*
         *      The db2 API doesn't qualify arguments as const even when they should be.
@@ -83,7 +81,7 @@ static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t *c
                memcpy(&login, &config->sql_login, sizeof(login));
                memcpy(&password, &config->sql_password, sizeof(password));
 
-               retval = SQLConnect(conn->hdbc,
+               retval = SQLConnect(conn->dbc_handle,
                                    server, SQL_NTS,
                                    login,  SQL_NTS,
                                    password, SQL_NTS);
@@ -106,7 +104,7 @@ static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *
        conn = handle->conn;
 
        /* allocate handle for statement */
-       SQLAllocHandle(SQL_HANDLE_STMT, conn->hdbc, &(conn->stmt));
+       SQLAllocHandle(SQL_HANDLE_STMT, conn->dbc_handle, &(conn->stmt));
 
        /* execute query */
        {
@@ -177,7 +175,6 @@ static sql_rcode_t sql_fields(char const **out[], rlm_sql_handle_t *handle, UNUS
        return RLM_SQL_OK;
 }
 
-
 static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *config)
 {
        int c, i;
@@ -192,12 +189,14 @@ static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *con
        memset(retval, 0, c*sizeof(char*)+1);
 
        /* advance cursor */
-       if(SQLFetch(conn->stmt) == SQL_NO_DATA_FOUND) {
+       if (SQLFetch(conn->stmt) == SQL_NO_DATA_FOUND) {
                handle->row = NULL;
-               goto error;
+               for (i = 0; i < c; i++) free(retval[i]);
+               free(retval);
+               return RLM_SQL_NO_MORE_ROWS;
        }
 
-       for(i = 0; i < c; i++) {
+       for (i = 0; i < c; i++) {
                /* get column length */
                SQLColAttribute(conn->stmt, i+1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &len);
 
@@ -212,14 +211,6 @@ static sql_rcode_t sql_fetch_row(rlm_sql_handle_t *handle, rlm_sql_config_t *con
 
        handle->row = retval;
        return RLM_SQL_OK;
-
-error:
-       for(i = 0; i < c; i++) {
-               free(retval[i]);
-       }
-       free(retval);
-
-       return RLM_SQL_ERROR;
 }
 
 static sql_rcode_t sql_free_result(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)