Package com.alibaba.druid.sql
Class SQLUtils
java.lang.Object
com.alibaba.druid.sql.SQLUtils
-
Nested Class Summary
Nested Classes -
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) Builds a SQL expression to convert a column's value to a date format based on the provided pattern and database type.static Object[]clearLimit(String query, DbType dbType) Clears the LIMIT clause from the provided SQL query and returns the modified query and the extracted LIMIT information.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 SQLLimitgetLimit(SQLStatement statement, DbType dbType) static SQLLimitstatic longstatic 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 booleanreplaceInParent(SQLExpr expr, SQLExpr target) static booleanreplaceInParent(SQLSelectQuery cmp, SQLSelectQuery dest) static booleanreplaceInParent(SQLTableSource cmp, SQLTableSource dest) static StringSorts the SQL statements in the provided SQL query string based on the specified database type.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 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, 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 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
-
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, List<Object> parameters, SQLUtils.FormatOption option, Map<String, String> tableMapping) -
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) Builds a SQL expression to convert a column's value to a date format based on the provided pattern and database type.- Parameters:
columnName- the name of the column to be convertedtableAlias- the alias of the table containing the column (optional)pattern- the date format pattern to be used for the conversion (optional)dbType- the database type for determining the appropriate conversion function- Returns:
- a SQL expression representing the converted date value, or an empty string if unable to build the expression
-
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
-
desensitizeTable
-
sort
Sorts the SQL statements in the provided SQL query string based on the specified database type.- Parameters:
sql- the SQL query string to be sorteddbType- the database type for parsing and sorting the SQL statements- Returns:
- a sorted SQL query string, or an empty string if the input is invalid
-
clearLimit
Clears the LIMIT clause from the provided SQL query and returns the modified query and the extracted LIMIT information.- Parameters:
query- the SQL query string to be modifieddbType- the database type for parsing the SQL statements- Returns:
- an array containing the modified SQL query string and the extracted LIMIT information, or null if no LIMIT clause is found
-
getLimit
-
getLimit
-
convertTimeZone
-
convertTimeZone
-
splitInsertValues
-