mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor SQLUtils
This commit is contained in:
@@ -66,29 +66,22 @@ public class SQLUtils {
|
||||
private final static Log LOG = LogFactory.getLog(SQLUtils.class);
|
||||
|
||||
public static String toSQLString(SQLObject sqlObject, String dbType) {
|
||||
if (JdbcUtils.MYSQL.equals(dbType) || //
|
||||
JdbcUtils.MARIADB.equals(dbType) || //
|
||||
JdbcUtils.H2.equals(dbType)) {
|
||||
return toMySqlString(sqlObject);
|
||||
return toSQLString(sqlObject, dbType, null);
|
||||
}
|
||||
|
||||
public static String toSQLString(SQLObject sqlObject, String dbType, FormatOption option) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor visitor = createOutputVisitor(out, dbType);
|
||||
|
||||
if (option == null) {
|
||||
option = DEFAULT_FORMAT_OPTION;
|
||||
}
|
||||
visitor.setUppCase(option.isUppCase());
|
||||
|
||||
sqlObject.accept(visitor);
|
||||
|
||||
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
|
||||
return toOracleString(sqlObject);
|
||||
}
|
||||
|
||||
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
|
||||
return toPGString(sqlObject);
|
||||
}
|
||||
|
||||
if (JdbcUtils.DB2.equals(dbType)) {
|
||||
return toDB2String(sqlObject);
|
||||
}
|
||||
|
||||
if (JdbcUtils.ODPS.equals(dbType)) {
|
||||
return toOdpsString(sqlObject);
|
||||
}
|
||||
|
||||
return toSQLServerString(sqlObject);
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
}
|
||||
|
||||
public static String toSQLString(SQLObject sqlObject) {
|
||||
@@ -100,11 +93,11 @@ public class SQLUtils {
|
||||
}
|
||||
|
||||
public static String toOdpsString(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
sqlObject.accept(new OdpsOutputVisitor(out));
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
return toOdpsString(sqlObject, null);
|
||||
}
|
||||
|
||||
public static String toOdpsString(SQLObject sqlObject, FormatOption option) {
|
||||
return toSQLString(sqlObject, JdbcConstants.ODPS, option);
|
||||
}
|
||||
|
||||
public static String toMySqlString(SQLObject sqlObject) {
|
||||
@@ -112,18 +105,7 @@ public class SQLUtils {
|
||||
}
|
||||
|
||||
public static String toMySqlString(SQLObject sqlObject, FormatOption option) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
|
||||
|
||||
if (option == null) {
|
||||
option = DEFAULT_FORMAT_OPTION;
|
||||
}
|
||||
visitor.setUppCase(option.isUppCase());
|
||||
|
||||
sqlObject.accept(visitor);
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
return toSQLString(sqlObject, JdbcConstants.MYSQL, option);
|
||||
}
|
||||
|
||||
public static SQLExpr toMySqlExpr(String sql) {
|
||||
@@ -163,19 +145,7 @@ public class SQLUtils {
|
||||
}
|
||||
|
||||
public static String toOracleString(SQLObject sqlObject, FormatOption option) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
OracleOutputVisitor visitor = new OracleOutputVisitor(out, false);
|
||||
|
||||
if (option == null) {
|
||||
option = DEFAULT_FORMAT_OPTION;
|
||||
}
|
||||
|
||||
visitor.setUppCase(option.isUppCase());
|
||||
|
||||
sqlObject.accept(visitor);
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
return toSQLString(sqlObject, JdbcConstants.ORACLE, option);
|
||||
}
|
||||
|
||||
public static String toPGString(SQLObject sqlObject) {
|
||||
@@ -183,35 +153,23 @@ public class SQLUtils {
|
||||
}
|
||||
|
||||
public static String toPGString(SQLObject sqlObject, FormatOption option) {
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
PGOutputVisitor visitor = new PGOutputVisitor(out);
|
||||
|
||||
if (option == null) {
|
||||
option = DEFAULT_FORMAT_OPTION;
|
||||
}
|
||||
visitor.setUppCase(option.isUppCase());
|
||||
|
||||
sqlObject.accept(visitor);
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
return toSQLString(sqlObject, JdbcConstants.POSTGRESQL, option);
|
||||
}
|
||||
|
||||
public static String toDB2String(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
sqlObject.accept(new DB2OutputVisitor(out));
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
return toDB2String(sqlObject, null);
|
||||
}
|
||||
|
||||
public static String toDB2String(SQLObject sqlObject, FormatOption option) {
|
||||
return toSQLString(sqlObject, JdbcConstants.DB2, option);
|
||||
}
|
||||
|
||||
public static String toSQLServerString(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
sqlObject.accept(new SQLServerOutputVisitor(out));
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
return toSQLServerString(sqlObject, null);
|
||||
}
|
||||
|
||||
public static String toSQLServerString(SQLObject sqlObject, FormatOption option) {
|
||||
return toSQLString(sqlObject, JdbcConstants.SQL_SERVER, option);
|
||||
}
|
||||
|
||||
public static String formatPGSql(String sql, FormatOption option) {
|
||||
@@ -298,7 +256,11 @@ public class SQLUtils {
|
||||
}
|
||||
|
||||
public static String toSQLString(List<SQLStatement> statementList, String dbType) {
|
||||
return toSQLString(statementList, dbType, null);
|
||||
return toSQLString(statementList, dbType, (List<Object>) null);
|
||||
}
|
||||
|
||||
public static String toSQLString(List<SQLStatement> statementList, String dbType, FormatOption option) {
|
||||
return toSQLString(statementList, dbType, null, option);
|
||||
}
|
||||
|
||||
public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters) {
|
||||
@@ -374,11 +336,16 @@ public class SQLUtils {
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static SQLASTOutputVisitor createOutputVisitor(Appendable out, String dbType) {
|
||||
return createFormatOutputVisitor(out, null, dbType);
|
||||
}
|
||||
|
||||
public static SQLASTOutputVisitor createFormatOutputVisitor(Appendable out, List<SQLStatement> statementList,
|
||||
public static SQLASTOutputVisitor createFormatOutputVisitor(Appendable out, //
|
||||
List<SQLStatement> statementList, //
|
||||
String dbType) {
|
||||
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
|
||||
if (statementList.size() == 1) {
|
||||
if (statementList == null || statementList.size() == 1) {
|
||||
return new OracleOutputVisitor(out, false);
|
||||
} else {
|
||||
return new OracleOutputVisitor(out, true);
|
||||
|
||||
Reference in New Issue
Block a user