Skip to content

Commit

Permalink
PHOENIX-5368 Convert query statements in PhoenixDatabaseMetaData to p…
Browse files Browse the repository at this point in the history
…repared statements (Rajeshbabu Chintaguntla)
  • Loading branch information
twdsilva committed Jul 13, 2019
1 parent bd9c23b commit a36efb1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
Expand All @@ -27,6 +28,7 @@
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;
Expand Down Expand Up @@ -455,16 +457,18 @@ public String getCatalogTerm() throws SQLException {

@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
Expand All @@ -480,22 +484,26 @@ public ResultSet getColumnPrivileges(String catalog, String schema, String table

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());
}
}
}
Expand Down Expand Up @@ -999,6 +1007,7 @@ public ResultSet getIndexInfo(String catalog, String schema, String table, boole
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" +
Expand All @@ -1022,13 +1031,18 @@ public ResultSet getIndexInfo(String catalog, String schema, String table, boole
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();
}


Expand Down Expand Up @@ -1287,23 +1301,27 @@ public ResultSet getSchemas() throws SQLException {

@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 + "'");
}

// 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
Expand All @@ -1319,6 +1337,7 @@ public String getStringFunctions() throws SQLException {
@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 + "," +
Expand All @@ -1327,16 +1346,21 @@ public ResultSet getSuperTables(String catalog, String schemaPattern, String tab
" 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
Expand Down Expand Up @@ -1410,6 +1434,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
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);
Expand Down Expand Up @@ -1462,12 +1487,16 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
" 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);
Expand Down Expand Up @@ -1502,23 +1531,28 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
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");
buf.append(whereClause);
}
}
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
Expand Down Expand Up @@ -2037,4 +2071,12 @@ public ResultSet getPseudoColumns(String catalog, String schemaPattern, String t
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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public PhoenixPreparedStatement(PhoenixConnection connection, String query) thro
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;
Expand Down

0 comments on commit a36efb1

Please sign in to comment.