Remove hardcoded ERRCODE values from fe-connect
These values are duplicated and prone to errors where they are updated
in errcodes.h but not here. We should just use the ERRCODE values in
errcodes.h instead.
Author: Ashwin Agrawal
Author: Jimmy Yih
#include <poll.h>
#endif
+#include "utils/elog.h"
+
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -106,16 +108,6 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
*/
#define ERRCODE_APPNAME_UNKNOWN "42704"
-#undef ERRCODE_INVALID_PASSWORD
-#undef ERRCODE_CANNOT_CONNECT_NOW
-#undef ERRCODE_MIRROR_OR_QUIESCENT
-/* This is part of the protocol so just define it */
-#define ERRCODE_INVALID_PASSWORD "28P01"
-/* This too */
-#define ERRCODE_CANNOT_CONNECT_NOW "57P03"
-#define ERRCODE_MIRROR_OR_QUIESCENT "57M01"
-#define ERRCODE_MIRROR_READY "57M02"
-
/*
* fall back options if they are not specified by arguments or defined
* by environment variables
@@ -2708,6 +2700,8 @@ error_return:
static PGPing
internal_ping(PGconn *conn)
{
+ int last_sqlstate;
+
/* Say "no attempt" if we never got to PQconnectPoll */
if (!conn || !conn->options_valid)
return PQPING_NO_ATTEMPT;
@@ -2753,17 +2747,20 @@ internal_ping(PGconn *conn)
* Report postmaster is ready to accept transition message. (this is
* mainly for pg_ctl to start segment.)
*/
- if (strcmp(conn->last_sqlstate, ERRCODE_MIRROR_OR_QUIESCENT) == 0)
+ last_sqlstate = MAKE_SQLSTATE(conn->last_sqlstate[0], conn->last_sqlstate[1],
+ conn->last_sqlstate[2], conn->last_sqlstate[3],
+ conn->last_sqlstate[4]);
+ if (last_sqlstate == ERRCODE_MIRROR_OR_QUIESCENT)
return PQPING_MIRROR_OR_QUIESCENT;
- if (strcmp(conn->last_sqlstate, ERRCODE_MIRROR_READY) == 0)
+ if (last_sqlstate == ERRCODE_MIRROR_READY)
return PQPING_MIRROR_READY;
/*
* Report PQPING_REJECT if server says it's not accepting connections. (We
* distinguish this case mainly for the convenience of pg_ctl.)
*/
- if (strcmp(conn->last_sqlstate, ERRCODE_CANNOT_CONNECT_NOW) == 0)
+ if (last_sqlstate == ERRCODE_CANNOT_CONNECT_NOW)
return PQPING_REJECT;
/*
@@ -5905,11 +5902,20 @@ dot_pg_pass_warning(PGconn *conn)
{
/* If it was 'invalid authorization', add .pgpass mention */
/* only works with >= 9.0 servers */
- if (conn->dot_pgpass_used && conn->password_needed && conn->result &&
- strcmp(PQresultErrorField(conn->result, PG_DIAG_SQLSTATE),
- ERRCODE_INVALID_PASSWORD) == 0)
+ if (conn->dot_pgpass_used && conn->password_needed && conn->result)
{
char pgpassfile[MAXPGPATH];
+ char *sqlstate;
+ int sqlstate_errcode;
+
+ sqlstate = PQresultErrorField(conn->result, PG_DIAG_SQLSTATE);
+ if (sqlstate == NULL)
+ return;
+
+ sqlstate_errcode = MAKE_SQLSTATE(sqlstate[0], sqlstate[1], sqlstate[2],
+ sqlstate[3], sqlstate[4]);
+ if (sqlstate_errcode != ERRCODE_INVALID_PASSWORD)
+ return;
if (!getPgPassFilename(pgpassfile))
return;
