com.alibaba.druid.sql.ast.statement.SQLInsertStatement Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLInsertStatement. 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 File: InsertHandler.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handle(DumpFileContext context, SQLStatement sqlStatement) throws InterruptedException {
    MySqlInsertStatement insert = (MySqlInsertStatement) sqlStatement;
    SQLInsertStatement.ValuesClause valueClause;

    valuesHandler.preProcess(context);
    for (int i = 0; i < insert.getValuesList().size(); i++) {
        valueClause = insert.getValuesList().get(i);
        try {
            processIncrementColumn(context, valueClause.getValues());
            valuesHandler.process(context, valueClause.getValues(), i == 0);
        } catch (SQLNonTransientException e) {
            context.addError(e.getMessage());
        }
    }
    valuesHandler.postProcess(context);
}
 
Example #2
Source File: ShardColumnValueUtil.java    From Zebra with Apache License 2.0 6 votes vote down vote up
private static Collection<Object> evalInsert(SQLParsedResult parseResult, String column, List<Object> params,
      boolean isBatchInsert) {
	MySqlInsertStatement stmt = (MySqlInsertStatement) parseResult.getStmt();

	List<SQLExpr> columns = stmt.getColumns();
	List<SQLInsertStatement.ValuesClause> valuesList = stmt.getValuesList();

	if (isBatchInsert) {
		List<Object> evalList = new LinkedList<Object>();
		parseBatchValueList(evalList, params, columns, valuesList, column);
		return evalList;
	} else {
		// use the first value in the values
		Set<Object> evalSet = new LinkedHashSet<Object>();
		parseValueList(evalSet, params, columns, valuesList, column);
		return evalSet;
	}
}
 
Example #3
Source File: ShardColumnValueUtil.java    From Zebra with Apache License 2.0 6 votes vote down vote up
private static void parseValueList(Set<Object> evalSet, List<Object> params, List<SQLExpr> columns,
      List<SQLInsertStatement.ValuesClause> valuesList, String column) {
	SQLInsertStatement.ValuesClause values = valuesList.get(0);
	for (int i = 0; i < columns.size(); i++) {
		SQLName columnObj = (SQLName) columns.get(i);
		if (evalColumn(columnObj.getSimpleName(), column)) {
			SQLExpr sqlExpr = values.getValues().get(i);
			if (sqlExpr instanceof SQLVariantRefExpr) {
				SQLVariantRefExpr ref = (SQLVariantRefExpr) sqlExpr;
				evalSet.add(params.get(ref.getIndex()));
			} else if (sqlExpr instanceof SQLValuableExpr) {
				evalSet.add(((SQLValuableExpr) sqlExpr).getValue());
			}
			break;
		}
	}
}
 
Example #4
Source File: InsertParser.java    From dts with Apache License 2.0 5 votes vote down vote up
@Override
public TableDataInfo getPresentValue(List<Object> sqlParamsList, MySqlInsertStatement parseSqlStatement,
    StatementAdapter statementAdapter, TableMetaInfo tableMetaInfo) throws SQLException {
    TableDataInfo txcTable = new TableDataInfo();
    txcTable.setTableName(parseSqlStatement.getTableName().getSimpleName());
    List<TxcLine> line = txcTable.getLine();
    List<SQLInsertStatement.ValuesClause> valuesList = parseSqlStatement.getValuesList();
    List<SQLExpr> columns = parseSqlStatement.getColumns();
    for (SQLInsertStatement.ValuesClause valuesClause : valuesList) {
        List<SQLExpr> values = valuesClause.getValues();
        TxcLine txcLine = new TxcLine();
        for (int i = 0; i < columns.size(); i++) {
            TxcField txcField = new TxcField();
            String columnName = SQLUtils.toSQLString(columns.get(i)).replace("\'", "").replace("`", "").trim();
            txcField.setName(columnName);
            if (sqlParamsList != null && !sqlParamsList.isEmpty()) {
                if (columnName.equalsIgnoreCase(tableMetaInfo.getAutoIncrementPrimaryKey())) {
                    sqlParamsList.add(i, getAutoIncrementPrimaryKeyValue(statementAdapter.getStatement()));
                }
                txcField.setValue(sqlParamsList.get(i));
            } else {
                txcField.setValue(SQLUtils.toSQLString(values.get(i)));
            }
            txcField.setJdkValue(SerializeUtils.serialize(txcField.getValue()));
            txcLine.getFields().add(txcField);
        }
        line.add(txcLine);
    }
    return txcTable;
}
 
Example #5
Source File: SqlParser.java    From es_data_export with Apache License 2.0 5 votes vote down vote up
public static boolean isInsertSql(String sql){
	MySqlStatementParser parser = new MySqlStatementParser(sql);
	SQLStatement statement = parser.parseStatement();
	if(statement instanceof SQLInsertStatement){
		SQLInsertStatement insert =(SQLInsertStatement) statement; 
		tableName = insert.getTableName().toString();
		return true;
	}else if(statement instanceof SQLUpdateStatement){
		SQLUpdateStatement update =(SQLUpdateStatement) statement; 
		tableName = update.getTableName().toString();
		return true;
	}
	return false;
}
 
Example #6
Source File: ShardColumnValueUtil.java    From Zebra with Apache License 2.0 5 votes vote down vote up
private static Set<Object> evalReplace(SQLParsedResult parseResult, String column, List<Object> params) {
	Set<Object> evalSet = new LinkedHashSet<Object>();
	MySqlReplaceStatement stmt = (MySqlReplaceStatement) parseResult.getStmt();
	List<SQLExpr> columns = stmt.getColumns();
	List<SQLInsertStatement.ValuesClause> valuesList = stmt.getValuesList();

	// use the first value in the values
	parseValueList(evalSet, params, columns, valuesList, column);

	return evalSet;
}