Java Code Examples for com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause. 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: Mycat2   Source File: BatchInsertSequence.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public void route(SystemConfig sysConfig, SchemaConfig schema, int sqlType,
		String realSQL, String charset, ServerConnection sc,
		LayerCachePool cachePool) {
	int rs = ServerParse.parse(realSQL);
	this.sqltype = rs & 0xff;
	this.sysConfig=sysConfig; 
	this.schema=schema;
	this.charset=charset; 
	this.sc=sc;	
	this.cachePool=cachePool;	
	
	try {
		MySqlStatementParser parser = new MySqlStatementParser(realSQL);	 
		SQLStatement statement = parser.parseStatement();
		MySqlInsertStatement insert = (MySqlInsertStatement)statement;
		if(insert.getValuesList()!=null){
			String tableName = StringUtil.getTableName(realSQL).toUpperCase();
			TableConfig tableConfig = schema.getTables().get(tableName);
			String primaryKey = tableConfig.getPrimaryKey();//获得表的主键字段
			
			SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
			sqlIdentifierExpr.setName(primaryKey);
			insert.getColumns().add(sqlIdentifierExpr);
			
			if(sequenceHandler == null){
				int seqHandlerType = MycatServer.getInstance().getConfig().getSystem().getSequenceHandlerType();
				switch(seqHandlerType){
					case SystemConfig.SEQUENCEHANDLER_MYSQLDB:
						sequenceHandler = IncrSequenceMySQLHandler.getInstance();
						break;
					case SystemConfig.SEQUENCEHANDLER_LOCALFILE:
						sequenceHandler = IncrSequencePropHandler.getInstance();
						break;
					case SystemConfig.SEQUENCEHANDLER_LOCAL_TIME:
						sequenceHandler = IncrSequenceTimeHandler.getInstance();
						break;
					case SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED:
						sequenceHandler = DistributedSequenceHandler.getInstance(MycatServer.getInstance().getConfig().getSystem());
						break;
					case SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT:
						sequenceHandler = IncrSequenceZKHandler.getInstance();
						break;
					default:
						throw new java.lang.IllegalArgumentException("Invalid sequnce handler type "+seqHandlerType);
				}
			}
			
			for(ValuesClause vc : insert.getValuesList()){
				SQLIntegerExpr sqlIntegerExpr = new SQLIntegerExpr();
				long value = sequenceHandler.nextId(tableName.toUpperCase());
				sqlIntegerExpr.setNumber(value);//插入生成的sequence值
				vc.addValue(sqlIntegerExpr);
			}
			
			String insertSql = insert.toString();
			this.executeSql = insertSql;
		}
		
	} catch (Exception e) {
		LOGGER.error("BatchInsertSequence.route(......)",e);
	}
}
 
Example 2
Source Project: dble   Source File: DruidInsertParser.java    License: GNU General Public License v2.0 4 votes vote down vote up
private String convertInsertSQL(SchemaInfo schemaInfo, MySqlInsertStatement insert, String originSql, TableConfig tc) throws SQLNonTransientException {

        TableMeta orgTbMeta = ProxyMeta.getInstance().getTmManager().getSyncTableMeta(schemaInfo.getSchema(), schemaInfo.getTable());
        if (orgTbMeta == null)
            return originSql;

        boolean isAutoIncrement = tc.isAutoIncrement();

        StringBuilder sb = new StringBuilder(200);
        sb.append("insert ");
        if (insert.isIgnore()) {
            sb.append("ignore ");
        }
        sb.append("into `");
        sb.append(schemaInfo.getTable());
        sb.append("`");

        List<SQLExpr> columns = insert.getColumns();

        int autoIncrement = -1;
        int colSize;
        // insert without columns :insert into t values(xxx,xxx)
        if (columns == null || columns.size() <= 0) {
            if (isAutoIncrement) {
                autoIncrement = getIncrementKeyIndex(schemaInfo, tc.getIncrementColumn());
            }
            colSize = orgTbMeta.getColumns().size();
        } else {
            genColumnNames(tc, isAutoIncrement, sb, columns);
            colSize = columns.size();
            if (isAutoIncrement) {
                getIncrementKeyIndex(schemaInfo, tc.getIncrementColumn());
                autoIncrement = columns.size();
                sb.append(",").append("`").append(tc.getIncrementColumn()).append("`");
                colSize++;
            }
            sb.append(")");
        }

        sb.append(" values");
        String tableKey = StringUtil.getFullName(schemaInfo.getSchema(), schemaInfo.getTable());
        List<ValuesClause> vcl = insert.getValuesList();
        if (vcl != null && vcl.size() > 1) { // batch insert
            for (int j = 0; j < vcl.size(); j++) {
                if (j != vcl.size() - 1)
                    appendValues(tableKey, vcl.get(j).getValues(), sb, autoIncrement, colSize).append(",");
                else
                    appendValues(tableKey, vcl.get(j).getValues(), sb, autoIncrement, colSize);
            }
        } else {
            List<SQLExpr> values = insert.getValues().getValues();
            appendValues(tableKey, values, sb, autoIncrement, colSize);
        }

        List<SQLExpr> dku = insert.getDuplicateKeyUpdate();
        if (dku != null && dku.size() > 0) {
            genDuplicate(sb, dku);
        }
        return RouterUtil.removeSchema(sb.toString(), schemaInfo.getSchema());
    }