use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_10 method test_parseCompleteValues_false.
public void test_parseCompleteValues_false() throws Exception {
String sql = "insert into t(a,b) values ('a1','b1'),('a2','b2'),('a3','b3'),('a4','b4');";
MySqlStatementParser parser = new MySqlStatementParser(sql);
parser.setParseCompleteValues(false);
parser.setParseValuesSize(3);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(3, insertStmt.getValuesList().size());
Assert.assertEquals(2, insertStmt.getValues().getValues().size());
Assert.assertEquals(2, insertStmt.getColumns().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
String formatSql = //
"INSERT INTO t (a, b)" + //
"\nVALUES ('a1', 'b1')," + //
"\n\t('a2', 'b2')," + "\n\t('a3', 'b3')";
Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_12 method test_insert_rollback_on_fail.
public void test_insert_rollback_on_fail() throws Exception {
String sql = "insert into t1 values('A\\0\\0\\0B')";
MySqlStatementParser parser = new MySqlStatementParser(sql);
parser.setParseCompleteValues(false);
parser.setParseValuesSize(3);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(1, insertStmt.getValuesList().size());
Assert.assertEquals(1, insertStmt.getValues().getValues().size());
Assert.assertEquals(0, insertStmt.getColumns().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
String formatSql = "INSERT INTO t1" + "\nVALUES ('A\\0\\0\\0B')";
Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
}
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();
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat-Server by MyCATApache.
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 druid by alibaba.
the class MySqlASTVisitorAdapterTest method test_adapter.
public void test_adapter() throws Exception {
MySqlASTVisitorAdapter adapter = new MySqlASTVisitorAdapter();
new SQLBooleanExpr().accept(adapter);
new SQLLimit().accept(adapter);
new MySqlTableIndex().accept(adapter);
new MySqlKey().accept(adapter);
new MySqlPrimaryKey().accept(adapter);
new MySqlIntervalExpr().accept(adapter);
new SQLBinaryExpr().accept(adapter);
new MySqlPrepareStatement().accept(adapter);
new MySqlExecuteStatement().accept(adapter);
new MysqlDeallocatePrepareStatement().accept(adapter);
new MySqlDeleteStatement().accept(adapter);
new MySqlInsertStatement().accept(adapter);
new MySqlLoadXmlStatement().accept(adapter);
new MySqlReplaceStatement().accept(adapter);
new SQLStartTransactionStatement().accept(adapter);
new MySqlRollbackStatement().accept(adapter);
new MySqlShowColumnsStatement().accept(adapter);
new MySqlShowDatabasesStatement().accept(adapter);
new MySqlShowWarningsStatement().accept(adapter);
new MySqlShowStatusStatement().accept(adapter);
new CobarShowStatus().accept(adapter);
new MySqlKillStatement().accept(adapter);
new MySqlBinlogStatement().accept(adapter);
new MySqlResetStatement().accept(adapter);
new UserSpecification().accept(adapter);
new MySqlPartitionByKey().accept(adapter);
new MySqlOutFileExpr().accept(adapter);
new MySqlUpdateStatement().accept(adapter);
new MySqlSetTransactionStatement().accept(adapter);
new MySqlSetNamesStatement().accept(adapter);
new MySqlShowMasterLogsStatement().accept(adapter);
new MySqlSetCharSetStatement().accept(adapter);
new MySqlShowAuthorsStatement().accept(adapter);
new MySqlShowCollationStatement().accept(adapter);
new MySqlShowBinLogEventsStatement().accept(adapter);
new MySqlShowCharacterSetStatement().accept(adapter);
new MySqlShowContributorsStatement().accept(adapter);
new MySqlShowCreateDatabaseStatement().accept(adapter);
new MySqlShowCreateEventStatement().accept(adapter);
new MySqlShowCreateFunctionStatement().accept(adapter);
new MySqlShowCreateProcedureStatement().accept(adapter);
new MySqlShowCreateTableStatement().accept(adapter);
new MySqlShowCreateTriggerStatement().accept(adapter);
new MySqlShowCreateViewStatement().accept(adapter);
new MySqlShowEngineStatement().accept(adapter);
new MySqlShowEnginesStatement().accept(adapter);
new MySqlShowErrorsStatement().accept(adapter);
new MySqlShowEventsStatement().accept(adapter);
new MySqlShowFunctionCodeStatement().accept(adapter);
new MySqlShowFunctionStatusStatement().accept(adapter);
new MySqlShowGrantsStatement().accept(adapter);
new MySqlUserName().accept(adapter);
new MySqlShowIndexesStatement().accept(adapter);
new MySqlShowKeysStatement().accept(adapter);
new MySqlShowMasterStatusStatement().accept(adapter);
new MySqlShowOpenTablesStatement().accept(adapter);
new MySqlShowBinaryLogsStatement().accept(adapter);
new MySqlShowPluginsStatement().accept(adapter);
new MySqlShowPrivilegesStatement().accept(adapter);
new MySqlShowProcedureCodeStatement().accept(adapter);
new MySqlShowProcedureStatusStatement().accept(adapter);
new MySqlShowProcessListStatement().accept(adapter);
new MySqlShowProfileStatement().accept(adapter);
new MySqlShowSlaveHostsStatement().accept(adapter);
new MySqlShowRelayLogEventsStatement().accept(adapter);
new MySqlShowSlaveStatusStatement().accept(adapter);
new MySqlShowTableStatusStatement().accept(adapter);
new MySqlShowTriggersStatement().accept(adapter);
new MySqlRenameTableStatement().accept(adapter);
new MySqlUnionQuery().accept(adapter);
new MySqlUseIndexHint().accept(adapter);
new MySqlIgnoreIndexHint().accept(adapter);
new MySqlLockTableStatement().accept(adapter);
new MySqlUnlockTablesStatement().accept(adapter);
new MySqlForceIndexHint().accept(adapter);
new MySqlAlterTableChangeColumn().accept(adapter);
new MySqlAlterTableCharacter().accept(adapter);
new MySqlAlterTableOption().accept(adapter);
new MySqlCreateTableStatement().accept(adapter);
new MySqlCharExpr().accept(adapter);
new MySqlUnique().accept(adapter);
new MySqlAlterTableModifyColumn().accept(adapter);
new MySqlAlterTableDiscardTablespace().accept(adapter);
new MySqlAlterTableImportTablespace().accept(adapter);
new TableSpaceOption().accept(adapter);
}
Aggregations