mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved pg parser, support array datatype
This commit is contained in:
@@ -16,9 +16,11 @@
|
||||
package com.alibaba.druid.sql.dialect.postgresql.parser;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.ast.SQLArrayDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCharacterDataType;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
@@ -72,6 +74,18 @@ public class PGExprParser extends SQLExprParser {
|
||||
}
|
||||
return super.parseDataType();
|
||||
}
|
||||
|
||||
protected SQLDataType parseDataTypeRest(SQLDataType dataType) {
|
||||
dataType = super.parseDataTypeRest(dataType);
|
||||
|
||||
if (lexer.token() == Token.LBRACKET) {
|
||||
lexer.nextToken();
|
||||
accept(Token.RBRACKET);
|
||||
dataType = new SQLArrayDataType(dataType);
|
||||
}
|
||||
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public PGSelectParser createSelectParser() {
|
||||
return new PGSelectParser(this);
|
||||
|
||||
@@ -2506,4 +2506,11 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLArrayDataType x) {
|
||||
x.getComponentType().accept(this);
|
||||
print('[');
|
||||
printAndAccept(x.getArguments(), ", ");
|
||||
print(']');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3836,6 +3836,8 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.ARRAY)) {
|
||||
return parseDataTypeRest(charType);
|
||||
} else if (lexer.token == Token.LBRACKET) {
|
||||
return parseDataTypeRest(charType);
|
||||
}
|
||||
|
||||
return charType;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.alibaba.druid.bvt.sql.postgresql.expr;
|
||||
|
||||
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGMacAddrExpr;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGExprParser;
|
||||
import org.junit.Assert;
|
||||
|
||||
|
||||
public class ArrayTest extends PGTest {
|
||||
public void test_timestamp() throws Exception {
|
||||
String sql = "cast(xx as varchar(12)[])";
|
||||
PGExprParser parser = new PGExprParser(sql);
|
||||
SQLExpr expr =parser.expr();
|
||||
Assert.assertEquals("CAST(xx AS varchar(12)[])", SQLUtils.toPGString(expr));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user