use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_25_time method test_insert.
public void test_insert() throws Exception {
String sql = "INSERT INTO DB1.TB2 (col1, col2, col3) VALUES(1, Timestamp '2019-01-01:12:12:21', '3')";
{
List<Object> outParameters = new ArrayList<Object>();
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, outParameters);
assertEquals("INSERT INTO DB1.TB2(col1, col2, col3)\n" + "VALUES (?, ?, ?)", psql);
assertEquals(3, outParameters.size());
String rsql = ParameterizedOutputVisitorUtils.restore(psql, JdbcConstants.MYSQL, outParameters);
assertEquals("INSERT INTO DB1.TB2 (col1, col2, col3)\n" + "VALUES (1, '2019-01-01:12:12:21', '3')", rsql);
}
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
assertEquals("INSERT INTO DB1.TB2 (col1, col2, col3)\n" + "VALUES (1, TIMESTAMP '2019-01-01:12:12:21', '3')", SQLUtils.toMySqlString(insertStmt));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat_plus by coderczp.
the class ExplainHandler method isMycatSeq.
private static boolean isMycatSeq(String stmt, SchemaConfig schema) {
if (pattern.matcher(stmt).find()) {
return true;
}
SQLStatementParser parser = new MySqlStatementParser(stmt);
MySqlInsertStatement statement = (MySqlInsertStatement) parser.parseStatement();
String tableName = statement.getTableName().getSimpleName();
TableConfig tableConfig = schema.getTables().get(tableName.toUpperCase());
if (tableConfig == null) {
return false;
}
if (tableConfig.isAutoIncrement()) {
boolean isHasIdInSql = false;
String primaryKey = tableConfig.getPrimaryKey();
List<SQLExpr> columns = statement.getColumns();
for (SQLExpr column : columns) {
String columnName = column.toString();
if (primaryKey.equalsIgnoreCase(columnName)) {
isHasIdInSql = true;
break;
}
}
if (!isHasIdInSql) {
return true;
}
}
return false;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat_plus by coderczp.
the class ParseUtil method changeInsertAddSlot.
public static String changeInsertAddSlot(String sql, int slotValue) {
SQLStatementParser parser = new MycatStatementParser(sql);
MySqlInsertStatement insert = (MySqlInsertStatement) parser.parseStatement();
insert.getColumns().add(new SQLIdentifierExpr("_slot"));
insert.getValues().getValues().add(new SQLIntegerExpr(slotValue));
return insert.toString();
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat_plus by coderczp.
the class BatchInsertSequence method route.
@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().getSequnceHandlerType();
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());
// 插入生成的sequence值
sqlIntegerExpr.setNumber(value);
vc.addValue(sqlIntegerExpr);
}
String insertSql = insert.toString();
this.executeSql = insertSql;
}
} catch (Exception e) {
LOGGER.error("BatchInsertSequence.route(......)", e);
}
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat-Server by MyCATApache.
the class ParseUtil method changeInsertAddSlot.
public static String changeInsertAddSlot(String sql, int slotValue) {
SQLStatementParser parser = new MycatStatementParser(sql);
MySqlInsertStatement insert = (MySqlInsertStatement) parser.parseStatement();
insert.getColumns().add(new SQLIdentifierExpr("_slot"));
insert.getValues().getValues().add(new SQLIntegerExpr(slotValue));
return insert.toString();
}
Aggregations