Package com.alibaba.druid.sql
Class SQLUtils
java.lang.Object
com.alibaba.druid.sql.SQLUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classTokenInfo class to hold token, its corresponding string value, and position -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidacceptAggregateFunction(String sql, DbType dbType, Consumer<SQLAggregateExpr> consumer, Predicate<SQLAggregateExpr> filter) static voidacceptBinaryOpExpr(String sql, DbType dbType, Consumer<SQLBinaryOpExpr> consumer, Predicate<SQLBinaryOpExpr> filter) static voidacceptBinaryOpExprGroup(String sql, DbType dbType, Consumer<SQLBinaryOpExprGroup> consumer, Predicate<SQLBinaryOpExprGroup> filter) static voidacceptBooleanOr(String sql, DbType dbType, Consumer<SQLBinaryOpExprGroup> consumer) static voidacceptFunction(String sql, DbType dbType, Consumer<SQLMethodInvokeExpr> consumer, Predicate<SQLMethodInvokeExpr> filter) static voidacceptInsertInto(String sql, DbType dbType, Consumer<SQLInsertInto> consumer, Predicate<SQLInsertInto> filter) static voidacceptSelectQueryBlock(String sql, DbType dbType, Consumer<SQLSelectQueryBlock> consumer, Predicate<SQLSelectQueryBlock> filter) static voidacceptTableSource(String sql, DbType dbType, Consumer<SQLTableSource> consumer, Predicate<SQLTableSource> filter) static voidaddCondition(SQLStatement stmt, SQLBinaryOperator op, SQLExpr condition, boolean left) static StringaddCondition(String sql, String condition, DbType dbType) static StringaddCondition(String sql, String condition, SQLBinaryOperator op, boolean left, DbType dbType) static voidaddSelectItem(SQLStatement stmt, SQLExpr expr, String alias, boolean first) static voidaddSelectItem(SQLSelectQueryBlock queryBlock, SQLExpr expr, String alias, boolean first) static StringaddSelectItem(String selectSql, String expr, String alias, boolean first, DbType dbType) static StringaddSelectItem(String selectSql, String expr, String alias, DbType dbType) static SQLExprbuildCondition(SQLBinaryOperator op, SQLExpr condition, boolean left, SQLExpr where) static StringbuildToDate(String columnName, String tableAlias, String pattern, DbType dbType) static intcalculateTokenLevenshteinDistance(String sql1, DbType dbType1, String sql2, DbType dbType2) Calculate Levenshtein distance between two SQL statements based on their token sequences.static Object[]clearLimit(String query, DbType dbType) static SQLStatementconvertTimeZone(SQLStatement stmt, TimeZone from, TimeZone to) static StringconvertTimeZone(String sql, TimeZone from, TimeZone to) static SQLASTOutputVisitorcreateFormatOutputVisitor(StringBuilder out, List<SQLStatement> statementList, DbType dbType) static SQLASTOutputVisitorcreateOutputVisitor(StringBuilder out, DbType dbType) static SchemaStatVisitorcreateSchemaStatVisitor(DbType dbType) static SchemaStatVisitorcreateSchemaStatVisitor(SchemaRepository repository) static SchemaStatVisitorcreateSchemaStatVisitor(SchemaRepository repository, DbType dbType) static SchemaStatVisitorcreateSchemaStatVisitor(List<SQLStatement> statementList, DbType dbType) Deprecated.static StringdesensitizeTable(String tableName) static StringforcedNormalize(String name, DbType dbType) static Stringstatic Stringformat(String sql, DbType dbType, SQLUtils.FormatOption option) static Stringstatic Stringstatic Stringformat(String sql, DbType dbType, List<Object> parameters, SQLUtils.FormatOption option, SQLParserFeature[] features) static Stringstatic StringformatHive(String sql) static StringformatHive(String sql, SQLUtils.FormatOption option) static StringformatMySql(String sql) static StringformatMySql(String sql, SQLUtils.FormatOption option) static StringformatOdps(String sql) static StringformatOdps(String sql, SQLUtils.FormatOption option) static StringformatOracle(String sql) static StringformatOracle(String sql, SQLUtils.FormatOption option) static StringformatPGSql(String sql, SQLUtils.FormatOption option) static StringformatPresto(String sql) static StringformatPresto(String sql, SQLUtils.FormatOption option) static StringformatSQLServer(String sql) static List<SQLUtils.TokenInfo>getAllTokens(String sql, DbType dbType) Get all tokens from SQL parsing based on dialect type, including their string valuesstatic List<SQLUtils.TokenInfo>getAllTokens(String sql, DbType dbType, boolean keepComments) Get all tokens from SQL parsing based on dialect type, including their string valuesstatic List<SQLUtils.TokenInfo>getAllTokens(String sql, String dbType) Get all tokens from SQL parsing based on dialect type (overloaded method with String dbType)static List<SQLUtils.TokenInfo>getAllTokens(String sql, String dbType, boolean keepComments) Get all tokens from SQL parsing based on dialect type (overloaded method with String dbType)static SQLLimitgetLimit(SQLStatement statement, DbType dbType) static SQLLimitstatic longstatic booleanisQuoteChar(char c) static booleanstatic booleannameEquals(SQLName a, SQLName b) static booleannameEquals(String a, String b) static Stringstatic Stringstatic Stringstatic SQLExprstatic SQLStatementstatic SQLStatementparseSingleStatement(String sql, DbType dbType, boolean keepComments) static SQLStatementparseSingleStatement(String sql, DbType dbType, SQLParserFeature... features) static SQLStatementparseSingleStatement(String sql, String dbType, SQLParserFeature... features) static List<SQLStatement>parseStatements(String sql, DbType dbType) static List<SQLStatement>parseStatements(String sql, DbType dbType, boolean keepComments) static List<SQLStatement>parseStatements(String sql, DbType dbType, SQLParserFeature... features) static List<SQLStatement>parseStatements(String sql, String dbType) static List<SQLStatement>parseStatements(String sql, String dbType, SQLParserFeature... features) static Stringstatic StringremoveQuote(String str) static booleanreplaceInParent(SQLDataType expr, SQLDataType target) static booleanreplaceInParent(SQLExpr expr, SQLExpr target) static booleanreplaceInParent(SQLStatement cmp, SQLStatement dest) static booleanreplaceInParent(SQLSelectQuery cmp, SQLSelectQuery dest) static booleanreplaceInParent(SQLSelect cmp, SQLSelect dest) static booleanreplaceInParent(SQLTableSource cmp, SQLTableSource dest) static String重新排序建表语句,解决建表语句的依赖关系static List<SQLInsertStatement>splitInsertValues(DbType dbType, String insertSql, int size) static StringtoAntsparkString(SQLObject sqlObject) static StringtoAntsparkString(SQLObject sqlObject, SQLUtils.FormatOption option) static StringtoDB2String(SQLObject sqlObject) static StringtoDB2String(SQLObject sqlObject, SQLUtils.FormatOption option) static StringtoHiveString(SQLObject sqlObject) static SQLExprtoMySqlExpr(String sql) static StringtoMySqlString(SQLObject sqlObject) static StringtoMySqlString(SQLObject sqlObject, SQLUtils.FormatOption option) static StringtoMySqlString(SQLObject sqlObject, VisitorFeature... features) static StringtoMySqlStringIfNotNull(SQLObject sqlObject, String defaultStr) static StringtoNormalizeMysqlString(SQLObject sqlObject) static StringtoOdpsString(SQLObject sqlObject) static StringtoOdpsString(SQLObject sqlObject, SQLUtils.FormatOption option) static StringtoOracleString(SQLObject sqlObject) static StringtoOracleString(SQLObject sqlObject, SQLUtils.FormatOption option) static SQLSelectOrderByItemtoOrderByItem(String sql, DbType dbType) static StringtoPGString(SQLObject sqlObject) static StringtoPGString(SQLObject sqlObject, SQLUtils.FormatOption option) static SQLSelectItemtoSelectItem(String sql, DbType dbType) static SQLExprstatic SQLExprstatic SQLExprtoSQLExpr(String sql, DbType dbType, SQLParserFeature... features) static StringtoSQLServerString(SQLObject sqlObject) static StringtoSQLServerString(SQLObject sqlObject, SQLUtils.FormatOption option) static StringtoSQLString(SQLObject obj) static StringtoSQLString(SQLObject sqlObject, DbType dbType) static StringtoSQLString(SQLObject sqlObject, DbType dbType, SQLUtils.FormatOption option) static StringtoSQLString(SQLObject sqlObject, DbType dbType, SQLUtils.FormatOption option, VisitorFeature... features) static StringtoSQLString(SQLObject sqlObject, String dbType) static StringtoSQLString(List<SQLStatement> statementList, DbType dbType) static StringtoSQLString(List<SQLStatement> statementList, DbType dbType, SQLUtils.FormatOption option) static StringtoSQLString(List<SQLStatement> statementList, DbType dbType, SQLASTOutputVisitor visitor) static StringtoSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters) static StringtoSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, SQLUtils.FormatOption option) static StringtoSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, SQLUtils.FormatOption option, Map<String, String> tableMapping) static StringtoSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, Map<String, String> tableMapping, SQLASTOutputVisitor visitor) static List<SQLStatement>toStatementList(String sql, DbType dbType) static SQLUpdateSetItemtoUpdateSetItem(String sql, DbType dbType) static String
-
Field Details
-
DEFAULT_FORMAT_OPTION
-
DEFAULT_LCASE_FORMAT_OPTION
-
-
Constructor Details
-
SQLUtils
public SQLUtils()
-
-
Method Details
-
toSQLString
-
toSQLString
-
toSQLString
-
toSQLString
public static String toSQLString(SQLObject sqlObject, DbType dbType, SQLUtils.FormatOption option, VisitorFeature... features) -
toSQLString
-
toOdpsString
-
toHiveString
-
toOdpsString
-
toAntsparkString
-
toAntsparkString
-
toMySqlString
-
toMySqlStringIfNotNull
-
toMySqlString
-
toNormalizeMysqlString
-
toMySqlString
-
toMySqlExpr
-
formatMySql
-
formatMySql
-
formatOracle
-
formatOracle
-
formatOdps
-
formatPresto
-
formatPresto
-
formatHive
-
formatOdps
-
formatHive
-
formatSQLServer
-
toOracleString
-
toOracleString
-
toPGString
-
toPGString
-
toDB2String
-
toDB2String
-
toSQLServerString
-
toSQLServerString
-
formatPGSql
-
toSQLExpr
-
toSQLExpr
-
toOrderByItem
-
toUpdateSetItem
-
toSelectItem
-
toStatementList
-
toSQLExpr
-
format
-
format
-
format
-
format
-
format
-
format
public static String format(String sql, DbType dbType, List<Object> parameters, SQLUtils.FormatOption option, SQLParserFeature[] features) -
toSQLString
-
toSQLString
public static String toSQLString(List<SQLStatement> statementList, DbType dbType, SQLUtils.FormatOption option) -
toSQLString
-
toSQLString
public static String toSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, SQLUtils.FormatOption option) -
toSQLString
public static String toSQLString(List<SQLStatement> statementList, DbType dbType, SQLASTOutputVisitor visitor) -
toSQLString
public static String toSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, SQLUtils.FormatOption option, Map<String, String> tableMapping) -
toSQLString
public static String toSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, Map<String, String> tableMapping, SQLASTOutputVisitor visitor) -
createOutputVisitor
-
createFormatOutputVisitor
public static SQLASTOutputVisitor createFormatOutputVisitor(StringBuilder out, List<SQLStatement> statementList, DbType dbType) -
createSchemaStatVisitor
@Deprecated public static SchemaStatVisitor createSchemaStatVisitor(List<SQLStatement> statementList, DbType dbType) Deprecated. -
createSchemaStatVisitor
-
createSchemaStatVisitor
-
createSchemaStatVisitor
-
parseStatements
public static List<SQLStatement> parseStatements(String sql, String dbType, SQLParserFeature... features) -
parseStatements
public static List<SQLStatement> parseStatements(String sql, DbType dbType, SQLParserFeature... features) -
parseStatements
-
parseStatements
-
parseStatements
-
parseSingleStatement
-
parseSingleStatement
public static SQLStatement parseSingleStatement(String sql, String dbType, SQLParserFeature... features) -
parseSingleStatement
public static SQLStatement parseSingleStatement(String sql, DbType dbType, SQLParserFeature... features) -
parseSingleMysqlStatement
-
buildToDate
public static String buildToDate(String columnName, String tableAlias, String pattern, DbType dbType) - Parameters:
columnName-tableAlias-pattern- if pattern is null,it will be set {%Y-%m-%d %H:%i:%s} as mysql default value and set {yyyy-mm-dd hh24:mi:ss} as oracle default valuedbType-DbTypeif dbType is null ,it will be set the mysql as a default value
-
split
-
translateOracleToMySql
-
addCondition
-
addCondition
public static String addCondition(String sql, String condition, SQLBinaryOperator op, boolean left, DbType dbType) -
addCondition
public static void addCondition(SQLStatement stmt, SQLBinaryOperator op, SQLExpr condition, boolean left) -
buildCondition
public static SQLExpr buildCondition(SQLBinaryOperator op, SQLExpr condition, boolean left, SQLExpr where) -
addSelectItem
-
acceptBooleanOr
public static void acceptBooleanOr(String sql, DbType dbType, Consumer<SQLBinaryOpExprGroup> consumer) -
acceptBinaryOpExprGroup
public static void acceptBinaryOpExprGroup(String sql, DbType dbType, Consumer<SQLBinaryOpExprGroup> consumer, Predicate<SQLBinaryOpExprGroup> filter) -
acceptBinaryOpExpr
public static void acceptBinaryOpExpr(String sql, DbType dbType, Consumer<SQLBinaryOpExpr> consumer, Predicate<SQLBinaryOpExpr> filter) -
acceptTableSource
public static void acceptTableSource(String sql, DbType dbType, Consumer<SQLTableSource> consumer, Predicate<SQLTableSource> filter) -
acceptSelectQueryBlock
public static void acceptSelectQueryBlock(String sql, DbType dbType, Consumer<SQLSelectQueryBlock> consumer, Predicate<SQLSelectQueryBlock> filter) -
acceptAggregateFunction
public static void acceptAggregateFunction(String sql, DbType dbType, Consumer<SQLAggregateExpr> consumer, Predicate<SQLAggregateExpr> filter) -
acceptFunction
public static void acceptFunction(String sql, DbType dbType, Consumer<SQLMethodInvokeExpr> consumer, Predicate<SQLMethodInvokeExpr> filter) -
acceptInsertInto
public static void acceptInsertInto(String sql, DbType dbType, Consumer<SQLInsertInto> consumer, Predicate<SQLInsertInto> filter) -
addSelectItem
-
addSelectItem
-
addSelectItem
public static void addSelectItem(SQLSelectQueryBlock queryBlock, SQLExpr expr, String alias, boolean first) -
refactor
-
hash
-
not
-
normalize
-
normalize
-
normalize
-
forcedNormalize
-
nameEquals
-
nameEquals
-
isValue
-
replaceInParent
-
replaceInParent
-
replaceInParent
-
replaceInParent
-
replaceInParent
-
replaceInParent
-
desensitizeTable
-
sort
重新排序建表语句,解决建表语句的依赖关系- Parameters:
sql-dbType-
-
clearLimit
- Parameters:
query-dbType-- Returns:
- 0:sql.toString, 1:
-
getLimit
-
getLimit
-
convertTimeZone
-
convertTimeZone
-
splitInsertValues
-
isQuoteChar
public static boolean isQuoteChar(char c) -
removeQuote
-
getAllTokens
Get all tokens from SQL parsing based on dialect type, including their string values- Parameters:
sql- SQL statement to parsedbType- Database type (dialect)- Returns:
- List of TokenInfo objects containing token and corresponding stringVal pairs (excluding comments)
-
getAllTokens
public static List<SQLUtils.TokenInfo> getAllTokens(String sql, DbType dbType, boolean keepComments) Get all tokens from SQL parsing based on dialect type, including their string values- Parameters:
sql- SQL statement to parsedbType- Database type (dialect)keepComments- Whether to keep comment tokens (LINE_COMMENT, MULTI_LINE_COMMENT, HINT)- Returns:
- List of TokenInfo objects containing token and corresponding stringVal pairs
-
getAllTokens
Get all tokens from SQL parsing based on dialect type (overloaded method with String dbType)- Parameters:
sql- SQL statement to parsedbType- Database type string (will be converted to DbType)- Returns:
- List of TokenInfo objects containing token and corresponding stringVal pairs
-
getAllTokens
public static List<SQLUtils.TokenInfo> getAllTokens(String sql, String dbType, boolean keepComments) Get all tokens from SQL parsing based on dialect type (overloaded method with String dbType)- Parameters:
sql- SQL statement to parsedbType- Database type string (will be converted to DbType)keepComments- Whether to keep comment tokens (LINE_COMMENT, MULTI_LINE_COMMENT, HINT)- Returns:
- List of TokenInfo objects containing token and corresponding stringVal pairs
-
calculateTokenLevenshteinDistance
public static int calculateTokenLevenshteinDistance(String sql1, DbType dbType1, String sql2, DbType dbType2) Calculate Levenshtein distance between two SQL statements based on their token sequences. For IDENTIFIER, PROPERTY, and LITERAL_INT tokens, the stringVal must also match for tokens to be considered equal.- Parameters:
sql1- First SQL statementdbType1- Database type for first SQLsql2- Second SQL statementdbType2- Database type for second SQL- Returns:
- Levenshtein distance between the two token sequences
-