Skip to content

Commit

Permalink
Add oscar parser support
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhaoyus committed Aug 29, 2022
1 parent 423cdf0 commit 64230b8
Show file tree
Hide file tree
Showing 24 changed files with 27 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@
import com.alibaba.druid.sql.dialect.oscar.visitor.OscarASTVisitor;

public interface OscarObject extends SQLObject {

void accept0(OscarASTVisitor visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public abstract class OscarObjectImpl extends SQLObjectImpl implements OscarObject {

public OscarObjectImpl() {

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@
import com.alibaba.druid.sql.dialect.oscar.visitor.OscarASTVisitor;

public class OscarTop extends OscarObjectImpl {

private SQLExpr expr;
private boolean percent;
private boolean withTies;

public OscarTop() {

}

@Override
public void accept0(OscarASTVisitor visitor) {

}

public OscarTop(SQLExpr expr) {
Expand Down Expand Up @@ -69,7 +66,6 @@ public void setWithTies(boolean withTies) {
this.withTies = withTies;
}


public OscarTop clone() {
OscarTop x = new OscarTop();
if (expr != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
import com.alibaba.druid.sql.dialect.oscar.ast.OscarObject;

public interface OscarExpr extends SQLExpr, OscarObject {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class OscarAlterSchemaStatement extends SQLStatementImpl implements OscarStatement, SQLAlterStatement {

private SQLIdentifierExpr schemaName;
private SQLIdentifierExpr newName;
private SQLIdentifierExpr newOwner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@
*/
package com.alibaba.druid.sql.dialect.oscar.ast.stmt;


public class OscarAlterTableAlterColumnSetNotNull {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class OscarCreateSchemaStatement extends SQLStatementImpl implements OscarStatement, SQLCreateStatement {

private SQLIdentifierExpr schemaName;
private SQLIdentifierExpr userName;
private boolean ifNotExists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class OscarDeleteStatement extends SQLDeleteStatement implements OscarStatement {

private boolean returning;

public OscarDeleteStatement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class OscarDropSchemaStatement extends SQLStatementImpl implements OscarStatement, SQLDropStatement {

private SQLIdentifierExpr schemaName;
private boolean ifExists;
private boolean cascade;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
import java.util.List;

public class OscarFunctionTableSource extends SQLExprTableSource implements OscarObject {

private final List<SQLParameter> parameters = new ArrayList<SQLParameter>();

public OscarFunctionTableSource() {

}

public OscarFunctionTableSource(SQLExpr expr) {
Expand All @@ -59,7 +57,6 @@ public void accept0(OscarASTVisitor visitor) {

@Override
public PGFunctionTableSource clone() {

PGFunctionTableSource x = new PGFunctionTableSource();

x.setAlias(this.alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import java.util.List;

public class OscarInsertStatement extends SQLInsertStatement implements OscarStatement {


private List<ValuesClause> valuesList = new ArrayList<ValuesClause>();
private SQLExpr returning;
private boolean defaultValues;
Expand Down Expand Up @@ -68,7 +66,6 @@ public void setReturning(SQLExpr returning) {
this.returning = returning;
}


public ValuesClause getValues() {
if (valuesList.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.List;

public class OscarSelectQueryBlock extends SQLSelectQueryBlock implements OscarObject {

private List<SQLExpr> distinctOn = new ArrayList<SQLExpr>(2);

private OscarTop top;
Expand All @@ -39,7 +38,6 @@ public class OscarSelectQueryBlock extends SQLSelectQueryBlock implements OscarO
private IntoOptionTemp intoOptionTemp;
private IntoOptionLocal intoOptionLocal;


@Override
public void accept0(OscarASTVisitor visitor) {
if (visitor.visit(this)) {
Expand Down Expand Up @@ -121,7 +119,6 @@ public void setDistinctOn(List<SQLExpr> distinctOn) {
}

public static class FetchClause extends OscarObjectImpl {

public static enum Option {
FIRST, NEXT
}
Expand Down Expand Up @@ -166,14 +163,11 @@ public OscarTop getTop() {
return top;
}


public void setTop(int rowCount) {
setTop(new OscarTop(new SQLIntegerExpr(rowCount)));
}


public static class ForClause extends OscarObjectImpl {

public static enum Option {
UPDATE, SHARE
}
Expand Down Expand Up @@ -223,7 +217,4 @@ public void accept0(OscarASTVisitor visitor) {
visitor.endVisit(this);
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class OscarSelectStatement extends SQLSelectStatement implements OscarStatement {

public OscarSelectStatement() {
super(DbType.oscar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class OscarShowStatement extends SQLStatementImpl implements OscarStatement, SQLShowStatement {

private SQLExpr expr;

public SQLExpr getExpr() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
import com.alibaba.druid.sql.dialect.oscar.ast.OscarObject;

public interface OscarStatement extends SQLStatement, OscarObject {

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class OscarUpdateStatement extends SQLUpdateStatement implements OscarStatement {

private boolean only;

public OscarUpdateStatement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.alibaba.druid.sql.parser.*;

public class OscarCreateTableParser extends SQLCreateTableParser {

public OscarCreateTableParser(Lexer lexer) {
super(new OscarExprParser(lexer));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Arrays;

public class OscarExprParser extends SQLExprParser {

public static final String[] AGGREGATE_FUNCTIONS;

public static final long[] AGGREGATE_FUNCTIONS_CODES;
Expand Down Expand Up @@ -65,7 +64,7 @@ public OscarExprParser(Lexer lexer) {
this.aggregateFunctionHashCodes = AGGREGATE_FUNCTIONS_CODES;
this.dbType = DbType.oscar;
}

@Override
public SQLDataType parseDataType() {
if (lexer.token() == Token.TYPE) {
Expand All @@ -85,7 +84,7 @@ protected SQLDataType parseDataTypeRest(SQLDataType dataType) {

return dataType;
}

public OscarSelectParser createSelectParser() {
return new OscarSelectParser(this);
}
Expand Down Expand Up @@ -147,7 +146,7 @@ public SQLExpr primary() {
.select());
return queryExpr;
}

return super.primary();
}

Expand Down Expand Up @@ -237,15 +236,15 @@ public SQLExpr primaryRest(SQLExpr expr) {
if (lexer.token() == Token.COLONCOLON) {
lexer.nextToken();
SQLDataType dataType = this.parseDataType();

PGTypeCastExpr castExpr = new PGTypeCastExpr();

castExpr.setExpr(expr);
castExpr.setDataType(dataType);

return primaryRest(castExpr);
}

if (lexer.token() == Token.LBRACKET) {
SQLArrayExpr array = new SQLArrayExpr();
array.setExpr(expr);
Expand All @@ -254,7 +253,7 @@ public SQLExpr primaryRest(SQLExpr expr) {
accept(Token.RBRACKET);
return primaryRest(array);
}

if (expr.getClass() == SQLIdentifierExpr.class) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
String ident = identifierExpr.getName();
Expand Down Expand Up @@ -295,7 +294,6 @@ public SQLExpr primaryRest(SQLExpr expr) {
accept(Token.LITERAL_CHARS);
}


return primaryRest(timestamp);
} else if (FnvHash.Constants.TIMESTAMPTZ == hash) {
if (lexer.token() != Token.LITERAL_ALIAS //
Expand All @@ -322,26 +320,25 @@ public SQLExpr primaryRest(SQLExpr expr) {
accept(Token.LITERAL_CHARS);
}


return primaryRest(timestamp);
} else if (FnvHash.Constants.EXTRACT == hash) {
accept(Token.LPAREN);

PGExtractExpr extract = new PGExtractExpr();

String fieldName = lexer.stringVal();
PGDateField field = PGDateField.valueOf(fieldName.toUpperCase());
lexer.nextToken();

extract.setField(field);

accept(Token.FROM);
SQLExpr source = this.expr();

extract.setSource(source);

accept(Token.RPAREN);

return primaryRest(extract);
} else if (FnvHash.Constants.POINT == hash) {
switch (lexer.token()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static com.alibaba.druid.sql.parser.Token.LITERAL_CHARS;

public class OscarLexer extends Lexer {

public static final Keywords DEFAULT_OSCAR_KEYWORDS;

static {
Expand Down Expand Up @@ -58,10 +57,10 @@ public class OscarLexer extends Lexer {
map.put("SHARE", Token.SHARE);
map.put("SHOW", Token.SHOW);
map.put("START", Token.START);

map.put("USING", Token.USING);
map.put("WINDOW", Token.WINDOW);

map.put("TRUE", Token.TRUE);
map.put("FALSE", Token.FALSE);
map.put("ARRAY", Token.ARRAY);
Expand Down Expand Up @@ -93,7 +92,7 @@ public OscarLexer(String input, SQLParserFeature... features) {
config(feature, true);
}
}

protected void scanString() {
mark = pos;
boolean hasSpecial = false;
Expand Down Expand Up @@ -184,7 +183,7 @@ protected void scanString() {
stringVal = new String(buf, 0, bufPos);
}
}

public void scanSharp() {
scanChar();
if (ch == '>') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.List;

public class OscarSelectParser extends SQLSelectParser {

public OscarSelectParser(SQLExprParser exprParser) {
super(exprParser);
}
Expand Down Expand Up @@ -270,7 +269,7 @@ public SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
if (alias != null) {
functionTableSource.setAlias(alias);
}

lexer.nextToken();
parserParameters(functionTableSource.getParameters());
accept(Token.RPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.Set;

public class OscarOutputVisitor extends SQLASTOutputVisitor implements OscarASTVisitor, OracleASTVisitor {

public OscarOutputVisitor(Appendable appender) {
super(appender);
this.dbType = DbType.oscar;
Expand Down Expand Up @@ -120,7 +119,6 @@ public boolean visit(ForClause x) {
return false;
}


public boolean visit(OscarSelectQueryBlock x) {
if ((!isParameterized()) && isPrettyFormat() && x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
Expand Down
Loading

0 comments on commit 64230b8

Please sign in to comment.