Skip to content
Permalink
Browse files

refactor: remove autoCommit argument from QueryExecutor#createSimpleQ…

…uery/createParameterizedQuery

Autocommit was not used there, thus it was removed to simplify the code.
  • Loading branch information
vlsi committed Aug 10, 2016
1 parent 5d7a9bb commit 756363efb5e09fc97d819e7367862b0d0c93e56b
@@ -35,12 +35,11 @@
* in single quote literals
* @param withParameters whether to replace ?, ? with $1, $2, etc
* @param splitStatements whether to split statements by semicolon
* @param isAutoCommit whether autocommit is enabled
* @param isBatchedReWriteConfigured whether re-write optimization is enabled
* @return list of native queries
*/
public static List<NativeQuery> parseJdbcSql(String query, boolean standardConformingStrings,
boolean withParameters, boolean splitStatements, boolean isAutoCommit,
boolean withParameters, boolean splitStatements,
boolean isBatchedReWriteConfigured) {
if (!withParameters && !splitStatements) {
return Collections.singletonList(new NativeQuery(query,
@@ -110,7 +110,7 @@
* Execute a Query, passing results to a provided ResultHandler.
*
* @param query the query to execute; must be a query returned from calling
* {@link #createSimpleQuery(String, boolean)} or {@link #createParameterizedQuery(String, boolean)} on this
* {@link #createSimpleQuery(String)} or {@link #createParameterizedQuery(String)} on this
* QueryExecutor object.
* @param parameters the parameters for the query. Must be non-<code>null</code> if the query
* takes parameters. Must be a parameter object returned by
@@ -129,7 +129,7 @@ void execute(Query query, ParameterList parameters, ResultHandler handler, int m
* Execute several Query, passing results to a provided ResultHandler.
*
* @param queries the queries to execute; each must be a query returned from calling
* {@link #createSimpleQuery(String, boolean)} or {@link #createParameterizedQuery(String, boolean)}
* {@link #createSimpleQuery(String)} or {@link #createParameterizedQuery(String)}
* on this QueryExecutor object.
* @param parameterLists the parameter lists for the queries. The parameter lists correspond 1:1
* to the queries passed in the <code>queries</code> array. Each must be non-
@@ -163,10 +163,9 @@ void execute(Query[] queries, ParameterList[] parameterLists, BatchResultHandler
* ParameterList.
*
* @param sql the SQL for the query to create
* @param autocommit indicating when connection has autocommit enabled.
* @return a new Query object
*/
Query createSimpleQuery(String sql, boolean autocommit);
Query createSimpleQuery(String sql);

/**
* Create a parameterized Query object suitable for execution by this QueryExecutor. The provided
@@ -175,10 +174,9 @@ void execute(Query[] queries, ParameterList[] parameterLists, BatchResultHandler
* ParameterList.
*
* @param sql the SQL for the query to create, with '?' placeholders for parameters.
* @param autocommit indicating when connection has autocommit enabled.
* @return a new Query object
*/
Query createParameterizedQuery(String sql, boolean autocommit); // Parsed for parameter placeholders ('?')
Query createParameterizedQuery(String sql); // Parsed for parameter placeholders ('?')

/**
* Prior to attempting to retrieve notifications, we need to pull any recently received
@@ -65,7 +65,7 @@ public void handleCompletion() throws SQLException {
public static byte[][] run(ProtocolConnection protoConnection, String queryString,
boolean wantResults) throws SQLException {
QueryExecutor executor = protoConnection.getQueryExecutor();
Query query = executor.createSimpleQuery(queryString, false);
Query query = executor.createSimpleQuery(queryString);
SimpleResultHandler handler = new SimpleResultHandler();

int flags = QueryExecutor.QUERY_ONESHOT | QueryExecutor.QUERY_SUPPRESS_BEGIN;
@@ -48,11 +48,11 @@ public QueryExecutorImpl(ProtocolConnectionImpl protoConnection, PGStream pgStre
// Query parsing
//

public Query createSimpleQuery(String sql, boolean autocommit) {
public Query createSimpleQuery(String sql) {
return new V2Query(sql, false, protoConnection);
}

public Query createParameterizedQuery(String sql, boolean autocommit) {
public Query createParameterizedQuery(String sql) {
return new V2Query(sql, true, protoConnection);
}

@@ -120,7 +120,7 @@ public void handleCompletion() throws SQLException {

try {
// Create and issue a dummy query to use the existing prefix infrastructure
V2Query query = (V2Query) createSimpleQuery("", false);
V2Query query = (V2Query) createSimpleQuery("");
SimpleParameterList params = (SimpleParameterList) query.createParameterList();
sendQuery(query, params, "BEGIN");
processResults(query, handler, 0, 0);
@@ -29,7 +29,7 @@
useEStringSyntax = pconn.getServerVersionNum() >= 80100;
boolean stdStrings = pconn.getStandardConformingStrings();

List<NativeQuery> queries = Parser.parseJdbcSql(query, stdStrings, withParameters, false, true, false);
List<NativeQuery> queries = Parser.parseJdbcSql(query, stdStrings, withParameters, false, false);
assert queries.size() <= 1 : "Exactly one query expected in V2. " + queries.size()
+ " queries given.";

@@ -137,19 +137,19 @@ boolean hasLock(Object holder) {
// Query parsing
//

public Query createSimpleQuery(String sql, boolean autocommit) {
return parseQuery(sql, false, autocommit);
public Query createSimpleQuery(String sql) {
return parseQuery(sql, false);
}

public Query createParameterizedQuery(String sql, boolean autocommit) {
return parseQuery(sql, true, autocommit);
public Query createParameterizedQuery(String sql) {
return parseQuery(sql, true);
}

private Query parseQuery(String query, boolean withParameters, boolean autocommit) {
private Query parseQuery(String query, boolean withParameters) {

List<NativeQuery> queries = Parser.parseJdbcSql(query,
protoConnection.getStandardConformingStrings(), withParameters, true,
autocommit, allowReWriteBatchedInserts);
allowReWriteBatchedInserts);
if (queries.isEmpty()) {
// Empty query
return EMPTY_QUERY;
@@ -323,8 +323,8 @@ public TimeZone get() {
});

// Initialize common queries.
commitQuery = getQueryExecutor().createSimpleQuery("COMMIT", getAutoCommit());
rollbackQuery = getQueryExecutor().createSimpleQuery("ROLLBACK", getAutoCommit());
commitQuery = getQueryExecutor().createSimpleQuery("COMMIT");
rollbackQuery = getQueryExecutor().createSimpleQuery("ROLLBACK");

int unknownLength = PGProperty.UNKNOWN_LENGTH.getInt(info);

@@ -905,7 +905,7 @@ public void addBatch(String p_sql) throws SQLException {
p_sql = replaceProcessing(p_sql, replaceProcessingEnabled,
connection.getStandardConformingStrings());

batchStatements.add(connection.getQueryExecutor().createSimpleQuery(p_sql, connection.getAutoCommit()));
batchStatements.add(connection.getQueryExecutor().createSimpleQuery(p_sql));
batchParameters.add(null);
}

@@ -70,31 +70,34 @@ public void testMultipleTrailingSemicolons() {

public void testHasReturning() {

List<NativeQuery> queries = Parser.parseJdbcSql("insert into foo (a,b,c) values (?,?,?) RetuRning a", true, true, false, true, true);
List<NativeQuery> queries = Parser.parseJdbcSql("insert into foo (a,b,c) values (?,?,?) RetuRning a", true, true, false,
true);
NativeQuery query = queries.get(0);
assertTrue("The parser should find the word returning", query.command.isReturningKeywordPresent());

queries = Parser.parseJdbcSql("insert into foo (a,b,c) values (?,?,?)", true, true, false, true, true);
queries = Parser.parseJdbcSql("insert into foo (a,b,c) values (?,?,?)", true, true, false, true);
query = queries.get(0);
assertFalse("The parser should not find the word returning", query.command.isReturningKeywordPresent());

queries = Parser.parseJdbcSql("insert into foo (a,b,c) values ('returning',?,?)", true, true, false, true, true);
queries = Parser.parseJdbcSql("insert into foo (a,b,c) values ('returning',?,?)", true, true, false,
true);
query = queries.get(0);
assertFalse("The parser should not find the word returning as it is in quotes ", query.command.isReturningKeywordPresent());

queries = Parser.parseJdbcSql("select 1 as returning", true, true, false, true, true);
queries = Parser.parseJdbcSql("select 1 as returning", true, true, false, true);
query = queries.get(0);
assertFalse("This is not an insert command", query.command.getType() == SqlCommandType.INSERT);
assertTrue("Returning is OK here as it is not an insert command ", query.command.isReturningKeywordPresent());

}

public void testHasDelete() {
List<NativeQuery> queries = Parser.parseJdbcSql("DeLeTe from foo where a=1", true, true, false, true, true);
List<NativeQuery> queries = Parser.parseJdbcSql("DeLeTe from foo where a=1", true, true, false,
true);
NativeQuery query = queries.get(0);
assertTrue("This is a delete command", query.command.getType() == SqlCommandType.DELETE);

queries = Parser.parseJdbcSql("update foo set (a=?,b=?,c=?)", true, true, false, true, true);
queries = Parser.parseJdbcSql("update foo set (a=?,b=?,c=?)", true, true, false, true);
query = queries.get(0);
assertFalse("This is not a delete command", query.command.getType() == SqlCommandType.DELETE);

@@ -103,27 +106,28 @@ public void testHasDelete() {

public void testisMove() {

List<NativeQuery> queries = Parser.parseJdbcSql("MoVe NEXT FROM FOO", true, true, false, true, true);
List<NativeQuery> queries = Parser.parseJdbcSql("MoVe NEXT FROM FOO", true, true, false, true);
NativeQuery query = queries.get(0);
assertTrue("This is a move command", query.command.getType() == SqlCommandType.MOVE);

queries = Parser.parseJdbcSql("update foo set (a=?,b=?,c=?)", true, true, false, true, true);
queries = Parser.parseJdbcSql("update foo set (a=?,b=?,c=?)", true, true, false, true);
query = queries.get(0);
assertFalse("This is not a move command", query.command.getType() == SqlCommandType.MOVE);

}

public void testIsInsert() {

List<NativeQuery> queries = Parser.parseJdbcSql("InSeRt into foo (a,b,c) values (?,?,?) returning a", true, true, false, true, true);
List<NativeQuery> queries = Parser.parseJdbcSql("InSeRt into foo (a,b,c) values (?,?,?) returning a", true, true, false,
true);
NativeQuery query = queries.get(0);
assertTrue("This is an insert command", query.command.getType() == SqlCommandType.INSERT);

queries = Parser.parseJdbcSql("update foo set (a=?,b=?,c=?)", true, true, false, true, true);
queries = Parser.parseJdbcSql("update foo set (a=?,b=?,c=?)", true, true, false, true);
query = queries.get(0);
assertFalse("This is not an insert command", query.command.getType() == SqlCommandType.INSERT);

queries = Parser.parseJdbcSql("select 1 as insert", true, true, false, true, true);
queries = Parser.parseJdbcSql("select 1 as insert", true, true, false, true);
query = queries.get(0);
assertFalse("This is not insert command", query.command.getType() == SqlCommandType.INSERT);
}
@@ -141,7 +145,7 @@ public void testCompositeWithComments() {
private String reparse(String query, boolean standardConformingStrings, boolean withParameters,
boolean splitStatements) {
return toString(
Parser.parseJdbcSql(query, standardConformingStrings, withParameters, splitStatements, true, false));
Parser.parseJdbcSql(query, standardConformingStrings, withParameters, splitStatements, false));
}

private String toString(List<NativeQuery> queries) {

0 comments on commit 756363e

Please sign in to comment.
You can’t perform that action at this time.