PQresult() return value not very handy... - Mailing list pgsql-general

From Peter de Vroomen
Subject PQresult() return value not very handy...
Date
Msg-id aeq881$pcp$1@news1.xs4all.nl
Whole thread Raw
Responses Re: PQresult() return value not very handy...  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hi,

For NULL fields in a record, PQresult returns an empty string. This is not
very usefull, as it means I will have to check the resulting string for
empty AND check for errors retrieving the value, something like:

    m_bField1IsNull = false;
    pszValue = PQresult( m_Result, nTuple, 0 );
    if( pszValue == NULL )
    {
        return( false );
    }
    if( pszValue[0] == '\0' )
    {
        if( PQgetisnull( m_Result, nTuple, 0 ))
        {
            m_bField1IsNull = true;
        }
    }
    strcpy( m_szField1, pszValue );

I have to issue 2 if-statements, one to check for error, one to check for
NULL. I have to do this for EVERY field that could be NULL, even if the
field is definately not NULL. The call to PQgetisnull is just wasted CPU
time.

It would have been better if PQresult() would return NULL when an error
occurs and also return NULL when the field is NULL. In that case my code
would become the following:

    m_bField1IsNull = false;
    pszValue = PQresult( m_Result, nTuple, 0 );
    if( pszValue == NULL )
    {
        if( PQgetisnull( m_Result, nTuple, 0 ))
        {
            m_bField1IsNull = true;
            strcpy( m_szField1, "" );
        }
        else
        {
            return( false );
        }
    }
    else
    {
        strcpy( m_szField1, pszValue );
    }

In this case, the function PQgetisnull would ONLY be called if there was a
possibility that the field actually was NULL.

Any chance that this will be changed in the future? I expect that these
kinds of things can be done throughout the libpq C API (I am just beginning
to use it, so I wouldn't actually know :-)), so it would be nice if someone
would do some optimizations like this one :-).

Please don't flame me on bugs/typing errors in the above code examples, they
are not real code, I just typed them in to explain the general idea.

Regards,

Peter



pgsql-general by date:

Previous
From: "Marc G. Fournier"
Date:
Subject: Re: Download version on website
Next
From: Jeremy Cowgar
Date:
Subject: OUTER JOIN and WHERE