Java Code Examples for com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: dts   Source File: UpdateParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TableDataInfo getPresentValue(List<Object> sqlParamsList, SQLUpdateStatement parseSqlStatement,
    StatementAdapter statementAdapter, TableMetaInfo tableMetaInfo) throws SQLException {
    TableDataInfo txcTable = new TableDataInfo();
    txcTable.setTableName(parseSqlStatement.getTableName().getSimpleName());
    TxcLine txcLine = new TxcLine();
    List<SQLUpdateSetItem> items = parseSqlStatement.getItems();
    int variantExpr = 0;
    for (int i = 0; i < items.size(); i++) {
        SQLUpdateSetItem sqlUpdateSetItem = items.get(i);
        TxcField txcField = new TxcField();
        String cloumnName =
            SQLUtils.toSQLString(sqlUpdateSetItem.getColumn()).replace("\'", "").replace("`", "").trim();
        txcField.setName(cloumnName);
        if (sqlUpdateSetItem.getValue() instanceof SQLVariantRefExpr) {
            txcField.setValue(sqlParamsList.get(variantExpr++));
        } else if (sqlUpdateSetItem.getValue() instanceof SQLValuableExpr) {
            txcField.setValue(SQLUtils.toSQLString(items.get(i).getValue()));
        } else {
            throw new UnsupportedOperationException(
                String.format("Do not support complex sql,%s", sqlUpdateSetItem.getClass().toString()));
        }
        txcField.setJdkValue(SerializeUtils.serialize(txcField.getValue()));
        txcLine.getFields().add(txcField);
    }
    txcTable.getLine().add(txcLine);
    return txcTable;
}
 
Example 2
Source Project: dts   Source File: UpdateParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected List<Object> getWhereParams(List<Object> sqlParamsList, SQLUpdateStatement parseSqlStatement) {
    if (sqlParamsList != null && !sqlParamsList.isEmpty()) {
        int size = 0;
        for (SQLUpdateSetItem sqlUpdateSetItem : parseSqlStatement.getItems()) {
            if (sqlUpdateSetItem.getValue() instanceof SQLVariantRefExpr) {
                size++;
            }
        }
        return Lists.newArrayList(sqlParamsList.subList(size, sqlParamsList.size()));
    }
    return Lists.newArrayList();
}
 
Example 3
Source Project: dts   Source File: UpdateParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected String selectSql(SQLUpdateStatement mySqlUpdateStatement, Set<String> primaryKeyNameSet) {
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("SELECT ");
    List<SQLUpdateSetItem> items = mySqlUpdateStatement.getItems();
    for (SQLUpdateSetItem sqlUpdateSetItem : items) {
        stringBuffer.append(SQLUtils.toSQLString(sqlUpdateSetItem.getColumn())).append(",");
    }
    stringBuffer.append(String.join(",", primaryKeyNameSet));
    stringBuffer.append(" from ").append(mySqlUpdateStatement.getTableName().getSimpleName()).append(" where ");
    stringBuffer.append(SQLUtils.toSQLString(mySqlUpdateStatement.getWhere()));
    return stringBuffer.toString();
}
 
Example 4
Source Project: baymax   Source File: MySqlUpdateParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 判断某个列是否能被update,分区表的分区列是不能被update的.
 */
protected void checkUpdateColumn(){
    MySqlUpdateStatement update = (MySqlUpdateStatement)statement;
    String tableName = StringUtil.removeBackquote(update.getTableName().getSimpleName());
    List<SQLUpdateSetItem> updateSetItem = update.getItems();
    String[] partitionColumns = BaymaxContext.getPartitionColumns(tableName);
    if(partitionColumns != null && partitionColumns.length > 0 && updateSetItem != null && updateSetItem.size() > 0) {
        for(SQLUpdateSetItem item : updateSetItem) {
            String column = StringUtil.removeBackquote(item.getColumn().toString());
            if (StringUtil.contains(partitionColumns, column)){
                throw new BayMaxException("分区表的分区键不能被更新:" + tableName + "." + column);
            }
        }
    }
}
 
Example 5
Source Project: elasticsearch-sql   Source File: ElasticSqlSelectParser.java    License: Apache License 2.0 4 votes vote down vote up
protected MySqlUpdateStatement parseUpdateStatment() {
    MySqlUpdateStatement update = new MySqlUpdateStatement();

    lexer.nextToken();

    if (lexer.identifierEquals(FnvHash.Constants.LOW_PRIORITY)) {
        lexer.nextToken();
        update.setLowPriority(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.IGNORE)) {
        lexer.nextToken();
        update.setIgnore(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.COMMIT_ON_SUCCESS)) {
        lexer.nextToken();
        update.setCommitOnSuccess(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.ROLLBACK_ON_FAIL)) {
        lexer.nextToken();
        update.setRollBackOnFail(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.QUEUE_ON_PK)) {
        lexer.nextToken();
        update.setQueryOnPk(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.TARGET_AFFECT_ROW)) {
        lexer.nextToken();
        SQLExpr targetAffectRow = this.exprParser.expr();
        update.setTargetAffectRow(targetAffectRow);
    }

    if (lexer.identifierEquals(FnvHash.Constants.FORCE)) {
        lexer.nextToken();

        if (lexer.token() == Token.ALL) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONS");
            update.setForceAllPartitions(true);
        } else if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)){
            lexer.nextToken();
            update.setForceAllPartitions(true);
        } else if (lexer.token() == Token.PARTITION) {
            lexer.nextToken();
            SQLName partition = this.exprParser.name();
            update.setForcePartition(partition);
        } else {
            throw new ParserException("TODO. " + lexer.info());
        }
    }

    while (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(update.getHints());
    }

    SQLSelectParser selectParser = this.exprParser.createSelectParser();
    SQLTableSource updateTableSource = selectParser.parseTableSource();
    update.setTableSource(updateTableSource);

    accept(Token.SET);

    for (;;) {
        SQLUpdateSetItem item = this.exprParser.parseUpdateSetItem();
        update.addItem(item);

        if (lexer.token() != Token.COMMA) {
            break;
        }

        lexer.nextToken();
    }

    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        update.setWhere(this.exprParser.expr());
    }

    update.setOrderBy(this.exprParser.parseOrderBy());
    update.setLimit(this.exprParser.parseLimit());

    return update;
}