You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by td...@apache.org on 2019/07/13 01:18:13 UTC
[phoenix] branch master updated: PHOENIX-5368 Convert query
statements in PhoenixDatabaseMetaData to prepared statements (Rajeshbabu
Chintaguntla)
This is an automated email from the ASF dual-hosted git repository.
tdsilva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/master by this push:
new 3c5edd6 PHOENIX-5368 Convert query statements in PhoenixDatabaseMetaData to prepared statements (Rajeshbabu Chintaguntla)
3c5edd6 is described below
commit 3c5edd6fab97ccc09ec9e93b8c839f8a076ca891
Author: Thomas D'Silva <td...@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 d56eaa4..a70dc92 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;
@@ -473,16 +475,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
@@ -498,22 +502,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());
}
}
}
@@ -1018,6 +1026,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" +
@@ -1041,13 +1050,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();
}
@@ -1306,14 +1320,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 + "'");
@@ -1321,8 +1337,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
@@ -1338,6 +1356,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 + "," +
@@ -1346,16 +1365,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
@@ -1429,6 +1453,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);
@@ -1481,12 +1506,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);
@@ -1521,14 +1550,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");
@@ -1536,8 +1569,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
@@ -2056,4 +2090,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;