Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: enhance readability connection of startup params #2705

Merged
merged 5 commits into from
Dec 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@
*/
public class ConnectionFactoryImpl extends ConnectionFactory {

private static class StartupParam {
public String key;
public String value;
Comment on lines +65 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add final


StartupParam(String key, String value) {
this.key = key;
this.value = value;
}

@Override
public String toString() {
return this.key + "=" + this.value;
}

public byte[] getEncodedKey() {
return this.key.getBytes(StandardCharsets.UTF_8);
}

public byte[] getEncodedValue() {
return this.value.getBytes(StandardCharsets.UTF_8);
}
}

private static final Logger LOGGER = Logger.getLogger(ConnectionFactoryImpl.class.getName());
private static final int AUTH_REQ_OK = 0;
private static final int AUTH_REQ_KRB4 = 1;
Expand Down Expand Up @@ -173,7 +196,7 @@ private PGStream tryConnect(Properties info, SocketFactory socketFactory, HostSp
newStream.setNetworkTimeout(socketTimeout * 1000);
}

List<String[]> paramList = getParametersForStartup(user, database, info);
List<StartupParam> paramList = getParametersForStartup(user, database, info);
sendStartupPacket(newStream, paramList);

// Do authentication (until AuthenticationOk).
Expand Down Expand Up @@ -347,41 +370,41 @@ public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, Properties info) t
PSQLState.CONNECTION_UNABLE_TO_CONNECT);
}

private List<String[]> getParametersForStartup(String user, String database, Properties info) {
List<String[]> paramList = new ArrayList<String[]>();
paramList.add(new String[]{"user", user});
paramList.add(new String[]{"database", database});
paramList.add(new String[]{"client_encoding", "UTF8"});
paramList.add(new String[]{"DateStyle", "ISO"});
paramList.add(new String[]{"TimeZone", createPostgresTimeZone()});
private List<StartupParam> getParametersForStartup(String user, String database, Properties info) {
List<StartupParam> paramList = new ArrayList<>();
paramList.add(new StartupParam("user", user));
paramList.add(new StartupParam("database", database));
paramList.add(new StartupParam("client_encoding", "UTF8"));
paramList.add(new StartupParam("DateStyle", "ISO"));
paramList.add(new StartupParam("TimeZone", createPostgresTimeZone()));

Version assumeVersion = ServerVersion.from(PGProperty.ASSUME_MIN_SERVER_VERSION.getOrDefault(info));

if (assumeVersion.getVersionNum() >= ServerVersion.v9_0.getVersionNum()) {
// User is explicitly telling us this is a 9.0+ server so set properties here:
paramList.add(new String[]{"extra_float_digits", "3"});
paramList.add(new StartupParam("extra_float_digits", "3"));
String appName = PGProperty.APPLICATION_NAME.getOrDefault(info);
if (appName != null) {
paramList.add(new String[]{"application_name", appName});
paramList.add(new StartupParam("application_name", appName));
}
} else {
// User has not explicitly told us that this is a 9.0+ server so stick to old default:
paramList.add(new String[]{"extra_float_digits", "2"});
paramList.add(new StartupParam("extra_float_digits", "2"));
}

String replication = PGProperty.REPLICATION.getOrDefault(info);
if (replication != null && assumeVersion.getVersionNum() >= ServerVersion.v9_4.getVersionNum()) {
paramList.add(new String[]{"replication", replication});
paramList.add(new StartupParam("replication", replication));
}

String currentSchema = PGProperty.CURRENT_SCHEMA.getOrDefault(info);
if (currentSchema != null) {
paramList.add(new String[]{"search_path", currentSchema});
paramList.add(new StartupParam("search_path", currentSchema));
}

String options = PGProperty.OPTIONS.getOrDefault(info);
if (options != null) {
paramList.add(new String[]{"options", options});
paramList.add(new StartupParam("options", options));
}

return paramList;
Expand Down Expand Up @@ -603,17 +626,15 @@ private PGStream enableSSL(PGStream pgStream, SslMode sslMode, Properties info,
}
}

private void sendStartupPacket(PGStream pgStream, List<String[]> params)
private void sendStartupPacket(PGStream pgStream, List<StartupParam> params)
throws IOException {
if (LOGGER.isLoggable(Level.FINEST)) {
StringBuilder details = new StringBuilder();
for (int i = 0; i < params.size(); ++i) {
if (i != 0) {
details.append(", ");
}
details.append(params.get(i)[0]);
details.append("=");
details.append(params.get(i)[1]);
details.append(params.get(i).toString());
}
AlaaAttya marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.log(Level.FINEST, " FE=> StartupPacket({0})", details);
}
Expand All @@ -622,8 +643,8 @@ private void sendStartupPacket(PGStream pgStream, List<String[]> params)
int length = 4 + 4;
byte[][] encodedParams = new byte[params.size() * 2][];
for (int i = 0; i < params.size(); ++i) {
encodedParams[i * 2] = params.get(i)[0].getBytes(StandardCharsets.UTF_8);
encodedParams[i * 2 + 1] = params.get(i)[1].getBytes(StandardCharsets.UTF_8);
encodedParams[i * 2] = params.get(i).getEncodedKey();
encodedParams[i * 2 + 1] = params.get(i).getEncodedValue();
length += encodedParams[i * 2].length + 1 + encodedParams[i * 2 + 1].length + 1;
}

Expand Down