use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.
the class MySqlCreateTableParser method partitionClauseRest.
protected void partitionClauseRest(SQLPartitionBy clause) {
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
SQLIntegerExpr countExpr = this.exprParser.integerExpr();
clause.setPartitionsCount(countExpr);
}
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
if (identifierEquals("NUM")) {
lexer.nextToken();
}
clause.setPartitionsCount(this.exprParser.expr());
clause.putAttribute("ads.partition", Boolean.TRUE);
}
if (identifierEquals("SUBPARTITION")) {
lexer.nextToken();
accept(Token.BY);
SQLSubPartitionBy subPartitionByClause = null;
boolean linear = false;
if (identifierEquals("LINEAR")) {
lexer.nextToken();
linear = true;
}
if (lexer.token() == Token.KEY) {
MySqlSubPartitionByKey subPartitionKey = new MySqlSubPartitionByKey();
lexer.nextToken();
if (linear) {
clause.setLinear(true);
}
accept(Token.LPAREN);
for (; ; ) {
subPartitionKey.addColumn(this.exprParser.name());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
subPartitionByClause = subPartitionKey;
} else if (identifierEquals("HASH")) {
lexer.nextToken();
SQLSubPartitionByHash subPartitionHash = new SQLSubPartitionByHash();
if (linear) {
clause.setLinear(true);
}
if (lexer.token() == Token.KEY) {
lexer.nextToken();
subPartitionHash.setKey(true);
}
accept(Token.LPAREN);
subPartitionHash.setExpr(this.exprParser.expr());
accept(Token.RPAREN);
subPartitionByClause = subPartitionHash;
} else if (identifierEquals("LIST")) {
lexer.nextToken();
MySqlSubPartitionByList subPartitionList = new MySqlSubPartitionByList();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLExpr expr = this.exprParser.expr();
if (expr instanceof SQLIdentifierExpr && (identifierEquals("bigint") || identifierEquals("long"))) {
String dataType = lexer.stringVal();
lexer.nextToken();
SQLColumnDefinition column = this.exprParser.createColumnDefinition();
column.setName((SQLIdentifierExpr) expr);
column.setDataType(new SQLDataTypeImpl(dataType));
subPartitionList.addColumn(column);
subPartitionList.putAttribute("ads.subPartitionList", Boolean.TRUE);
} else {
subPartitionList.setExpr(expr);
}
accept(Token.RPAREN);
} else {
acceptIdentifier("COLUMNS");
accept(Token.LPAREN);
for (; ; ) {
subPartitionList.addColumn(this.exprParser.parseColumn());
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
subPartitionByClause = subPartitionList;
}
if (identifierEquals("SUBPARTITION")) {
lexer.nextToken();
acceptIdentifier("OPTIONS");
accept(Token.LPAREN);
SQLAssignItem option = this.exprParser.parseAssignItem();
accept(Token.RPAREN);
option.setParent(subPartitionByClause);
subPartitionByClause.getOptions().add(option);
}
if (identifierEquals("SUBPARTITIONS")) {
lexer.nextToken();
Number intValue = lexer.integerValue();
SQLNumberExpr numExpr = new SQLNumberExpr(intValue);
subPartitionByClause.setSubPartitionsCount(numExpr);
lexer.nextToken();
}
if (subPartitionByClause != null) {
subPartitionByClause.setLinear(linear);
clause.setSubPartitionBy(subPartitionByClause);
}
}
}
use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.
the class MySqlStatementParser method parserParameters.
/**
* parse create procedure parameters
*
* @param parameters
*/
private void parserParameters(List<SQLParameter> parameters) {
if (lexer.token() == Token.RPAREN) {
return;
}
for (; ; ) {
SQLParameter parameter = new SQLParameter();
if (lexer.token() == Token.CURSOR) {
lexer.nextToken();
parameter.setName(this.exprParser.name());
accept(Token.IS);
SQLSelect select = this.createSQLSelectParser().select();
SQLDataTypeImpl dataType = new SQLDataTypeImpl();
dataType.setName("CURSOR");
parameter.setDataType(dataType);
parameter.setDefaultValue(new SQLQueryExpr(select));
} else if (lexer.token() == Token.IN || lexer.token() == Token.OUT || lexer.token() == Token.INOUT) {
if (lexer.token() == Token.IN) {
parameter.setParamType(ParameterType.IN);
} else if (lexer.token() == Token.OUT) {
parameter.setParamType(ParameterType.OUT);
} else if (lexer.token() == Token.INOUT) {
parameter.setParamType(ParameterType.INOUT);
}
lexer.nextToken();
parameter.setName(this.exprParser.name());
parameter.setDataType(this.exprParser.parseDataType());
} else {
// default parameter type is in
parameter.setParamType(ParameterType.DEFAULT);
parameter.setName(this.exprParser.name());
parameter.setDataType(this.exprParser.parseDataType());
if (lexer.token() == Token.COLONEQ) {
lexer.nextToken();
parameter.setDefaultValue(this.exprParser.expr());
}
}
parameters.add(parameter);
if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) {
lexer.nextToken();
}
if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) {
continue;
}
break;
}
}
use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.
the class OracleStatementParser method parserParameters.
private void parserParameters(List<SQLParameter> parameters) {
for (; ; ) {
SQLParameter parameter = new SQLParameter();
if (lexer.token() == Token.CURSOR) {
lexer.nextToken();
parameter.setName(this.exprParser.name());
accept(Token.IS);
SQLSelect select = this.createSQLSelectParser().select();
SQLDataTypeImpl dataType = new SQLDataTypeImpl();
dataType.setName("CURSOR");
parameter.setDataType(dataType);
parameter.setDefaultValue(new SQLQueryExpr(select));
} else {
parameter.setName(this.exprParser.name());
parameter.setDataType(this.exprParser.parseDataType());
if (lexer.token() == Token.COLONEQ) {
lexer.nextToken();
parameter.setDefaultValue(this.exprParser.expr());
}
}
parameters.add(parameter);
if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) {
lexer.nextToken();
}
if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) {
continue;
}
break;
}
}
use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.
the class OracleExprParser method parseDataType.
public SQLDataType parseDataType() {
if (lexer.token() == Token.CONSTRAINT || lexer.token() == Token.COMMA) {
return null;
}
if (lexer.token() == Token.DEFAULT || lexer.token() == Token.NOT || lexer.token() == Token.NULL) {
return null;
}
if (lexer.token() == Token.INTERVAL) {
lexer.nextToken();
if (identifierEquals("YEAR")) {
lexer.nextToken();
OracleDataTypeIntervalYear interval = new OracleDataTypeIntervalYear();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
interval.addArgument(this.expr());
accept(Token.RPAREN);
}
accept(Token.TO);
acceptIdentifier("MONTH");
return interval;
} else {
acceptIdentifier("DAY");
OracleDataTypeIntervalDay interval = new OracleDataTypeIntervalDay();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
interval.addArgument(this.expr());
accept(Token.RPAREN);
}
accept(Token.TO);
acceptIdentifier("SECOND");
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
interval.getFractionalSeconds().add(this.expr());
accept(Token.RPAREN);
}
return interval;
}
}
String typeName;
if (identifierEquals("LONG")) {
lexer.nextToken();
acceptIdentifier("RAW");
typeName = "LONG RAW";
} else {
SQLName typeExpr = name();
typeName = typeExpr.toString();
}
if ("TIMESTAMP".equalsIgnoreCase(typeName)) {
OracleDataTypeTimestamp timestamp = new OracleDataTypeTimestamp();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
timestamp.addArgument(this.expr());
accept(Token.RPAREN);
}
if (lexer.token() == Token.WITH) {
lexer.nextToken();
if (identifierEquals("LOCAL")) {
lexer.nextToken();
timestamp.setWithLocalTimeZone(true);
} else {
timestamp.setWithTimeZone(true);
}
acceptIdentifier("TIME");
acceptIdentifier("ZONE");
}
return timestamp;
}
if (isCharType(typeName)) {
SQLCharacterDataType charType = new SQLCharacterDataType(typeName);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
charType.addArgument(this.expr());
if (identifierEquals("CHAR")) {
lexer.nextToken();
charType.setCharType(SQLCharacterDataType.CHAR_TYPE_CHAR);
} else if (identifierEquals("BYTE")) {
lexer.nextToken();
charType.setCharType(SQLCharacterDataType.CHAR_TYPE_BYTE);
}
accept(Token.RPAREN);
}
return parseCharTypeRest(charType);
}
if (lexer.token() == Token.PERCENT) {
lexer.nextToken();
if (identifierEquals("TYPE")) {
lexer.nextToken();
typeName += "%TYPE";
} else if (identifierEquals("ROWTYPE")) {
lexer.nextToken();
typeName += "%ROWTYPE";
} else {
throw new ParserException("syntax error : " + lexer.token() + " " + lexer.stringVal());
}
}
SQLDataType dataType = new SQLDataTypeImpl(typeName);
return parseDataTypeRest(dataType);
}
use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.
the class EqualTest_cast method test_exits.
public void test_exits() throws Exception {
String sql = "cast(a as varchar(50))";
String sql_c = "cast(b as varchar(50))";
SQLCastExpr exprA, exprB, exprC;
{
OracleExprParser parser = new OracleExprParser(sql);
exprA = (SQLCastExpr) parser.expr();
}
{
OracleExprParser parser = new OracleExprParser(sql);
exprB = (SQLCastExpr) parser.expr();
}
{
OracleExprParser parser = new OracleExprParser(sql_c);
exprC = (SQLCastExpr) parser.expr();
}
Assert.assertEquals(exprA, exprB);
Assert.assertNotEquals(exprA, exprC);
Assert.assertTrue(exprA.equals(exprA));
Assert.assertFalse(exprA.equals(new Object()));
Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
Assert.assertEquals(new SQLCastExpr(), new SQLCastExpr());
Assert.assertEquals(new SQLCastExpr().hashCode(), new SQLCastExpr().hashCode());
Assert.assertEquals(new SQLDataTypeImpl(), new SQLDataTypeImpl());
Assert.assertEquals(new SQLDataTypeImpl().hashCode(), new SQLDataTypeImpl().hashCode());
}
Aggregations