Skip to content

Commit

Permalink
Add PostgreSQL historical Cast And operators about JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro80 committed Feb 17, 2019
1 parent 8d6ef3b commit 7e0c943
Show file tree
Hide file tree
Showing 18 changed files with 3,831 additions and 3,388 deletions.
5 changes: 5 additions & 0 deletions MiniSqlParser/Enums/ExpOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public enum ExpOperator
,RightBitShift
,BitAnd
,BitOr
,GetJsonObj // ->
,GetJsonObjAsText // ->>
,GetJsonPath // #>
,GetJsonPathAsText // #>>
,DelJsonObj // #-
}
}
5 changes: 5 additions & 0 deletions MiniSqlParser/Enums/PredicateOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public enum PredicateOperator
, Equal2
, NotEqual
, NotEqual2
, ContainsJsonValueL // @>
, ContainsJsonValueR // <@
, ExistsJsonValue1 // ?
, ExistsJsonValue2 // ?|
, ExistsJsonValue3 // ?&
}
}
19 changes: 16 additions & 3 deletions MiniSqlParser/Exprs/CastExpr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ namespace MiniSqlParser
{
public class CastExpr : Expr
{
public CastExpr(Expr operand, Identifier typeName) {
this.Comments = new Comments(5);
public CastExpr(Expr operand
, Identifier typeName
, bool isPostgreSqlHistoricalCast=false) {
if(isPostgreSqlHistoricalCast){
this.Comments = new Comments(2);
}else{
this.Comments = new Comments(5);
}
this.Operand = operand;
this.TypeName = typeName;
this.IsPostgreSqlHistoricalCast = isPostgreSqlHistoricalCast;
}

internal CastExpr(Expr operand, Identifier typeName, Comments comments) {
internal CastExpr(Expr operand
, Identifier typeName
, bool isPostgreSqlHistoricalCast
, Comments comments) {
this.Comments = comments;
this.Operand = operand;
this.TypeName = typeName;
this.IsPostgreSqlHistoricalCast = isPostgreSqlHistoricalCast;
}

private Expr _operand;
Expand All @@ -28,6 +39,8 @@ public Expr Operand {

public Identifier TypeName { get; set; }

public bool IsPostgreSqlHistoricalCast { get; private set; }

protected override void AcceptImp(IVisitor visitor) {
visitor.VisitBefore(this);
this.Operand.Accept(visitor);
Expand Down
23 changes: 21 additions & 2 deletions MiniSqlParser/Parser/MakeASTListener_Expr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ public override void ExitBinaryOpExpr(MiniSqlParserParser.BinaryOpExprContext co
op = ExpOperator.BitAnd;
} else if(opType == MiniSqlParserLexer.PIPE) {
op = ExpOperator.BitOr;
} else if(opType == MiniSqlParserLexer.ARROW){
op = ExpOperator.GetJsonObj;
} else if(opType == MiniSqlParserLexer.ARROW2) {
op = ExpOperator.GetJsonObjAsText;
} else if(opType == MiniSqlParserLexer.S_GT) {
op = ExpOperator.GetJsonPath;
} else if(opType == MiniSqlParserLexer.S_GT2) {
op = ExpOperator.GetJsonPathAsText;
} else if(opType == MiniSqlParserLexer.S_MINUS) {
op = ExpOperator.DelJsonObj;
} else {
throw new InvalidEnumArgumentException("Undefined ExpOperator is used"
, (int)opType
Expand Down Expand Up @@ -308,6 +318,16 @@ public override void ExitSubQueryExpr(MiniSqlParserParser.SubQueryExprContext co
_stack.Push(node);
}

public override void ExitPostgreSqlCastExpr(MiniSqlParserParser.PostgreSqlCastExprContext context) {
var comments = this.GetComments(context);
var typeNameComment = this.GetComments(context.type_name()).Last;
comments.Insert(1, typeNameComment);
var typeName = context.type_name().GetText();
var operand = (Expr)_stack.Pop();
var node = new CastExpr(operand, typeName, true, comments);
_stack.Push(node);
}

public override void ExitBitwiseNotExpr(MiniSqlParserParser.BitwiseNotExprContext context) {
var comments = this.GetComments(context);
var operand = (Expr)_stack.Pop();
Expand Down Expand Up @@ -379,8 +399,7 @@ public override void ExitCastExpr(MiniSqlParserParser.CastExprContext context) {
comments.Insert(3, typeNameComment);
var typeName = context.type_name().GetText();
var operand = (Expr)_stack.Pop();
var name = context.K_CAST().GetText();
var node = new CastExpr(operand, typeName, comments);
var node = new CastExpr(operand, typeName, false, comments);
_stack.Push(node);
}

Expand Down
8 changes: 8 additions & 0 deletions MiniSqlParser/Parser/MakeASTListener_Predicate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public override void ExitBinaryOpPredicate(MiniSqlParserParser.BinaryOpPredicate
op = PredicateOperator.Equal2;
} else if(opType == MiniSqlParserLexer.NOT_EQ1) {
op = PredicateOperator.NotEqual2;
} else if(opType == MiniSqlParserLexer.AT_LT) {
op = PredicateOperator.ContainsJsonValueL;
} else if(opType == MiniSqlParserLexer.AT_GT) {
op = PredicateOperator.ContainsJsonValueR;
} else if(opType == MiniSqlParserLexer.QRY_PIPE) {
op = PredicateOperator.ExistsJsonValue2;
} else if(opType == MiniSqlParserLexer.QRY_AMP) {
op = PredicateOperator.ExistsJsonValue3;
} else {
throw new CannotBuildASTException("Undifined PredicateOperator is used");
}
Expand Down
26 changes: 23 additions & 3 deletions MiniSqlParser/Parser/MiniSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,10 @@ predicate
| PLACEHOLDER2 # PhPredicate
| expr op=( '<' | '<=' | '>' | '>=' ) expr # BinaryOpPredicate
| expr op=( '=' | '==' | '!=' | '<>' ) expr # BinaryOpPredicate
| expr K_NOT? op=( K_LIKE | K_ILIKE | K_GLOB | K_MATCH | K_REGEXP )
| expr {IsPostgreSql}? op=( '<@' | '@>' | '?|' | '?&' )
expr # BinaryOpPredicate
| expr K_NOT?
op=( K_LIKE | K_ILIKE | K_GLOB | K_MATCH | K_REGEXP )
expr ( K_ESCAPE expr )? # LikePredicate
| expr K_IS K_NOT? K_NULL # IsNullPredicate
| expr K_IS K_NOT? expr # IsPredicate
Expand Down Expand Up @@ -473,14 +476,20 @@ expr
| literal_value # LiteralExpr
| PLACEHOLDER1 # PhExpr
| PLACEHOLDER2 # PhExpr
| column_name ( {IsOracle}? OUTER_JOIN )? # ColumnExpr
| column_name ( {IsOracle}? OUTER_JOIN )? # ColumnExpr
| '(' query ')' # SubQueryExpr
| expr {IsPostgreSql}? '::' type_name # PostgreSqlCastExpr
| '~' expr # BitwiseNotExpr
| expr {IsOracle || IsSQLite || IsPostgreSql}?
op='||' expr # BinaryOpExpr
| expr op=( '*' | '/' | '%' ) expr # BinaryOpExpr
| expr op=( '+' | '-' ) expr # BinaryOpExpr
| expr op=( '<<' | '>>' | '&' | '|' ) expr # BinaryOpExpr
| expr op=( '<<' | '>>' | '&' | '|' ) expr # BinaryOpExpr
| expr {IsPostgreSql}?
/* '?' expr is recogify with PLACEHOLDER2. */
/* so, currently ignore this expr. */
op=( '->' | '->>' | '#>' | '#>>' | '#-' )
expr # BinaryOpExpr
| substring_function # SubstrFuncExpr
| extract_function # ExtractFuncExpr
| aggregate_function1 # AggregateFuncExpr
Expand Down Expand Up @@ -702,6 +711,7 @@ identifiable_keyword
| K_SECOND
/* | K_SELECT */
/* | K_SET */
| K_SIMILAR
| K_SKIP
| K_SOME
| K_SUM
Expand Down Expand Up @@ -792,6 +802,15 @@ EQ : '==';
NOT_EQ1 : '!=';
NOT_EQ2 : '<>';
OUTER_JOIN : '(+)';
ARROW : '->';
ARROW2 : '->>';
S_GT : '#>';
S_GT2 : '#>>';
S_MINUS : '#-';
AT_GT : '@>';
AT_LT : '<@';
QRY_PIPE : '?|';
QRY_AMP : '?&';


UINTEGER_LITERAL
Expand Down Expand Up @@ -985,6 +1004,7 @@ K_ROWS : R O W S;
K_SECOND : S E C O N D;
K_SELECT : S E L E C T;
K_SET : S E T;
K_SIMILAR : S I M I L A R;
K_SKIP : S K I P;
K_SOME : S O M E;
K_SUM : S U M;
Expand Down
24 changes: 23 additions & 1 deletion MiniSqlParser/Parser/MiniSqlParser.interp

Large diffs are not rendered by default.

Loading

0 comments on commit 7e0c943

Please sign in to comment.