From 512be7c3a908437214926b43f8fc1a189a48cb54 Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Wed, 27 Feb 2019 11:50:33 +1100
Subject: [PATCH 2/8] New TargetSessionAttrsType enum

This new enum is useful to compare the requested session type
instead of comparing it with string always. This may not show
much improvement with current code, but it will be useful with
further patches
---
 src/interfaces/libpq/fe-connect.c | 12 ++++++++----
 src/interfaces/libpq/libpq-fe.h   |  6 ++++++
 src/interfaces/libpq/libpq-int.h  |  1 +
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 9bb2e9631f..24f203230b 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1294,8 +1294,11 @@ connectOptions2(PGconn *conn)
 	 */
 	if (conn->target_session_attrs)
 	{
-		if (strcmp(conn->target_session_attrs, "any") != 0
-			&& strcmp(conn->target_session_attrs, "read-write") != 0)
+		if (strcmp(conn->target_session_attrs, "any") == 0)
+			conn->requested_session_type = SESSION_TYPE_ANY;
+		else if (strcmp(conn->target_session_attrs, "read-write") == 0)
+			conn->requested_session_type = SESSION_TYPE_READ_WRITE;
+		else
 		{
 			conn->status = CONNECTION_BAD;
 			printfPQExpBuffer(&conn->errorMessage,
@@ -3479,8 +3482,7 @@ keep_going:						/* We will come back to here until there is
 				 * may just skip the test in that case.
 				 */
 				if (conn->sversion >= 70400 &&
-					conn->target_session_attrs != NULL &&
-					strcmp(conn->target_session_attrs, "read-write") == 0)
+					conn->requested_session_type != SESSION_TYPE_ANY)
 				{
 					/*
 					 * Save existing error messages across the PQsendQuery
@@ -3789,6 +3791,8 @@ makeEmptyPGconn(void)
 	conn->try_gss = true;
 #endif
 
+	conn->requested_session_type = SESSION_TYPE_ANY;
+
 	/*
 	 * We try to send at least 8K at a time, which is the usual size of pipe
 	 * buffers on Unix systems.  That way, when we are sending a large amount
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 39579cd040..ab415d684a 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -71,6 +71,12 @@ typedef enum
 	CONNECTION_CHECK_TARGET		/* Check if we have a proper target connection */
 } ConnStatusType;
 
+typedef enum
+{
+	SESSION_TYPE_ANY = 0,		/* Any session (default) */
+	SESSION_TYPE_READ_WRITE		/* Read-write session */
+}			TargetSessionAttrsType;
+
 typedef enum
 {
 	PGRES_POLLING_FAILED = 0,
diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h
index 1221ea9eef..6ba936dd10 100644
--- a/src/interfaces/libpq/libpq-int.h
+++ b/src/interfaces/libpq/libpq-int.h
@@ -366,6 +366,7 @@ struct pg_conn
 
 	/* Type of connection to make.  Possible values: any, read-write. */
 	char	   *target_session_attrs;
+	TargetSessionAttrsType requested_session_type;
 
 	/* Optional file to write trace info to */
 	FILE	   *Pfdebug;
-- 
2.20.1.windows.1

