Refactor one conversion of SQLSTATE to string in elog.c
authorMichael Paquier
Wed, 1 Sep 2021 02:48:08 +0000 (11:48 +0900)
committerMichael Paquier
Wed, 1 Sep 2021 02:48:08 +0000 (11:48 +0900)
unpack_sql_state() has been introduced in d46bc44 to refactor the
unpacking of a SQLSTATE into a string, but it forgot one code path when
sending error reports to clients that could make use of it.  This
changes the code to also use unpack_sql_state() there, simplifying a bit
the code.

Author: Peter Smith
Discussion: https://postgr.es/m/CAHut+PuYituuD1-VVZUNcmCQuc3ZzZMPoO57POgm8tnXOkwJAA@mail.gmail.com

src/backend/utils/error/elog.c

index a3e1c59a829940847c90b63f5bc76eea05174288..816b071afaa893d4ef81d8546fa47fbc4c6ff162 100644 (file)
@@ -3312,8 +3312,6 @@ send_message_to_frontend(ErrorData *edata)
        /* New style with separate fields */
        const char *sev;
        char        tbuf[12];
-       int         ssval;
-       int         i;
 
        /* 'N' (Notice) is for nonfatal conditions, 'E' is for errors */
        pq_beginmessage(&msgbuf, (edata->elevel < ERROR) ? 'N' : 'E');
@@ -3324,17 +3322,8 @@ send_message_to_frontend(ErrorData *edata)
        pq_sendbyte(&msgbuf, PG_DIAG_SEVERITY_NONLOCALIZED);
        err_sendstring(&msgbuf, sev);
 
-       /* unpack MAKE_SQLSTATE code */
-       ssval = edata->sqlerrcode;
-       for (i = 0; i < 5; i++)
-       {
-           tbuf[i] = PGUNSIXBIT(ssval);
-           ssval >>= 6;
-       }
-       tbuf[i] = '\0';
-
        pq_sendbyte(&msgbuf, PG_DIAG_SQLSTATE);
-       err_sendstring(&msgbuf, tbuf);
+       err_sendstring(&msgbuf, unpack_sql_state(edata->sqlerrcode));
 
        /* M field is required per protocol, so always send something */
        pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_PRIMARY);