improved odps sql formatter

This commit is contained in:
shaojin.wensj
2015-07-20 04:41:57 +08:00
parent 8f51368245
commit a5811a4f3e
38 changed files with 415 additions and 103 deletions
@@ -37,11 +37,14 @@ public interface SQLObject {
Map<String, Object> getAttributesDirect();
void addBeforeComment(String comment);
void addBeforeComment(List<String> comments);
List<String> getBeforeCommentsDirect();
void addAfterComment(String comment);
void addAfterComment(List<String> comments);
List<String> getAfterCommentsDirect();
boolean hasBeforeComment();
@@ -111,6 +111,10 @@ public abstract class SQLObjectImpl implements SQLObject {
@SuppressWarnings("unchecked")
public void addBeforeComment(String comment) {
if (comment == null) {
return;
}
if (attributes == null) {
attributes = new HashMap<String, Object>(1);
}
@@ -124,6 +128,20 @@ public abstract class SQLObjectImpl implements SQLObject {
comments.add(comment);
}
@SuppressWarnings("unchecked")
public void addBeforeComment(List<String> comments) {
if (attributes == null) {
attributes = new HashMap<String, Object>(1);
}
List<String> attrComments = (List<String>) attributes.get("format.before_comment");
if (attrComments == null) {
attributes.put("format.before_comment", comments);
} else {
attrComments.addAll(comments);
}
}
@SuppressWarnings("unchecked")
public List<String> getBeforeCommentsDirect() {
if (attributes == null) {
@@ -148,6 +166,20 @@ public abstract class SQLObjectImpl implements SQLObject {
comments.add(comment);
}
@SuppressWarnings("unchecked")
public void addAfterComment(List<String> comments) {
if (attributes == null) {
attributes = new HashMap<String, Object>(1);
}
List<String> attrComments = (List<String>) attributes.get("format.after_comment");
if (attrComments == null) {
attributes.put("format.after_comment", comments);
} else {
attrComments.addAll(comments);
}
}
@SuppressWarnings("unchecked")
public List<String> getAfterCommentsDirect() {
if (attributes == null) {
@@ -16,5 +16,5 @@
package com.alibaba.druid.sql.ast;
public interface SQLStatement extends SQLObject {
String getDbType();
}
@@ -16,11 +16,11 @@
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLCommentStatement extends SQLObjectImpl implements SQLStatement {
public class SQLCommentStatement extends SQLStatementImpl implements SQLStatement {
public static enum Type {
TABLE, COLUMN
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
private String dbType;
public SQLInsertStatement(){
@@ -85,4 +86,13 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
visitor.endVisit(this);
}
}
@Override
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
}
@@ -18,9 +18,12 @@ package com.alibaba.druid.sql.ast.statement;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLHint;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLSelect extends SQLObjectImpl {
@@ -118,4 +121,16 @@ public class SQLSelect extends SQLObjectImpl {
return true;
}
public String toString() {
SQLObject parent = this.getParent();
if (parent instanceof SQLStatement) {
String dbType = ((SQLStatement) parent).getDbType();
if (dbType != null) {
return SQLUtils.toSQLString(this, dbType);
}
}
return super.toString();
}
}
@@ -21,9 +21,9 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLSelectItem extends SQLObjectImpl {
private SQLExpr expr;
private String alias;
private boolean connectByRoot = false;
protected SQLExpr expr;
protected String alias;
protected boolean connectByRoot = false;
public SQLSelectItem(){