use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat-Server by MyCATApache.
the class DruidMysqlCreateTableTest method isInsertHasSlot.
private boolean isInsertHasSlot(String sql) {
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlInsertStatement insertStatement = (MySqlInsertStatement) parser.parseStatement();
List<SQLExpr> cc = insertStatement.getColumns();
for (SQLExpr sqlExpr : cc) {
SQLIdentifierExpr c = (SQLIdentifierExpr) sqlExpr;
if ("_slot".equalsIgnoreCase(c.getName()) && cc.size() == insertStatement.getValues().getValues().size())
return true;
}
return false;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlStatementParser method parseInsert.
public SQLInsertStatement parseInsert() {
MySqlInsertStatement insertStatement = new MySqlInsertStatement();
if (lexer.token() == Token.INSERT) {
lexer.nextToken();
for (; ; ) {
if (identifierEquals(LOW_PRIORITY)) {
insertStatement.setLowPriority(true);
lexer.nextToken();
continue;
}
if (identifierEquals(DELAYED)) {
insertStatement.setDelayed(true);
lexer.nextToken();
continue;
}
if (identifierEquals("HIGH_PRIORITY")) {
insertStatement.setHighPriority(true);
lexer.nextToken();
continue;
}
if (identifierEquals(IGNORE)) {
insertStatement.setIgnore(true);
lexer.nextToken();
continue;
}
if (identifierEquals("ROLLBACK_ON_FAIL")) {
insertStatement.setRollbackOnFail(true);
lexer.nextToken();
continue;
}
break;
}
if (lexer.token() == Token.INTO) {
lexer.nextToken();
}
SQLName tableName = this.exprParser.name();
insertStatement.setTableName(tableName);
if (lexer.token() == Token.IDENTIFIER && !identifierEquals("VALUE")) {
insertStatement.setAlias(lexer.stringVal());
lexer.nextToken();
}
}
int columnSize = 0;
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
if (lexer.token() == (Token.SELECT)) {
SQLSelect select = this.exprParser.createSelectParser().select();
select.setParent(insertStatement);
insertStatement.setQuery(select);
} else {
this.exprParser.exprList(insertStatement.getColumns(), insertStatement);
columnSize = insertStatement.getColumns().size();
}
accept(Token.RPAREN);
}
if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) {
lexer.nextTokenLParen();
parseValueClause(insertStatement.getValuesList(), columnSize);
} else if (lexer.token() == Token.SET) {
lexer.nextToken();
SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause();
insertStatement.getValuesList().add(values);
for (; ; ) {
SQLName name = this.exprParser.name();
insertStatement.addColumn(name);
if (lexer.token() == Token.EQ) {
lexer.nextToken();
} else {
accept(Token.COLONEQ);
}
values.addValue(this.exprParser.expr());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
} else if (lexer.token() == (Token.SELECT)) {
SQLSelect select = this.exprParser.createSelectParser().select();
select.setParent(insertStatement);
insertStatement.setQuery(select);
} else if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
SQLSelect select = this.exprParser.createSelectParser().select();
select.setParent(insertStatement);
insertStatement.setQuery(select);
accept(Token.RPAREN);
}
if (lexer.token() == Token.ON) {
lexer.nextToken();
acceptIdentifier("DUPLICATE");
accept(Token.KEY);
accept(Token.UPDATE);
exprParser.exprList(insertStatement.getDuplicateKeyUpdate(), insertStatement);
}
return insertStatement;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class WallVisitorUtils method checkInsertForMultiTenant.
private static void checkInsertForMultiTenant(WallVisitor visitor, SQLInsertInto x) {
TenantCallBack tenantCallBack = visitor.getConfig().getTenantCallBack();
String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
if (tenantCallBack == null && (tenantTablePattern == null || tenantTablePattern.length() == 0)) {
return;
}
if (x == null) {
throw new IllegalStateException("x is null");
}
SQLExprTableSource tableSource = x.getTableSource();
String alias = null;
String matchTableName = null;
String tenantColumn = null;
SQLExpr tableExpr = tableSource.getExpr();
if (tableExpr instanceof SQLIdentifierExpr) {
String tableName = ((SQLIdentifierExpr) tableExpr).getName();
if (tenantCallBack != null) {
tenantColumn = tenantCallBack.getTenantColumn(StatementType.INSERT, tableName);
}
if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
tenantColumn = visitor.getConfig().getTenantColumn();
}
if (!StringUtils.isEmpty(tenantColumn)) {
matchTableName = tableName;
alias = tableSource.getAlias();
}
}
if (matchTableName == null) {
return;
}
SQLExpr item = null;
if (alias != null) {
item = new SQLPropertyExpr(new SQLIdentifierExpr(alias), tenantColumn);
} else {
item = new SQLIdentifierExpr(tenantColumn);
}
SQLExpr value = generateTenantValue(visitor, alias, StatementType.INSERT, matchTableName);
// add insert item and value
x.getColumns().add(item);
List<ValuesClause> valuesClauses = null;
ValuesClause valuesClause = null;
if (x instanceof MySqlInsertStatement) {
valuesClauses = ((MySqlInsertStatement) x).getValuesList();
} else if (x instanceof SQLServerInsertStatement) {
valuesClauses = ((MySqlInsertStatement) x).getValuesList();
} else {
valuesClause = x.getValues();
}
if (valuesClauses != null && valuesClauses.size() > 0) {
for (ValuesClause clause : valuesClauses) {
clause.addValue(value);
}
}
if (valuesClause != null) {
valuesClause.addValue(value);
}
// insert .. select
SQLSelect select = x.getQuery();
if (select != null) {
List<SQLSelectQueryBlock> queryBlocks = splitSQLSelectQuery(select.getQuery());
for (SQLSelectQueryBlock queryBlock : queryBlocks) {
queryBlock.getSelectList().add(new SQLSelectItem(value));
}
}
visitor.setSqlModified(true);
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_7 method test_0.
public void test_0() throws Exception {
String sql = "insert into Apply (applicant_id, applytime, applytype, approver_id, end, process, start) values (?, ?, ?, ?, ?, ?, ?)";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(7, insertStmt.getColumns().size());
Assert.assertEquals(1, insertStmt.getValuesList().size());
Assert.assertEquals(7, insertStmt.getValuesList().get(0).getValues().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
Assert.assertEquals(//
"INSERT INTO Apply (applicant_id, applytime, applytype, approver_id, end" + //
"\n\t, process, start)" + //
"\nVALUES (?, ?, ?, ?, ?" + //
"\n\t, ?, ?)", SQLUtils.toMySqlString(insertStmt));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.
the class MySqlInsertTest_8 method test_0.
public void test_0() throws Exception {
String sql = "insert into dd.table1(d,e) select * from bb.table3";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
Assert.assertEquals(2, insertStmt.getColumns().size());
Assert.assertEquals(0, insertStmt.getValuesList().size());
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(//
"INSERT INTO dd.table1 (d, e)" + //
"\nSELECT *" + //
"\nFROM bb.table3", SQLUtils.toMySqlString(insertStmt));
Assert.assertEquals(//
"insert into dd.table1 (d, e)" + //
"\nselect *" + //
"\nfrom bb.table3", SQLUtils.toMySqlString(insertStmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Aggregations