This is an automated email from the ASF dual-hosted git repository. tdsilva pushed a commit to branch 4.x-HBase-1.3 in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/4.x-HBase-1.3 by this push: new dd341b9 PHOENIX-5368 Convert query statements in PhoenixDatabaseMetaData to prepared statements (Rajeshbabu Chintaguntla) dd341b9 is described below commit dd341b9c5f674e2db6c20a5fb2781c070baaa349 Author: Thomas D'Silva <tdsi...@apache.org> AuthorDate: Fri Jul 12 18:13:38 2019 -0700 PHOENIX-5368 Convert query statements in PhoenixDatabaseMetaData to prepared statements (Rajeshbabu Chintaguntla) --- .../phoenix/jdbc/PhoenixDatabaseMetaData.java | 100 +++++++++++++++------ .../phoenix/jdbc/PhoenixPreparedStatement.java | 2 +- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java index 208cf46..56e052d 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java @@ -19,6 +19,7 @@ package org.apache.phoenix.jdbc; import java.sql.Connection; import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.RowIdLifetime; import java.sql.SQLException; @@ -27,6 +28,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.ArrayList; import java.util.List; import org.apache.hadoop.hbase.Cell; @@ -455,16 +457,18 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { @Override public ResultSet getCatalogs() throws SQLException { + List<String> parameterValues = new ArrayList<String>(4); StringBuilder buf = new StringBuilder("select \n" + " DISTINCT " + TENANT_ID + " " + TABLE_CAT + " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS + " where " + COLUMN_NAME + " is null" + " and " + COLUMN_FAMILY + " is null" + " and " + TENANT_ID + " is not null"); - addTenantIdFilter(buf, null); + addTenantIdFilter(buf, null, parameterValues); buf.append(" order by " + TENANT_ID); - Statement stmt = connection.createStatement(); - return stmt.executeQuery(buf.toString()); + PreparedStatement stmt = connection.prepareStatement(buf.toString()); + setParameters(stmt, parameterValues); + return stmt.executeQuery(); } @Override @@ -480,22 +484,26 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { public static final String GLOBAL_TENANANTS_ONLY = "null"; - private void addTenantIdFilter(StringBuilder buf, String tenantIdPattern) { + private void addTenantIdFilter(StringBuilder buf, String tenantIdPattern, + List<String> parameterValues) { PName tenantId = connection.getTenantId(); if (tenantIdPattern == null) { if (tenantId != null) { appendConjunction(buf); buf.append(" (" + TENANT_ID + " IS NULL " + - " OR " + TENANT_ID + " = '" + StringUtil.escapeStringConstant(tenantId.getString()) + "') "); + " OR " + TENANT_ID + " = ?) "); + parameterValues.add(tenantId.getString()); } } else if (tenantIdPattern.length() == 0) { appendConjunction(buf); buf.append(TENANT_ID + " IS NULL "); } else { appendConjunction(buf); - buf.append(" TENANT_ID LIKE '" + StringUtil.escapeStringConstant(tenantIdPattern) + "' "); + buf.append(" TENANT_ID LIKE ? "); + parameterValues.add(tenantIdPattern); if (tenantId != null) { - buf.append(" and TENANT_ID = '" + StringUtil.escapeStringConstant(tenantId.getString()) + "' "); + buf.append(" and TENANT_ID = ? "); + parameterValues.add(tenantId.getString()); } } } @@ -999,6 +1007,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { if (unique) { // No unique indexes return emptyResultSet; } + List<String> parameterValues = new ArrayList<String>(4); StringBuilder buf = new StringBuilder("select \n" + TENANT_ID + " " + TABLE_CAT + ",\n" + // use this column for column family name TABLE_SCHEM + ",\n" + @@ -1022,13 +1031,18 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { ARRAY_SIZE + "\nfrom " + SYSTEM_CATALOG + "\nwhere "); - buf.append(TABLE_SCHEM + (schema == null || schema.length() == 0 ? " is null" : " = '" + StringUtil.escapeStringConstant(schema) + "'" )); - buf.append("\nand " + DATA_TABLE_NAME + " = '" + StringUtil.escapeStringConstant(table) + "'" ); + buf.append(TABLE_SCHEM + (schema == null || schema.length() == 0 ? " is null" : " = ?" )); + if(schema != null && schema.length() > 0) { + parameterValues.add(schema); + } + buf.append("\nand " + DATA_TABLE_NAME + " = ?" ); + parameterValues.add(table); buf.append("\nand " + COLUMN_NAME + " is not null" ); - addTenantIdFilter(buf, catalog); + addTenantIdFilter(buf, catalog, parameterValues); buf.append("\norder by INDEX_NAME," + ORDINAL_POSITION); - Statement stmt = connection.createStatement(); - return stmt.executeQuery(buf.toString()); + PreparedStatement stmt = connection.prepareStatement(buf.toString()); + setParameters(stmt, parameterValues); + return stmt.executeQuery(); } @@ -1287,14 +1301,16 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { @Override public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException { + List<String> parameterValues = new ArrayList<String>(4); StringBuilder buf = new StringBuilder("select distinct \n" + TABLE_SCHEM + "," + TENANT_ID + " " + TABLE_CATALOG + " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS + " where " + COLUMN_NAME + " is null"); - addTenantIdFilter(buf, catalog); + addTenantIdFilter(buf, catalog, parameterValues); if (schemaPattern != null) { - buf.append(" and " + TABLE_SCHEM + " like '" + StringUtil.escapeStringConstant(schemaPattern) + "'"); + buf.append(" and " + TABLE_SCHEM + " like ?"); + parameterValues.add(schemaPattern); } if (SchemaUtil.isNamespaceMappingEnabled(null, connection.getQueryServices().getProps())) { buf.append(" and " + TABLE_NAME + " = '" + MetaDataClient.EMPTY_TABLE + "'"); @@ -1302,8 +1318,10 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { // TODO: we should union this with SYSTEM.SEQUENCE too, but we only have support for // UNION ALL and we really need UNION so that it dedups. - Statement stmt = connection.createStatement(); - return stmt.executeQuery(buf.toString()); + + PreparedStatement stmt = connection.prepareStatement(buf.toString()); + setParameters(stmt, parameterValues); + return stmt.executeQuery(); } @Override @@ -1319,6 +1337,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { @Override // TODO does this need to change to use the PARENT_TABLE link public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { + List<String> parameterValues = new ArrayList<String>(4); StringBuilder buf = new StringBuilder("select \n" + TENANT_ID + " " + TABLE_CAT + "," + // Use tenantId for catalog TABLE_SCHEM + "," + @@ -1327,16 +1346,21 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS + " where " + COLUMN_NAME + " is null" + " and " + LINK_TYPE + " = " + LinkType.PHYSICAL_TABLE.getSerializedValue()); - addTenantIdFilter(buf, catalog); + addTenantIdFilter(buf, catalog, parameterValues); if (schemaPattern != null) { - buf.append(" and " + TABLE_SCHEM + (schemaPattern.length() == 0 ? " is null" : " like '" + StringUtil.escapeStringConstant(schemaPattern) + "'" )); + buf.append(" and " + TABLE_SCHEM + (schemaPattern.length() == 0 ? " is null" : " like ?" )); + if(schemaPattern.length() > 0) { + parameterValues.add(schemaPattern); + } } if (tableNamePattern != null) { - buf.append(" and " + TABLE_NAME + " like '" + StringUtil.escapeStringConstant(tableNamePattern) + "'" ); + buf.append(" and " + TABLE_NAME + " like ?" ); + parameterValues.add(tableNamePattern); } buf.append(" order by " + TENANT_ID + "," + TABLE_SCHEM + "," +TABLE_NAME + "," + SUPERTABLE_NAME); - Statement stmt = connection.createStatement(); - return stmt.executeQuery(buf.toString()); + PreparedStatement stmt = connection.prepareStatement(buf.toString()); + setParameters(stmt, parameterValues); + return stmt.executeQuery(); } @Override @@ -1410,6 +1434,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { boolean isSequence = false; boolean hasTableTypes = types != null && types.length > 0; StringBuilder typeClauseBuf = new StringBuilder(); + List<String> parameterValues = new ArrayList<String>(4); if (hasTableTypes) { List<String> tableTypes = Lists.newArrayList(types); isSequence = tableTypes.remove(SEQUENCE_TABLE_TYPE); @@ -1462,12 +1487,16 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { " where " + COLUMN_NAME + " is null" + " and " + COLUMN_FAMILY + " is null" + " and " + TABLE_NAME + " != '" + MetaDataClient.EMPTY_TABLE + "'"); - addTenantIdFilter(buf, catalog); + addTenantIdFilter(buf, catalog, parameterValues); if (schemaPattern != null) { - buf.append(" and " + TABLE_SCHEM + (schemaPattern.length() == 0 ? " is null" : " like '" + StringUtil.escapeStringConstant(schemaPattern) + "'" )); + buf.append(" and " + TABLE_SCHEM + (schemaPattern.length() == 0 ? " is null" : " like ?" )); + if(schemaPattern.length() > 0) { + parameterValues.add(schemaPattern); + } } if (tableNamePattern != null) { - buf.append(" and " + TABLE_NAME + " like '" + StringUtil.escapeStringConstant(tableNamePattern) + "'" ); + buf.append(" and " + TABLE_NAME + " like ?" ); + parameterValues.add(tableNamePattern); } if (typeClauseBuf.length() > 0) { buf.append(typeClauseBuf); @@ -1502,14 +1531,18 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { buf.append( " from " + SYSTEM_SEQUENCE + "\n"); StringBuilder whereClause = new StringBuilder(); - addTenantIdFilter(whereClause, catalog); + addTenantIdFilter(whereClause, catalog, parameterValues); if (schemaPattern != null) { appendConjunction(whereClause); - whereClause.append(SEQUENCE_SCHEMA + (schemaPattern.length() == 0 ? " is null" : " like '" + StringUtil.escapeStringConstant(schemaPattern) + "'\n" )); + whereClause.append(SEQUENCE_SCHEMA + (schemaPattern.length() == 0 ? " is null" : " like ?\n" )); + if(schemaPattern.length() > 0) { + parameterValues.add(schemaPattern); + } } if (tableNamePattern != null) { appendConjunction(whereClause); - whereClause.append(SEQUENCE_NAME + " like '" + StringUtil.escapeStringConstant(tableNamePattern) + "'\n" ); + whereClause.append(SEQUENCE_NAME + " like ?\n" ); + parameterValues.add(tableNamePattern); } if (whereClause.length() > 0) { buf.append(" where\n"); @@ -1517,8 +1550,9 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { } } buf.append(" order by 4, 1, 2, 3\n"); - Statement stmt = connection.createStatement(); - return stmt.executeQuery(buf.toString()); + PreparedStatement stmt = connection.prepareStatement(buf.toString()); + setParameters(stmt, parameterValues); + return stmt.executeQuery(); } @Override @@ -2037,4 +2071,12 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData { public boolean generatedKeyAlwaysReturned() throws SQLException { return false; } + + + private void setParameters(PreparedStatement stmt, List<String> parameterValues) + throws SQLException { + for(int i = 0; i < parameterValues.size(); i++) { + stmt.setString(i+1, parameterValues.get(i)); + } + } } diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java index 914ea33..b54efc8 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.java @@ -97,7 +97,7 @@ public class PhoenixPreparedStatement extends PhoenixStatement implements Prepar Collections.fill(parameters, BindManager.UNBOUND_PARAMETER); } - public PhoenixPreparedStatement(PhoenixPreparedStatement statement) throws SQLException { + public PhoenixPreparedStatement(PhoenixPreparedStatement statement) { super(statement.connection); this.query = statement.query; this.statement = statement.statement;