Skip to content

Commit

Permalink
improved sql parser. for issue #1780
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 13, 2017
1 parent 4a88007 commit 66f3ffc
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 142 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/alibaba/druid/sql/ast/SQLDataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public interface SQLDataType extends SQLObject {
void setName(String name);

List<SQLExpr> getArguments();

Boolean getWithTimeZone();
void setWithTimeZone(Boolean value);

boolean isWithLocalTimeZone();
}
45 changes: 31 additions & 14 deletions src/main/java/com/alibaba/druid/sql/ast/SQLDataTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class SQLDataTypeImpl extends SQLObjectImpl implements SQLDataType {

protected String name;
protected final List<SQLExpr> arguments = new ArrayList<SQLExpr>();
private Boolean withTimeZone;

private boolean withLocalTimeZone = false;

public SQLDataTypeImpl(){

Expand Down Expand Up @@ -62,26 +65,40 @@ public void addArgument(SQLExpr argument) {
this.arguments.add(argument);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SQLDataTypeImpl dataType = (SQLDataTypeImpl) o;

if (name != null ? !name.equals(dataType.name) : dataType.name != null) return false;
if (arguments != null ? !arguments.equals(dataType.arguments) : dataType.arguments != null) return false;
return withTimeZone != null ? withTimeZone.equals(dataType.withTimeZone) : dataType.withTimeZone == null;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + arguments.hashCode();
result = prime * result + ((name == null) ? 0 : name.hashCode());
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (arguments != null ? arguments.hashCode() : 0);
result = 31 * result + (withTimeZone != null ? withTimeZone.hashCode() : 0);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
SQLDataTypeImpl other = (SQLDataTypeImpl) obj;
if (!arguments.equals(other.arguments)) return false;
if (name == null) {
if (other.name != null) return false;
} else if (!name.equals(other.name)) return false;
return true;
public Boolean getWithTimeZone() {
return withTimeZone;
}

public void setWithTimeZone(Boolean withTimeZone) {
this.withTimeZone = withTimeZone;
}

public boolean isWithLocalTimeZone() {
return withLocalTimeZone;
}

public void setWithLocalTimeZone(boolean withLocalTimeZone) {
this.withLocalTimeZone = withLocalTimeZone;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleSegmentAttributes;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleLobStorageClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleStorageClause;
Expand Down Expand Up @@ -200,7 +199,7 @@ public SQLDataType parseDataType(boolean restrict) {
}

if ("TIMESTAMP".equalsIgnoreCase(typeName)) {
OracleDataTypeTimestamp timestamp = new OracleDataTypeTimestamp();
SQLDataTypeImpl timestamp = new SQLDataTypeImpl(typeName);

if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
Expand All @@ -214,9 +213,9 @@ public SQLDataType parseDataType(boolean restrict) {
if (identifierEquals("LOCAL")) {
lexer.nextToken();
timestamp.setWithLocalTimeZone(true);
} else {
timestamp.setWithTimeZone(true);
}

timestamp.setWithTimeZone(true);

acceptIdentifier("TIME");
acceptIdentifier("ZONE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleLobStorageClause;
Expand Down Expand Up @@ -369,10 +368,6 @@ public interface OracleASTVisitor extends SQLASTVisitor {

void endVisit(OracleDropDbLinkStatement x);

boolean visit(OracleDataTypeTimestamp x);

void endVisit(OracleDataTypeTimestamp x);

boolean visit(OracleDataTypeIntervalYear x);

void endVisit(OracleDataTypeIntervalYear x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.alibaba.druid.sql.ast.statement.SQLMergeStatement.MergeUpdateClause;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.CellAssignment;
Expand Down Expand Up @@ -912,16 +911,6 @@ public void endVisit(OracleDropDbLinkStatement x) {

}

@Override
public boolean visit(OracleDataTypeTimestamp x) {
return true;
}

@Override
public void endVisit(OracleDataTypeTimestamp x) {

}

@Override
public boolean visit(OracleDataTypeIntervalYear x) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.CellAssignment;
Expand Down Expand Up @@ -2748,29 +2747,6 @@ public boolean visit(SQLCharacterDataType x) {
return false;
}

@Override
public boolean visit(OracleDataTypeTimestamp x) {
print0(x.getName());
if (x.getArguments().size() > 0) {
print('(');
x.getArguments().get(0).accept(this);
print(')');
}

if (x.isWithTimeZone()) {
print0(ucase ? " WITH TIME ZONE" : " with time zone");
} else if (x.isWithLocalTimeZone()) {
print0(ucase ? " WITH LOCAL TIME ZONE" : " with local time zone");
}

return false;
}

@Override
public void endVisit(OracleDataTypeTimestamp x) {

}

@Override
public boolean visit(OracleDataTypeIntervalYear x) {
print0(x.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.alibaba.druid.sql.ast.statement.SQLMergeStatement.MergeUpdateClause;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.CellAssignment;
Expand Down Expand Up @@ -1256,16 +1255,6 @@ public void endVisit(OracleDropDbLinkStatement x) {

}

@Override
public boolean visit(OracleDataTypeTimestamp x) {
return false;
}

@Override
public void endVisit(OracleDataTypeTimestamp x) {

}

@Override
public boolean visit(OracleDataTypeIntervalYear x) {
return false;
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1844,8 +1844,10 @@ && identifierEquals("byte") //
typeName += ' ' + lexer.stringVal();
lexer.nextToken();
}

SQLDataType dataType = new SQLDataTypeImpl(typeName);


return parseDataTypeRest(dataType);
}

Expand All @@ -1861,6 +1863,20 @@ protected SQLDataType parseDataTypeRest(SQLDataType dataType) {
dataType.setName("DOUBLE PRECISION");
}

if ("timestamp".equalsIgnoreCase(dataType.getName())) {
if (identifierEquals("WITHOUT")) {
lexer.nextToken();
acceptIdentifier("TIME");
acceptIdentifier("ZONE");
dataType.setWithTimeZone(false);
} else if (lexer.token() == Token.WITH) {
lexer.nextToken();
acceptIdentifier("TIME");
acceptIdentifier("ZONE");
dataType.setWithTimeZone(true);
}
}

return dataType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,19 @@ protected void printDataType(SQLDataType x) {
printAndAccept(x.getArguments(), ", ");
print(')');
}

Boolean withTimeZone = x.getWithTimeZone();
if (withTimeZone != null) {
if (withTimeZone) {
if (x.isWithLocalTimeZone()) {
print0(ucase ? " WITH LOCAL TIME ZONE" : " with local time zone");
} else {
print0(ucase ? " WITH TIME ZONE" : " with time zone");
}
} else {
print0(ucase ? " WITHOUT TIME ZONE" : " without time zone");
}
}
}

public boolean visit(SQLCharacterDataType x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public void test_types() throws Exception {
SQLStatement statement = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());
assertEquals(1, statementList.size());

Assert.assertEquals("CREATE TABLE T (" + //
assertEquals("CREATE TABLE T (" + //
"\n\tF1 TIMESTAMP," + //
"\n\tF2 TIMESTAMP(9)," + //
"\n\tF3 TIMESTAMP WITH TIME ZONE," + //
Expand All @@ -65,15 +65,15 @@ public void test_types() throws Exception {
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());

Assert.assertEquals(1, visitor.getTables().size());
assertEquals(1, visitor.getTables().size());

Assert.assertEquals(6, visitor.getColumns().size());
assertEquals(6, visitor.getColumns().size());

Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F1")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F2")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F3")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F4")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F5")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F6")));
assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F1")));
assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F2")));
assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F3")));
assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F4")));
assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F5")));
assertTrue(visitor.getColumns().contains(new TableStat.Column("T", "F6")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.alibaba.druid.sql.ast.statement.SQLMergeStatement.MergeUpdateClause;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.CellAssignment;
Expand Down Expand Up @@ -152,7 +151,6 @@ public void test_adapter() throws Exception {
new SQLDropSequenceStatement().accept(adapter);
new OracleDataTypeIntervalDay().accept(adapter);
new OracleDataTypeIntervalYear().accept(adapter);
new OracleDataTypeTimestamp().accept(adapter);
new OracleDropDbLinkStatement().accept(adapter);
new OracleCreateDatabaseDbLinkStatement().accept(adapter);
new SQLCreateProcedureStatement().accept(adapter);
Expand Down
Loading

0 comments on commit 66f3ffc

Please sign in to comment.