forked from alibaba/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request alibaba#767 from alibaba/revert-766-revert-760-dev
Revert "Revert "INTERVAL 语法支持""
- Loading branch information
Showing
7 changed files
with
146 additions
and
68 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/com/alibaba/druid/sql/dialect/postgresql/ast/expr/PGIntervalExpr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.alibaba.druid.sql.dialect.postgresql.ast.expr; | ||
|
||
import com.alibaba.druid.sql.ast.SQLExpr; | ||
import com.alibaba.druid.sql.ast.SQLExprImpl; | ||
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr; | ||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor; | ||
import com.alibaba.druid.sql.visitor.SQLASTVisitor; | ||
|
||
/** | ||
* Created by tianzhen.wtz on 2014/12/29 0029 16:10. | ||
* 类说明: | ||
*/ | ||
public class PGIntervalExpr extends SQLExprImpl implements SQLLiteralExpr,PGExpr{ | ||
|
||
private SQLExpr value; | ||
|
||
|
||
public SQLExpr getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(SQLExpr value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public void accept0(PGASTVisitor visitor) { | ||
visitor.visit(this); | ||
visitor.endVisit(this); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
PGIntervalExpr that = (PGIntervalExpr) o; | ||
|
||
if (value != null ? !value.equals(that.value) : that.value != null) return false; | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value != null ? value.hashCode() : 0; | ||
} | ||
|
||
@Override | ||
protected void accept0(SQLASTVisitor visitor) { | ||
this.accept0((PGASTVisitor) visitor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/java/com/alibaba/druid/sql/parser/PGIntervalSQLTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.alibaba.druid.sql.parser; | ||
|
||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement; | ||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser; | ||
import junit.framework.TestCase; | ||
import org.junit.Assert; | ||
|
||
/** | ||
* Created by tianzhen.wtz on 2014/12/26 0026 20:44. | ||
* 类说明: | ||
*/ | ||
public class PGIntervalSQLTest extends TestCase{ | ||
|
||
|
||
public void testIntervalSQL(){ | ||
String sql1="select timestamp '2001-09-28 01:00' + interval '23 hours'"; | ||
String sql1Result="SELECT TIMESTAMP '2001-09-28 01:00' + INTERVAL '23 hours'"; | ||
equal(sql1,sql1Result); | ||
|
||
String sql2="select interval '1 day' - interval '1 hour'"; | ||
String sql2Result="SELECT INTERVAL '1 day' - INTERVAL '1 hour'"; | ||
equal(sql2,sql2Result); | ||
|
||
String sql3="select date_part('month', interval '2 years 3 months')"; | ||
String sql3Result="SELECT date_part('month', INTERVAL '2 years 3 months')"; | ||
equal(sql3,sql3Result); | ||
} | ||
|
||
|
||
private void equal(String targetSql,String resultSql){ | ||
PGSQLStatementParser parser=new PGSQLStatementParser(targetSql); | ||
PGSelectStatement statement = parser.parseSelect(); | ||
Assert.assertTrue(statement.toString().equals(resultSql)); | ||
|
||
} | ||
|
||
} |