Search in sources :

Example 6 with HintNode

use of org.apache.phoenix.parse.HintNode in project phoenix by apache.

the class QueryUtil method constructUpsertStatement.

/**
 * Generate an upsert statement based on a list of {@code ColumnInfo}s with parameter markers. The list of
 * {@code ColumnInfo}s must contain at least one element.
 *
 * @param tableName name of the table for which the upsert statement is to be created
 * @param columns list of columns to be included in the upsert statement
 * @param hint hint to be added to the UPSERT statement.
 * @return the created {@code UPSERT} statement
 */
public static String constructUpsertStatement(String tableName, List<String> columns, Hint hint) {
    if (columns.isEmpty()) {
        throw new IllegalArgumentException("At least one column must be provided for upserts");
    }
    String hintStr = "";
    if (hint != null) {
        final HintNode node = new HintNode(hint.name());
        hintStr = node.toString();
    }
    List<String> parameterList = Lists.newArrayList();
    for (int i = 0; i < columns.size(); i++) {
        parameterList.add("?");
    }
    return String.format("UPSERT %s INTO %s (%s) VALUES (%s)", hintStr, tableName, Joiner.on(", ").join(Iterables.transform(columns, new Function<String, String>() {

        @Nullable
        @Override
        public String apply(@Nullable String columnName) {
            return getEscapedFullColumnName(columnName);
        }
    })), Joiner.on(", ").join(parameterList));
}
Also used : HintNode(org.apache.phoenix.parse.HintNode) Hint(org.apache.phoenix.parse.HintNode.Hint) Nullable(javax.annotation.Nullable)

Aggregations

HintNode (org.apache.phoenix.parse.HintNode)6 Hint (org.apache.phoenix.parse.HintNode.Hint)4 ParseNode (org.apache.phoenix.parse.ParseNode)4 SelectStatement (org.apache.phoenix.parse.SelectStatement)4 NamedTableNode (org.apache.phoenix.parse.NamedTableNode)3 PColumn (org.apache.phoenix.schema.PColumn)3 PTable (org.apache.phoenix.schema.PTable)3 TableRef (org.apache.phoenix.schema.TableRef)3 List (java.util.List)2 Scan (org.apache.hadoop.hbase.client.Scan)2 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)2 AggregatePlan (org.apache.phoenix.execute.AggregatePlan)2 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)2 QueryOptimizer (org.apache.phoenix.optimize.QueryOptimizer)2 AliasedNode (org.apache.phoenix.parse.AliasedNode)2 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)2 ConnectionQueryServices (org.apache.phoenix.query.ConnectionQueryServices)2 DelegateColumn (org.apache.phoenix.schema.DelegateColumn)2 ReadOnlyTableException (org.apache.phoenix.schema.ReadOnlyTableException)2 ArrayList (java.util.ArrayList)1