Message-ID: From: "cgm-aw (@cgm-aw)" To: "pgjdbc/pgjdbc" Date: Wed, 16 Apr 2025 06:15:01 +0000 Subject: Re: [pgjdbc/pgjdbc] PR #3606: fix: add support for multiple schemas via PgConnection.setSchema In-Reply-To: References: List-Id: X-GitHub-Author-Login: cgm-aw X-GitHub-Comment-Id: 2046156197 X-GitHub-Comment-Type: review_comment X-GitHub-Commit: 713f0335fa9ef65c4f1193834680216ba01f6dab X-GitHub-Issue: 3606 X-GitHub-Line: 1706 X-GitHub-Path: pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java X-GitHub-Repo: pgjdbc/pgjdbc X-GitHub-Type: review_comment X-GitHub-Url: https://github.com/pgjdbc/pgjdbc/pull/3606#discussion_r2046156197 Content-Type: text/plain; charset=utf-8 (on pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java:1706) `Utils.escapeLiteral` requires a StringBuilder and throws a checked exception, so I kept it "old school" since I didn't find it more readable with streams. I could refactor it like this to use streams: ``` public void setSchema(@Nullable String schema) throws SQLException { checkClosed(); try (Statement stmt = createStatement()) { if (schema == null) { stmt.executeUpdate("SET SESSION search_path TO DEFAULT"); } else { // We allow a space after the comma String encodedSchemas = Arrays.stream(schema.split(", ?")) .map(s -> { try { return Utils.escapeLiteral(new StringBuilder(), s, getStandardConformingStrings()); } catch (SQLException e) { throw new RuntimeException(e); } }).map(sb -> "'" + sb + "'") .collect(Collectors.joining(",")); String sql = "SET SESSION search_path TO " + encodedSchemas; stmt.executeUpdate(sql); LOGGER.log(Level.FINE, " setSchema = {0}", encodedSchemas); } } } ``` Let me know what you prefer.