Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: do not convert ?, ? to $1, $2 when statement.executeQuery(String…
…) is used (#644)

fixes #643
  • Loading branch information
vlsi committed Sep 21, 2016
1 parent fe10f9b commit 08e1a40
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
Expand Up @@ -8,6 +8,7 @@

package org.postgresql.core;

import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.util.LruCache;

import java.sql.SQLException;
Expand Down Expand Up @@ -56,7 +57,7 @@ public CachedQuery create(Object key) throws SQLException {
outParmBeforeFunc = false;
}
boolean isParameterized = key instanceof String || queryKey.isParameterized;
boolean splitStatements = isParameterized;
boolean splitStatements = isParameterized || queryExecutor.getPreferQueryMode().compareTo(PreferQueryMode.EXTENDED) >= 0;

String[] returningColumns;
if (key instanceof QueryWithReturningColumnsKey) {
Expand Down
6 changes: 4 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgStatement.java
Expand Up @@ -271,7 +271,8 @@ public boolean executeWithFlags(String sql, int flags) throws SQLException {

private boolean executeCachedSql(String sql, int flags, String[] columnNames) throws SQLException {
PreferQueryMode preferQueryMode = connection.getPreferQueryMode();
boolean shouldUseParameterized = preferQueryMode.compareTo(PreferQueryMode.EXTENDED) >= 0;
// Simple statements should not replace ?, ? with $1, $2
boolean shouldUseParameterized = false;
QueryExecutor queryExecutor = connection.getQueryExecutor();
Object key = queryExecutor
.createQueryKey(sql, replaceProcessingEnabled, shouldUseParameterized, columnNames);
Expand Down Expand Up @@ -663,7 +664,8 @@ public void addBatch(String p_sql) throws SQLException {
batchParameters = new ArrayList<ParameterList>();
}

boolean shouldUseParameterized = connection.getPreferQueryMode().compareTo(PreferQueryMode.EXTENDED) >= 0;
// Simple statements should not replace ?, ? with $1, $2
boolean shouldUseParameterized = false;
CachedQuery cachedQuery = connection.createQuery(p_sql, replaceProcessingEnabled, shouldUseParameterized);
batchStatements.add(cachedQuery.query);
batchParameters.add(null);
Expand Down
63 changes: 63 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc4/JsonbTest.java
@@ -0,0 +1,63 @@
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2016, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/

package org.postgresql.test.jdbc4;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;

import org.junit.Assume;
import org.junit.Test;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JsonbTest extends BaseTest4 {

public void setUp() throws Exception {
super.setUp();
Assume.assumeTrue("jsonb requires PostgreSQL 9.4+", TestUtil.haveMinimumServerVersion(con, "9.4"));
TestUtil.createTable(con, "jsonbtest", "detail jsonb");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO jsonbtest (detail) VALUES ('{\"a\": 1}')");
stmt.executeUpdate("INSERT INTO jsonbtest (detail) VALUES ('{\"b\": 1}')");
stmt.executeUpdate("INSERT INTO jsonbtest (detail) VALUES ('{\"c\": 1}')");
stmt.close();
}

public void tearDown() throws SQLException {
TestUtil.dropTable(con, "jsonbtest");
super.tearDown();
}

@Test
public void testJsonbNonPreparedStatement() throws SQLException {
Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT count(1) FROM jsonbtest WHERE detail ? 'a' = false;");
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
rs.close();
stmt.close();
}

@Test
public void testJsonbPreparedStatement() throws SQLException {
PreparedStatement stmt = con.prepareStatement("SELECT count(1) FROM jsonbtest WHERE detail ?? 'a' = false;");
ResultSet rs = stmt.executeQuery();
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
rs.close();
stmt.close();
}
}

0 comments on commit 08e1a40

Please sign in to comment.