mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for mysql create table parser, for issue #2999
This commit is contained in:
@@ -516,6 +516,16 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
column.setName(name());
|
||||
column.setDataType(parseDataType());
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.GENERATED)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("ALWAYS");
|
||||
accept(Token.AS);
|
||||
accept(Token.LPAREN);
|
||||
SQLExpr expr = this.expr();
|
||||
accept(Token.RPAREN);
|
||||
column.setGeneratedAlawsAs(expr);
|
||||
}
|
||||
|
||||
return parseColumnRest(column);
|
||||
}
|
||||
|
||||
|
||||
@@ -269,6 +269,13 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
dataType.accept(this);
|
||||
}
|
||||
|
||||
SQLExpr generatedAlawsAs = x.getGeneratedAlawsAs();
|
||||
if (generatedAlawsAs != null) {
|
||||
print0(ucase ? " GENERATED ALWAYS AS (" : " generated always as (");
|
||||
printExpr(generatedAlawsAs);
|
||||
print(')');
|
||||
}
|
||||
|
||||
final SQLExpr charsetExpr = x.getCharsetExpr();
|
||||
if (charsetExpr != null) {
|
||||
print0(ucase ? " CHARSET " : " charset ");
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.alibaba.druid.bvt.sql.mysql.createTable;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlCreateTableTest134 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "create table xuhan3(\n" +
|
||||
"id int not null AUTO_INCREMENT primary key, \n" +
|
||||
"name char(40), SimpleDate date, \n" +
|
||||
"SimpleDate_dayofweek tinyint(4) GENERATED ALWAYS AS (dayofweek(SimpleDate)) VIRTUAL, \n" +
|
||||
"KEY SimpleDate_dayofweek (SimpleDate_dayofweek));";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
MySqlCreateTableStatement stmt = (MySqlCreateTableStatement)statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
|
||||
assertEquals("CREATE TABLE xuhan3 (\n" +
|
||||
"\tid int NOT NULL PRIMARY KEY AUTO_INCREMENT,\n" +
|
||||
"\tname char(40),\n" +
|
||||
"\tSimpleDate date,\n" +
|
||||
"\tSimpleDate_dayofweek tinyint(4) GENERATED ALWAYS AS (dayofweek(SimpleDate)),\n" +
|
||||
"\tKEY SimpleDate_dayofweek (SimpleDate_dayofweek)\n" +
|
||||
");", stmt.toString());
|
||||
|
||||
assertEquals("create table xuhan3 (\n" +
|
||||
"\tid int not null primary key auto_increment,\n" +
|
||||
"\tname char(40),\n" +
|
||||
"\tSimpleDate date,\n" +
|
||||
"\tSimpleDate_dayofweek tinyint(4) generated always as (dayofweek(SimpleDate)),\n" +
|
||||
"\tkey SimpleDate_dayofweek (SimpleDate_dayofweek)\n" +
|
||||
");", stmt.toLowerCaseString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user