Skip to content

Commit

Permalink
refactor: factor out receiveParameterStatus so all the ParameterStatu…
Browse files Browse the repository at this point in the history
…s messages are handled in the same way

At least, TimeZone parameter status message was not handled in "process startup", then queryExecutor.timeZone resulted in null value,
and it resulted in QueryExecutor.timeZone to be null.

It is not known there were issues with that, however it makes sense to keep processing logic consistent
  • Loading branch information
vlsi committed Jan 8, 2018
1 parent be06946 commit a94cfea
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 118 deletions.
3 changes: 3 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/core/PGStream.java
Expand Up @@ -147,6 +147,9 @@ public Encoding getEncoding() {
* @throws IOException if something goes wrong * @throws IOException if something goes wrong
*/ */
public void setEncoding(Encoding encoding) throws IOException { public void setEncoding(Encoding encoding) throws IOException {
if (this.encoding != null && this.encoding.name().equals(encoding.name())) {
return;
}
// Close down any old writer. // Close down any old writer.
if (encodingWriter != null) { if (encodingWriter != null) {
encodingWriter.close(); encodingWriter.close();
Expand Down
200 changes: 82 additions & 118 deletions pgjdbc/src/main/java/org/postgresql/core/v3/QueryExecutorImpl.java
Expand Up @@ -1198,45 +1198,12 @@ CopyOperationImpl processCopyResults(CopyOperationImpl op, boolean block)
break; break;
case 'S': // Parameter Status case 'S': // Parameter Status
{ {
int l_len = pgStream.receiveInteger4(); try {
String name = pgStream.receiveString(); receiveParameterStatus();
String value = pgStream.receiveString(); } catch (SQLException e) {

error = e;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, " <=BE ParameterStatus({0} = {1})", new Object[]{name, value});
}

if (name.equals("client_encoding") && !value.equalsIgnoreCase("UTF8")
&& !allowEncodingChanges) {
close(); // we're screwed now; we can't trust any subsequent string.
error = new PSQLException(GT.tr(
"The server''s client_encoding parameter was changed to {0}. The JDBC driver requires client_encoding to be UTF8 for correct operation.",
value), PSQLState.CONNECTION_FAILURE);
endReceiving = true;
}

if (name.equals("DateStyle") && !value.startsWith("ISO,")) {
close(); // we're screwed now; we can't trust any subsequent date.
error = new PSQLException(GT.tr(
"The server''s DateStyle parameter was changed to {0}. The JDBC driver requires DateStyle to begin with ISO for correct operation.",
value), PSQLState.CONNECTION_FAILURE);
endReceiving = true; endReceiving = true;
} }

if (name.equals("standard_conforming_strings")) {
if (value.equals("on")) {
setStandardConformingStrings(true);
} else if (value.equals("off")) {
setStandardConformingStrings(false);
} else {
close();
// we're screwed now; we don't know how to escape string literals
error = new PSQLException(GT.tr(
"The server''s standard_conforming_strings parameter was reported as {0}. The JDBC driver expected on or off.",
value), PSQLState.CONNECTION_FAILURE);
endReceiving = true;
}
}
break; break;
} }


Expand Down Expand Up @@ -2236,52 +2203,12 @@ protected void processResults(ResultHandler handler, int flags) throws IOExcepti


case 'S': // Parameter Status case 'S': // Parameter Status
{ {
int l_len = pgStream.receiveInteger4(); try {
String name = pgStream.receiveString(); receiveParameterStatus();
String value = pgStream.receiveString(); } catch (SQLException e) {

handler.handleError(e);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, " <=BE ParameterStatus({0} = {1})", new Object[]{name, value});
}

if (name.equals("client_encoding") && !value.equalsIgnoreCase("UTF8")
&& !allowEncodingChanges) {
close(); // we're screwed now; we can't trust any subsequent string.
handler.handleError(new PSQLException(GT.tr(
"The server''s client_encoding parameter was changed to {0}. The JDBC driver requires client_encoding to be UTF8 for correct operation.",
value), PSQLState.CONNECTION_FAILURE));
endQuery = true;
}

if (name.equals("DateStyle") && !value.startsWith("ISO,")) {
close(); // we're screwed now; we can't trust any subsequent date.
handler.handleError(new PSQLException(GT.tr(
"The server''s DateStyle parameter was changed to {0}. The JDBC driver requires DateStyle to begin with ISO for correct operation.",
value), PSQLState.CONNECTION_FAILURE));
endQuery = true; endQuery = true;
} }

if (name.equals("standard_conforming_strings")) {
if (value.equals("on")) {
setStandardConformingStrings(true);
} else if (value.equals("off")) {
setStandardConformingStrings(false);
} else {
close();
// we're screwed now; we don't know how to escape string literals
handler.handleError(new PSQLException(GT.tr(
"The server''s standard_conforming_strings parameter was reported as {0}. The JDBC driver expected on or off.",
value), PSQLState.CONNECTION_FAILURE));
endQuery = true;
}
}

if ("TimeZone".equals(name)) {
setTimeZone(TimestampUtils.parseBackendTimeZone(value));
}
if ("application_name".equals(name)) {
setApplicationName(value);
}
break; break;
} }


Expand Down Expand Up @@ -2639,43 +2566,7 @@ public void readStartupMessages() throws IOException, SQLException {


case 'S': case 'S':
// ParameterStatus // ParameterStatus
int l_len = pgStream.receiveInteger4(); receiveParameterStatus();
String name = pgStream.receiveString();
String value = pgStream.receiveString();

if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, " <=BE ParameterStatus({0} = {1})", new Object[]{name, value});
}

if ("server_version_num".equals(name)) {
setServerVersionNum(Integer.parseInt(value));
} else if ("server_version".equals(name)) {
setServerVersion(value);
} else if ("client_encoding".equals(name)) {
if (!"UTF8".equals(value)) {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
pgStream.setEncoding(Encoding.getDatabaseEncoding("UTF8"));
} else if ("standard_conforming_strings".equals(name)) {
if ("on".equals(value)) {
setStandardConformingStrings(true);
} else if ("off".equals(value)) {
setStandardConformingStrings(false);
} else {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
} else if ("integer_datetimes".equals(name)) {
if ("on".equals(value)) {
setIntegerDateTimes(true);
} else if ("off".equals(value)) {
setIntegerDateTimes(false);
} else {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
}


break; break;


Expand All @@ -2689,7 +2580,80 @@ public void readStartupMessages() throws IOException, SQLException {
PSQLState.PROTOCOL_VIOLATION); PSQLState.PROTOCOL_VIOLATION);
} }


public void receiveParameterStatus() throws IOException, SQLException {
// ParameterStatus
int l_len = pgStream.receiveInteger4();
String name = pgStream.receiveString();
String value = pgStream.receiveString();

if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, " <=BE ParameterStatus({0} = {1})", new Object[]{name, value});
}

if (name.equals("client_encoding") && !value.equalsIgnoreCase("UTF8")
&& !allowEncodingChanges) {
close(); // we're screwed now; we can't trust any subsequent string.
throw new PSQLException(GT.tr(
"The server''s client_encoding parameter was changed to {0}. The JDBC driver requires client_encoding to be UTF8 for correct operation.",
value), PSQLState.CONNECTION_FAILURE);
}

if (name.equals("DateStyle") && !value.startsWith("ISO,")) {
close(); // we're screwed now; we can't trust any subsequent date.
throw new PSQLException(GT.tr(
"The server''s DateStyle parameter was changed to {0}. The JDBC driver requires DateStyle to begin with ISO for correct operation.",
value), PSQLState.CONNECTION_FAILURE);
}

if (name.equals("standard_conforming_strings")) {
if (value.equals("on")) {
setStandardConformingStrings(true);
} else if (value.equals("off")) {
setStandardConformingStrings(false);
} else {
close();
// we're screwed now; we don't know how to escape string literals
throw new PSQLException(GT.tr(
"The server''s standard_conforming_strings parameter was reported as {0}. The JDBC driver expected on or off.",
value), PSQLState.CONNECTION_FAILURE);
}
return;
}


if ("TimeZone".equals(name)) {
setTimeZone(TimestampUtils.parseBackendTimeZone(value));
} else if ("application_name".equals(name)) {
setApplicationName(value);
} else if ("server_version_num".equals(name)) {
setServerVersionNum(Integer.parseInt(value));
} else if ("server_version".equals(name)) {
setServerVersion(value);
} else if ("client_encoding".equals(name)) {
if (!"UTF8".equals(value)) {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
pgStream.setEncoding(Encoding.getDatabaseEncoding("UTF8"));
} else if ("standard_conforming_strings".equals(name)) {
if ("on".equals(value)) {
setStandardConformingStrings(true);
} else if ("off".equals(value)) {
setStandardConformingStrings(false);
} else {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
} else if ("integer_datetimes".equals(name)) {
if ("on".equals(value)) {
setIntegerDateTimes(true);
} else if ("off".equals(value)) {
setIntegerDateTimes(false);
} else {
throw new PSQLException(GT.tr("Protocol error. Session setup failed."),
PSQLState.PROTOCOL_VIOLATION);
}
}
}


public void setTimeZone(TimeZone timeZone) { public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone; this.timeZone = timeZone;
Expand Down

0 comments on commit a94cfea

Please sign in to comment.