mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser for postgresql
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
||||
package com.alibaba.druid.sql.dialect.postgresql.ast.stmt;
|
||||
|
||||
|
||||
public class PGAlterTableAlterColumnSetNotNull {
|
||||
|
||||
}
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.dialect.postgresql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLTimestampExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLUnaryExpr;
|
||||
@@ -216,6 +217,12 @@ public class PGExprParser extends SQLExprParser {
|
||||
PGLineSegmentsExpr lseg = new PGLineSegmentsExpr();
|
||||
lseg.setValue(value);
|
||||
return primaryRest(lseg);
|
||||
} else if (ident.equalsIgnoreCase("b") && lexer.token() == Token.LITERAL_CHARS) {
|
||||
String charValue = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
expr = new SQLBinaryExpr(charValue);
|
||||
|
||||
return primaryRest(expr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+38
-1
@@ -22,6 +22,8 @@ import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
@@ -287,7 +289,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
stmt.setWith(with);
|
||||
return stmt;
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.DELETE) {
|
||||
PGDeleteStatement stmt = this.parseDeleteStatement();
|
||||
stmt.setWith(with);
|
||||
@@ -295,4 +297,39 @@ public class PGSQLStatementParser extends SQLStatementParser {
|
||||
}
|
||||
throw new ParserException("TODO");
|
||||
}
|
||||
|
||||
protected SQLAlterTableAlterColumn parseAlterColumn() {
|
||||
accept(Token.COLUMN);
|
||||
|
||||
SQLColumnDefinition column = this.exprParser.parseColumn();
|
||||
|
||||
SQLAlterTableAlterColumn alterColumn = new SQLAlterTableAlterColumn();
|
||||
alterColumn.setColumn(column);
|
||||
|
||||
if (column.getDataType() == null && column.getConstraints().size() == 0) {
|
||||
if (lexer.token() == Token.SET) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.NOT) {
|
||||
lexer.nextToken();
|
||||
accept(Token.NULL);
|
||||
alterColumn.setSetNotNull(true);
|
||||
} else {
|
||||
accept(Token.DEFAULT);
|
||||
SQLExpr defaultValue = this.exprParser.expr();
|
||||
alterColumn.setSetDefault(defaultValue);
|
||||
}
|
||||
} else if (lexer.token() == Token.DROP) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.NOT) {
|
||||
lexer.nextToken();
|
||||
accept(Token.NULL);
|
||||
alterColumn.setDropNotNull(true);
|
||||
} else {
|
||||
accept(Token.DEFAULT);
|
||||
alterColumn.setDropDefault(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
return alterColumn;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
@@ -144,4 +145,9 @@ public class PGEvalVisitor extends PGASTVisitorAdapter implements SQLEvalVisitor
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryExpr x) {
|
||||
return SQLEvalVisitorUtils.visit(this, x);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.postgresql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause;
|
||||
@@ -645,4 +646,13 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
public void endVisit(PGLineSegmentsExpr x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryExpr x) {
|
||||
print("B'");
|
||||
print(x.getValue());
|
||||
print('\'');
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user