SQLRETURN setResultInStmt()

in src/odbc/rsodbc/rslibpq.c [2353:2456]


SQLRETURN setResultInStmt(SQLRETURN rc, RS_STMT_INFO *pStmt, PGresult *pgResult, int readStatusFlag, ExecStatusType pqRc, int *piStop, int iArrayBinding)
{
    RS_CONN_INFO *pConn = pStmt->phdbc;
    int iAddResultInList = TRUE;

    *piStop = FALSE;

    if(readStatusFlag)
        pqRc = PQresultStatus(pgResult);

    if(!(pqRc == PGRES_COMMAND_OK
            || pqRc == PGRES_TUPLES_OK
            || pqRc == PGRES_COPY_IN
            || pqRc == PGRES_COPY_OUT
            || pqRc == PGRES_EMPTY_QUERY))
    {
        char *pError = libpqErrorMsg(pConn);

        // Even one result in error, we are retuning error.
        rc = SQL_ERROR;

        if(pError && *pError != '\0')
            addError(&pStmt->pErrorList,"HY000", pError, 0, pConn);

        // Clear this result because we are not storing it
        PQclear(pgResult);
        pgResult = NULL;
    }
    else
    {
        // Create result object
        RS_RESULT_INFO *pResult = createResultObject(pStmt, pgResult);
        
        if(pqRc == PGRES_COMMAND_OK)
        {
            // non-SELECT command

            // Add rows affected count
            char *cmdStatus = PQcmdTuples(pgResult);

            if(cmdStatus)
            {
                long long llRowsUpdated = 0;

                sscanf(cmdStatus,"%lld",&llRowsUpdated);

                if((llRowsUpdated > 0) && (llRowsUpdated > LONG_MAX))
                    llRowsUpdated = LONG_MAX;

                if(iArrayBinding && pStmt->pResultHead != NULL && pStmt->pResultHead->lRowsUpdated > 0)
                {
                    // Accumulate the count for ARRAY processing of non-SELECT command.
                    iAddResultInList = FALSE;
                    pStmt->pResultHead->lRowsUpdated += (long)llRowsUpdated;

                    // Clear this result because we are not storing it
                    PQclear(pgResult);
                    pgResult = NULL;
                    if (pResult != NULL) {
                      delete pResult;
                      pResult = NULL;
                    }
                }
                else
                    pResult->lRowsUpdated = (long)llRowsUpdated;
            }
        }
        else
        if(pqRc == PGRES_COPY_IN
            || pqRc == PGRES_COPY_OUT)
        {
            // COPY/(UNLOAD CLIENT) command result. Do nothing.
        }
        else
        if(pqRc == PGRES_TUPLES_OK)
        {
            // SELECT kind of operation returning rows

            // #of rows in memory
            pResult->iNumberOfRowsInMem = PQntuples(pgResult);

            getResultDescription(pgResult, pResult, pConn->pConnectProps->iFetchRefCursor);
        }

        // Add result to statement
        if(iAddResultInList)
            addResult(pStmt, pResult);
    } // Success

    // pqRc == CONNECTION_BAD
    if (pqRc == PGRES_COPY_IN ||
        pqRc == PGRES_COPY_OUT ||
        pqRc == PGRES_COPY_BOTH ||
        (pConn && pConn->pgConn && PQstatus(pConn->pgConn) == CONNECTION_BAD)
        )
    {
        // No need to loop any more.
        *piStop = TRUE;

        return rc;
    }

    return rc;
}