public class StringUtils extends Object
| Modifier and Type | Field | Description |
|---|---|---|
(package private) static char[] |
EMPTY_SPACE |
|
private static char[] |
HEX_DIGITS |
|
private static int |
WILD_COMPARE_CONTINUE_WITH_WILD |
|
private static int |
WILD_COMPARE_MATCH |
|
private static int |
WILD_COMPARE_NO_MATCH |
|
(package private) static char |
WILDCARD_ESCAPE |
|
(package private) static char |
WILDCARD_MANY |
|
(package private) static char |
WILDCARD_ONE |
| Constructor | Description |
|---|---|
StringUtils() |
| Modifier and Type | Method | Description |
|---|---|---|
static void |
appendAsHex(StringBuilder builder,
byte[] bytes) |
|
static void |
appendAsHex(StringBuilder builder,
int value) |
|
static boolean |
canHandleAsServerPreparedStatementNoCache(String sql,
ServerVersion serverVersion,
boolean multiQueriesEnabled,
boolean noBackslashEscapes,
boolean useAnsiQuotes) |
|
static String |
dumpAsHex(byte[] byteBuffer,
int length) |
Returns the given bytes as a hex and ASCII dump (up to length bytes).
|
private static boolean |
endsWith(byte[] dataFrom,
String suffix) |
|
static boolean |
endsWithIgnoreCase(String searchIn,
String searchFor) |
Determines whether or not the string 'searchIn' ends with the string 'searchFor', dis-regarding case starting at 'startAt' Shorthand for a
String.regionMatch(...)
|
static String |
enquoteIdentifier(String value,
char quoteChar,
boolean pedantic,
boolean ansiQuotes,
boolean backslashEscape) |
Enquotes and/or validates an SQL identifier (e.g., table/column name) using the provided quote character.
|
static String |
enquoteLiteral(String value,
boolean pedantic,
boolean ansiQuotes,
boolean backslashEscape) |
Enquotes and/or validates an SQL string literal value.
|
private static String |
enquoteValue(String value,
char quoteChar,
boolean pedantic,
boolean backslashEscape,
IntPredicate quoteRule) |
Enquotes and/or validates an SQL literal/identifier-like value by (a) detecting whether the input is already surrounded by an allowed quote character and
is internally well-formed, and (b) otherwise producing a freshly quoted string using
quoteChar. |
static void |
escapeBytes(ByteArrayOutputStream bOut,
byte[] x) |
|
static String |
escapeQuote(String str,
String quotChar) |
|
static StringBuilder |
escapeString(StringBuilder buf,
String x,
boolean useAnsiQuotedIdentifiers,
CharsetEncoder charsetEncoder) |
|
static char |
firstAlphaCharUc(String searchIn,
int startAt) |
|
static char |
firstNonWsCharUc(String searchIn) |
Returns the first non-whitespace char, converted to upper case
|
static char |
firstNonWsCharUc(String searchIn,
int startAt) |
|
static String |
fixDecimalExponent(String dString) |
Adds '+' to decimal numbers that are positive (MySQL doesn't understand
them otherwise
|
static byte[] |
getBytes(char[] value) |
Returns the byte[] representation of subset of the given char[] using the default/platform encoding.
|
static byte[] |
getBytes(char[] value,
int offset,
int length) |
|
static byte[] |
getBytes(char[] value,
int offset,
int length,
String encoding) |
Returns the byte[] representation of subset of the given char[] using the given encoding.
|
static byte[] |
getBytes(char[] c,
String encoding) |
Returns the byte[] representation of subset of the given char[] using the given encoding.
|
static byte[] |
getBytes(String value) |
|
static byte[] |
getBytes(String value,
int offset,
int length) |
|
static byte[] |
getBytes(String value,
int offset,
int length,
String encoding) |
|
static byte[] |
getBytes(String s,
String encoding) |
Returns the byte[] representation of the given string using the given encoding.
|
static byte[] |
getBytesNullTerminated(String value,
String encoding) |
|
static byte[] |
getBytesWrapped(String s,
char beginWrap,
char endWrap,
String encoding) |
Returns the byte[] representation of the given string properly wrapped between the given char delimiters using the given encoding.
|
static String |
getFullyQualifiedName(String db,
String entity,
char quoteChar,
boolean isPedantic) |
Builds and returns a fully qualified name, quoted if necessary, for the given database entity.
|
static String |
getUniqueSavepointId() |
|
static boolean |
hasWildcards(String src) |
Does the string contain wildcard symbols ('%' or '_').
|
static void |
hexEscapeBlock(byte[] buf,
int size,
BiConsumer<Byte,Byte> bc) |
Used to escape binary data with hex
|
static int |
indexOf(byte[] s,
char c) |
|
static int |
indexOfIgnoreCase(int startingPosition,
String searchIn,
String searchFor) |
Finds the position of a substring within a string ignoring case.
|
static int |
indexOfIgnoreCase(int startingPosition,
String searchIn,
String[] searchForSequence,
String openingMarkers,
String closingMarkers,
Set<SearchMode> searchMode) |
Finds the position of the first of a consecutive sequence of strings within a string, ignoring case, with the option to skip text delimited by given
markers or within comments.
|
static int |
indexOfIgnoreCase(int startingPosition,
String searchIn,
String searchFor,
String openingMarkers,
String closingMarkers,
Set<SearchMode> searchMode) |
Finds the position of a substring within a string, ignoring case, with the option to skip text delimited by given markers or within comments.
|
static int |
indexOfIgnoreCase(int startingPosition,
String searchIn,
String searchFor,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode) |
Finds the position of a substring within a string, ignoring case, with the option to skip text delimited by given markers or within comments.
|
static int |
indexOfIgnoreCase(String searchIn,
String searchFor) |
Finds the position of a substring within a string ignoring case.
|
static int |
indexOfNextAlphanumericChar(int startingPosition,
String searchIn,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode) |
Finds the position of the next alphanumeric character within a string, with the option to skip text delimited by given markers or within comments.
|
static int |
indexOfNextNonWsChar(int startingPosition,
String searchIn,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode) |
Finds the position of the next non-whitespace character within a string, with the option to skip text delimited by given markers or within comments.
|
static int |
indexOfNextWsChar(int startingPosition,
String searchIn,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode) |
Finds the position of the next whitespace character within a string, with the option to skip text delimited by given markers or within comments.
|
static int |
indexOfQuoteDoubleAware(String searchIn,
char quoteChar,
int startFrom) |
|
private static boolean |
isCharAtPosNotEqualIgnoreCase(String searchIn,
int pos,
char firstCharOfSearchForUc,
char firstCharOfSearchForLc) |
|
protected static boolean |
isCharEqualIgnoreCase(char charToCompare,
char compareToCharUC,
char compareToCharLC) |
|
static boolean |
isEmptyOrWhitespaceOnly(String str) |
|
static boolean |
isNullOrEmpty(String str) |
|
static boolean |
isSimpleIdentifier(String identifier,
Predicate<String> reservedWordChecker) |
Checks whether the supplied string is a "simple" (unquoted) MySQL identifier.
|
static boolean |
isStrictlyNumeric(CharSequence cs) |
Checks is the CharSequence contains digits only.
|
static boolean |
isValidIdentifierChar(char ch) |
Tests whether a single Java char is allowed in an unquoted MySQL identifier.
|
static boolean |
isValidIdentifierCodePoint(int codePoint) |
Tests whether a single Unicode code-point is allowed in an unquoted MySQL identifier.
|
static String |
joinWithSerialComma(List<?> elements) |
Joins all elements of the given list using serial comma (Oxford comma) rules.
|
static int |
lastIndexOf(byte[] s,
char c) |
|
static boolean |
nullSafeEqual(String str1,
String str2) |
Two given strings are considered equal if both are null or if they have the same string value.
|
static String |
padString(String stringVal,
int requiredLength) |
|
static byte[] |
quoteBytes(byte[] bytes) |
|
static String |
quoteIdentifier(String identifier,
boolean isPedantic) |
Surrounds identifier with "`" and duplicates these symbols inside the identifier.
|
static String |
quoteIdentifier(String identifier,
char quoteChar,
boolean isPedantic) |
Surrounds identifier with quoteChar and duplicates these symbols inside the identifier.
|
static boolean |
regionMatchesIgnoreCase(String searchIn,
int startAt,
String searchFor) |
Determines whether or not the string 'searchIn' contains the string 'searchFor', disregarding case and starting at 'startAt'.
|
static int |
safeIntParse(String intAsString) |
|
static String |
safeTrim(String toTrim) |
|
static String |
sanitizeProcOrFuncName(String src) |
Next two functions are to help DBMD check if the given string is in form of database.name and return it as "database";"name" with comments removed.
|
static List<String> |
split(String stringToSplit,
String delimiter,
boolean trim) |
Splits stringToSplit into a list, using the given delimiter
|
static List<String> |
split(String stringToSplit,
String delimiter,
String openingMarkers,
String closingMarkers,
boolean trim) |
Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.
|
static List<String> |
split(String stringToSplit,
String delimiter,
String openingMarkers,
String closingMarkers,
boolean trim,
Set<SearchMode> searchMode) |
Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.
|
static List<String> |
split(String stringToSplit,
String delimiter,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
boolean trim) |
Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.
|
static List<String> |
split(String stringToSplit,
String delimiter,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
boolean trim,
Set<SearchMode> searchMode) |
Splits stringToSplit into a list, using the given delimiter and skipping all between the given markers.
|
static List<String> |
splitDbDotName(String source,
String db,
char quoteChar,
boolean isNoBslashEscSet) |
Splits an entity identifier into its parts (database and entity name) and returns a list containing the two elements.
|
private static boolean |
startsWith(byte[] dataFrom,
String chars) |
|
static boolean |
startsWithIgnoreCase(String searchIn,
String searchFor) |
Determines whether or not the string 'searchIn' starts with the string 'searchFor', dis-regarding case.
|
static boolean |
startsWithIgnoreCaseAndNonAlphaNumeric(String searchIn,
String searchFor) |
Determines whether or not the string 'searchIn' starts with the string 'searchFor', disregarding case,leading whitespace and non-alphanumeric characters.
|
static boolean |
startsWithIgnoreCaseAndWs(String searchIn,
String searchFor) |
Determines whether or not the string 'searchIn' starts with the string 'searchFor', disregarding case and leading whitespace
|
static int |
startsWithIgnoreCaseAndWs(String searchIn,
String[] searchFor) |
Determines whether or not the string 'searchIn' starts with one of the strings in 'searchFor', disregarding case and leading whitespace
|
static boolean |
startsWithIgnoreCaseAndWs(String searchIn,
String searchFor,
int beginPos) |
Determines whether or not the string 'searchIn' contains the string 'searchFor', disregarding case and leading whitespace
|
static String |
stringArrayToString(String[] elems,
String prefix,
String midDelimiter,
String lastDelimiter,
String suffix) |
Constructs a String containing all the elements in the String array bounded and joined by the provided concatenation elements.
|
static String |
stripCommentsAndHints(String source,
String openingMarkers,
String closingMarkers,
boolean allowBackslashEscapes) |
Removes comments and hints from the given string.
|
static byte[] |
stripEnclosure(byte[] source,
String prefix,
String suffix) |
|
static char[] |
toAsciiCharArray(byte[] buffer,
int startPos,
int length) |
Returns the bytes as an ASCII String.
|
static String |
toAsciiString(byte[] buffer) |
Returns the bytes as an ASCII String.
|
static String |
toAsciiString(byte[] buffer,
int startPos,
int length) |
Returns the bytes as an ASCII String.
|
static String |
toHexString(byte[] byteBuffer,
int length) |
Converts the given byte array into Hex String, stopping at given length.
|
static String |
toString(byte[] value) |
|
static String |
toString(byte[] value,
Charset charset) |
|
static String |
toString(byte[] value,
int offset,
int length) |
|
static String |
toString(byte[] value,
int offset,
int length,
String encoding) |
|
static String |
toString(byte[] value,
String encoding) |
|
static byte[] |
unquoteBytes(byte[] bytes) |
|
static String |
unquoteIdentifier(String identifier,
char quoteChar) |
Trims the identifier, removes quote chars from first and last positions and replaces double occurrences of quote char from entire identifier, i.e.
|
static String |
urlEncode(String stringToEncode) |
URL-encode the given string.
|
static boolean |
wildCompareIgnoreCase(String searchIn,
String searchFor) |
Compares searchIn against searchForWildcard with wildcards, in a case insensitive manner.
|
private static int |
wildCompareInternal(String searchIn,
String searchFor) |
Compares searchIn against searchForWildcard with wildcards (heavily borrowed from strings/ctype-simple.c in the server sources)
This method does a single passage matching for normal characters and WILDCARD_ONE (_), and recursive matching for WILDCARD_MANY (%) which may be repeated
for as many anchor chars are found.
|
static String |
zeroFill(String value,
int length) |
private static final int WILD_COMPARE_MATCH
private static final int WILD_COMPARE_CONTINUE_WITH_WILD
private static final int WILD_COMPARE_NO_MATCH
static final char WILDCARD_MANY
static final char WILDCARD_ONE
static final char WILDCARD_ESCAPE
private static final char[] HEX_DIGITS
static final char[] EMPTY_SPACE
public static String dumpAsHex(byte[] byteBuffer, int length)
byteBuffer - the data to dump as hexlength - the number of bytes to printpublic static String toHexString(byte[] byteBuffer, int length)
byteBuffer - the byte array to convertlength - the number of bytes from the given array to convertprivate static boolean endsWith(byte[] dataFrom,
String suffix)
public static char firstNonWsCharUc(String searchIn)
searchIn - the string to search inpublic static char firstNonWsCharUc(String searchIn, int startAt)
public static char firstAlphaCharUc(String searchIn, int startAt)
public static String fixDecimalExponent(String dString)
dString - The value as a stringpublic static byte[] getBytes(String s, String encoding)
s - source stringencoding - java encodingpublic static byte[] getBytesWrapped(String s, char beginWrap, char endWrap, String encoding)
s - source stringbeginWrap - opening char delimiterendWrap - closing char delimiterencoding - java encodingpublic static int indexOfIgnoreCase(String searchIn, String searchFor)
searchIn - the string to search insearchFor - the array of strings to search forsearchFor is found within searchIn starting from startingPosition.public static int indexOfIgnoreCase(int startingPosition,
String searchIn,
String searchFor)
startingPosition - the position to start the search fromsearchIn - the string to search insearchFor - the array of strings to search forsearchFor is found within searchIn starting from startingPosition.public static int indexOfIgnoreCase(int startingPosition,
String searchIn,
String[] searchForSequence,
String openingMarkers,
String closingMarkers,
Set<SearchMode> searchMode)
Independently of the searchMode provided, when searching for the second and following strings SearchMode.SKIP_WHITE_SPACE will
be added and SearchMode.SKIP_BETWEEN_MARKERS removed.
startingPosition - the position to start the search fromsearchIn - the string to search insearchForSequence - searchForSequenceopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipsearchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behavior of the searchsearchFor is found within searchIn starting from startingPosition.public static int indexOfIgnoreCase(int startingPosition,
String searchIn,
String searchFor,
String openingMarkers,
String closingMarkers,
Set<SearchMode> searchMode)
startingPosition - the position to start the search fromsearchIn - the string to search insearchFor - the string to search foropeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipsearchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behavior of the searchsearchFor is found within searchIn starting from startingPosition.public static int indexOfIgnoreCase(int startingPosition,
String searchIn,
String searchFor,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode)
startingPosition - the position to start the search fromsearchIn - the string to search insearchFor - the string to search foropeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipoverridingMarkers - the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and
overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed,
otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.searchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behavior of the searchsearchFor is found within searchIn starting from startingPosition.public static int indexOfNextAlphanumericChar(int startingPosition,
String searchIn,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode)
startingPosition - the position to start the search fromsearchIn - the string to search inopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipoverridingMarkers - the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and
overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed,
otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.searchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behavior of the searchsearchIn starting from startingPosition.public static int indexOfNextNonWsChar(int startingPosition,
String searchIn,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode)
startingPosition - the position to start the search fromsearchIn - the string to search inopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipoverridingMarkers - the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and
overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed,
otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.searchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behavior of the searchsearchIn starting from startingPosition.public static int indexOfNextWsChar(int startingPosition,
String searchIn,
String openingMarkers,
String closingMarkers,
String overridingMarkers,
Set<SearchMode> searchMode)
startingPosition - the position to start the search fromsearchIn - the string to search inopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipoverridingMarkers - the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and
overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed,
otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.searchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behavior of the searchsearchIn starting from startingPosition.private static boolean isCharAtPosNotEqualIgnoreCase(String searchIn, int pos, char firstCharOfSearchForUc, char firstCharOfSearchForLc)
protected static boolean isCharEqualIgnoreCase(char charToCompare,
char compareToCharUC,
char compareToCharLC)
public static List<String> split(String stringToSplit, String delimiter, boolean trim)
stringToSplit - the string to splitdelimiter - the string to split ontrim - should the split strings be whitespace trimmed?IllegalArgumentException - if an error occurspublic static List<String> split(String stringToSplit, String delimiter, String openingMarkers, String closingMarkers, boolean trim)
stringToSplit - the string to splitdelimiter - the string to split onopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skiptrim - should the split strings be whitespace trimmed?IllegalArgumentException - if an error occurspublic static List<String> split(String stringToSplit, String delimiter, String openingMarkers, String closingMarkers, boolean trim, Set<SearchMode> searchMode)
stringToSplit - the string to splitdelimiter - the string to split onopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skiptrim - should the split strings be whitespace trimmed?searchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behaviour of the searchIllegalArgumentException - if an error occurspublic static List<String> split(String stringToSplit, String delimiter, String openingMarkers, String closingMarkers, String overridingMarkers, boolean trim)
stringToSplit - the string to splitdelimiter - the string to split onopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipoverridingMarkers - the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and
overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed,
otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.trim - should the split strings be whitespace trimmed?IllegalArgumentException - if an error occurspublic static List<String> split(String stringToSplit, String delimiter, String openingMarkers, String closingMarkers, String overridingMarkers, boolean trim, Set<SearchMode> searchMode)
stringToSplit - the string to splitdelimiter - the string to split onopeningMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipoverridingMarkers - the subset of openingMarkers that override the remaining markers, e.g., if openingMarkers = "'(" and
overridingMarkers = "'" then the block between the outer parenthesis in "start ('max('); end" is strictly consumed,
otherwise the suffix " end" would end up being consumed too in the process of handling the nested parenthesis.trim - should the split strings be whitespace trimmed?searchMode - a Set, ideally an EnumSet, containing the flags from the enum StringUtils.SearchMode that determine the
behaviour of the searchIllegalArgumentException - if an error occursprivate static boolean startsWith(byte[] dataFrom,
String chars)
public static boolean regionMatchesIgnoreCase(String searchIn, int startAt, String searchFor)
searchIn - the string to search instartAt - the position to start atsearchFor - the string to search forpublic static boolean startsWithIgnoreCase(String searchIn, String searchFor)
searchIn - the string to search insearchFor - the string to search forpublic static boolean startsWithIgnoreCaseAndNonAlphaNumeric(String searchIn, String searchFor)
searchIn - the string to search insearchFor - the string to search forpublic static boolean startsWithIgnoreCaseAndWs(String searchIn, String searchFor)
searchIn - the string to search insearchFor - the string to search forpublic static boolean startsWithIgnoreCaseAndWs(String searchIn, String searchFor, int beginPos)
searchIn - the string to search insearchFor - the string to search forbeginPos - where to start searchingpublic static int startsWithIgnoreCaseAndWs(String searchIn, String[] searchFor)
searchIn - the string to search insearchFor - the string array to search forpublic static boolean endsWithIgnoreCase(String searchIn, String searchFor)
searchIn - the string to search insearchFor - the string to search forpublic static byte[] stripEnclosure(byte[] source,
String prefix,
String suffix)
source - bytes to stripprefix - prefixsuffix - suffixpublic static String toAsciiString(byte[] buffer)
buffer - the bytes representing the stringpublic static String toAsciiString(byte[] buffer, int startPos, int length)
buffer - the bytes to convertstartPos - the position to start convertinglength - the length of the string to convertpublic static char[] toAsciiCharArray(byte[] buffer,
int startPos,
int length)
buffer - the bytes to convertstartPos - the position to start convertinglength - the length of the string to convertpublic static boolean wildCompareIgnoreCase(String searchIn, String searchFor)
searchIn - the string to search insearchFor - the string to search for, using the 'standard' SQL wildcard chars of '%' and '_'private static int wildCompareInternal(String searchIn, String searchFor)
searchIn - the string to search insearchFor - the string to search for, using the 'standard' SQL wildcard chars of '%' and '_'public static int lastIndexOf(byte[] s,
char c)
public static int indexOf(byte[] s,
char c)
public static boolean isNullOrEmpty(String str)
public static boolean nullSafeEqual(String str1, String str2)
str1 - first string to comparestr2 - fecond string to comparetrue if both strings are null or have the same valuepublic static String stripCommentsAndHints(String source, String openingMarkers, String closingMarkers, boolean allowBackslashEscapes)
source - the query string to clean up.openingMarkers - characters that delimit the beginning of a text block to skipclosingMarkers - characters that delimit the end of a text block to skipallowBackslashEscapes - whether or not backslash escapes are allowedpublic static String sanitizeProcOrFuncName(String src)
src - the source stringpublic static List<String> splitDbDotName(String source, String db, char quoteChar, boolean isNoBslashEscSet)
db is used in its place and source corresponds to the full entity name.
If argument source is NULL or wildcard (%), returns an empty list.source - the source stringdb - database, if availablequoteChar - quote character as defined on serverisNoBslashEscSet - is our connection in no BackSlashEscape modepublic static String getFullyQualifiedName(String db, String entity, char quoteChar, boolean isPedantic)
db - database nameentity - identifierquoteChar - quote character as defined on serverisPedantic - are we in pedantic modepublic static boolean isEmptyOrWhitespaceOnly(String str)
public static String quoteIdentifier(String identifier, char quoteChar, boolean isPedantic)
quoteChar - ` or "identifier - in pedantic mode (connection property pedantic=true) identifier is treated as unquoted (as it is stored in the database) even if it starts and
ends with quoteChar;
in non-pedantic mode if identifier starts and ends with quoteChar method treats it as already quoted and doesn't modify.isPedantic - operating in pedantic mode?-> null-> `abc`-> `ab``c`-> `ab"c`-> `ab``c` in non-pedantic mode or ```ab````c``` in pedantic mode-> null-> "abc"-> "ab`c"-> "ab""c"-> "ab""c" in non-pedantic mode or """ab""""c""" in pedantic modepublic static String quoteIdentifier(String identifier, boolean isPedantic)
identifier - in pedantic mode (connection property pedantic=true) identifier is treated as unquoted (as it is stored in the database) even if it starts and
ends with "`";
in non-pedantic mode if identifier starts and ends with "`" method treats it as already quoted and doesn't modify.isPedantic - are we in pedantic mode-> null-> `abc`-> `ab``c`-> `ab"c`-> `ab``c` in non-pedantic mode or ```ab````c``` in pedantic modepublic static String unquoteIdentifier(String identifier, char quoteChar)
identifier - identifierquoteChar - ` or "| Input | | | Quote '`' | | | Quote '"' |
| abc | | | abc | | | abc |
| `abc` | | | abc | | | `abc` |
| `ab``c` | | | ab`c | | | `ab``c` |
| `"ab`c"` | | | `"ab`c"` | | | `"ab`c"` |
| `ab"c` | | | ab"c | | | `ab"c` |
| "abc" | | | "abc" | | | abc |
| "`ab""c`" | | | "`ab""c`" | | | `ab"c` |
| "ab`c" | | | "ab`c" | | | ab`c |
public static boolean isValidIdentifierChar(char ch)
ch - the character to testpublic static boolean isValidIdentifierCodePoint(int codePoint)
codePoint - Unicode code-pointpublic static int indexOfQuoteDoubleAware(String searchIn, char quoteChar, int startFrom)
public static String toString(byte[] value, int offset, int length)
public static String toString(byte[] value)
public static byte[] getBytes(char[] value)
value - charspublic static byte[] getBytes(char[] c,
String encoding)
c - charsencoding - java encodingpublic static byte[] getBytes(char[] value,
int offset,
int length)
public static byte[] getBytes(char[] value,
int offset,
int length,
String encoding)
value - charsoffset - offsetlength - lengthencoding - java encodingpublic static byte[] getBytes(String value)
public static byte[] getBytes(String value, int offset, int length)
public static final void hexEscapeBlock(byte[] buf,
int size,
BiConsumer<Byte,Byte> bc)
buf - source bytessize - number of bytes to readbc - consumer for low and high bits of each bytepublic static void appendAsHex(StringBuilder builder, byte[] bytes)
public static void appendAsHex(StringBuilder builder, int value)
public static boolean canHandleAsServerPreparedStatementNoCache(String sql, ServerVersion serverVersion, boolean multiQueriesEnabled, boolean noBackslashEscapes, boolean useAnsiQuotes)
public static int safeIntParse(String intAsString)
public static boolean isStrictlyNumeric(CharSequence cs)
cs - The CharSequence to check.true if the CharSequence not empty and contains only digits, false otherwise.public static String stringArrayToString(String[] elems, String prefix, String midDelimiter, String lastDelimiter, String suffix)
elems - the String array from where to take the elements.prefix - the prefix of the resulting String.midDelimiter - the delimiter to be used between the N-1 elementslastDelimiter - the delimiter to be used before the last element.suffix - the suffix of the resulting String.public static boolean hasWildcards(String src)
src - stringpublic static String getUniqueSavepointId()
public static String joinWithSerialComma(List<?> elements)
elements - the elements to joinpublic static byte[] unquoteBytes(byte[] bytes)
public static byte[] quoteBytes(byte[] bytes)
public static StringBuilder escapeString(StringBuilder buf, String x, boolean useAnsiQuotedIdentifiers, CharsetEncoder charsetEncoder)
public static void escapeBytes(ByteArrayOutputStream bOut, byte[] x)
public static String urlEncode(String stringToEncode)
stringToEncode - the string to encodeprivate static String enquoteValue(String value, char quoteChar, boolean pedantic, boolean backslashEscape, IntPredicate quoteRule)
quoteChar.
Behavior summary:
quoteChar and doubles every occurrence of
quoteChar inside the value.quoteRule), this method attempts to
treat the input as already quoted and validates:
backslashEscape is enabled, andvalue is returned unchanged.quoteChar. Any occurrences of
quoteChar encountered while scanning are doubled in the produced output (unless escaped by backslash when backslashEscape is
enabled).
The quote delimiter that is validated inside the input is the detected opening quote character (if any), whereas the quote delimiter used when
producing a new quoted value is always quoteChar.
value - The input string to validate and/or quote.quoteChar - The quote character to use when generating a new quoted value (e.g. '\'', '"', or '`')pedantic - Whether to disable "already quoted" detection and force stricter quoting behavior.backslashEscape - Whether to treat backslash (\) as an escape character when validating internal quotes and when determining whether a closing quote is
escaped.quoteRule - Predicate that returns true for character that should be treated as a valid quote delimiter for "already quoted" detection (typically
by capturing session/sql-mode state such as ANSI_QUOTES in the caller).value unchanged if it is detected to be already properly quoted and internally well-formed; otherwise, a newly quoted representation
surrounded by quoteChar.public static String enquoteLiteral(String value, boolean pedantic, boolean ansiQuotes, boolean backslashEscape)
In non-pedantic mode, if value appears to be already quoted, this method attempts to validate it and will return it unchanged when it is
considered properly quoted. Otherwise it will generate a new SQL literal quoted with single quotes (').
Quote detection rules (non-pedantic):
'...' as a quoted literal."..." as a quoted literal only when ansiQuotes == false (i.e., " is treated as a string delimiter, not an
identifier delimiter).Escaping rules:
backslashEscape == false: internal single quotes are handled by doubling ('').backslashEscape == true: backslash escaping is honored when validating whether quotes are escaped, and the closing quote is considered
invalid if escaped by an odd number of trailing backslashes.value - The input value to quote and/or validate as a SQL string literal.pedantic - If true, disables "already quoted" detection and forces generation of a quoted literal (with escaping rules applied).ansiQuotes - Whether ANSI_QUOTES is enabled for the target SQL mode; affects whether "..." is treated as an already-quoted literal (only when
false)backslashEscape - Whether backslash (\) is treated as an escape character for quote validation and closing-quote detection.value unchanged if it is detected to be already properly quoted; otherwise a newly generated single-quoted SQL literal.public static String enquoteIdentifier(String value, char quoteChar, boolean pedantic, boolean ansiQuotes, boolean backslashEscape)
In non-pedantic mode, if value appears to be already quoted with an allowed identifier quote delimiter, this method attempts to validate it and
will return it unchanged when it is considered properly quoted. Otherwise it will generate a newly quoted identifier using quoteChar.
Quote detection rules (non-pedantic):
`...` (backticks) as a quoted identifier."..." as a quoted identifier only when ansiQuotes == true (i.e., " is treated as an identifier delimiter).Escaping rules:
pedantic == true and backslashEscape == false: the method simply surrounds the value with quoteChar and doubles every
occurrence of quoteChar inside the value.backslashEscape == true: backslash escaping is honored when validating whether quotes are escaped, and the closing quote is considered
invalid if escaped by an odd number of trailing backslashes.Note: This method does not validate identifier semantics (allowed characters, reserved words, qualification, etc.); it only deals with quoting/escaping/validation of the delimiter characters.
value - The identifier text to quote and/or validate.quoteChar - The quote character to use when generating a new quoted identifier (commonly '`' or '"', depending on SQL dialect/mode).pedantic - If true, disables "already quoted" detection and forces generation of a quoted identifier (with escaping rules applied).ansiQuotes - Whether ANSI_QUOTES is enabled for the target SQL mode; affects whether "..." is treated as an already-quoted identifier (only when
true).backslashEscape - Whether backslash (\) is treated as an escape character for quote validation and closing-quote detection.value unchanged if it is detected to be already properly quoted; otherwise a newly generated quoted identifier using quoteChar.public static boolean isSimpleIdentifier(String identifier, Predicate<String> reservedWordChecker)
The method enforces the following constraints:
null and non-empty.isValidIdentifierChar(char).reservedWordChecker is provided (non-null), the identifier must not be a reserved word as determined by
reservedWordChecker.test(identifier).
Reserved word handling is delegated to the optional reservedWordChecker to avoid hard dependencies on metadata/JDBC layers and to allow callers
to apply the correct rules for their context (e.g., server version, SQL mode, or a specific reserved word set). If reservedWordChecker is
null, reserved word checks are skipped.
identifier - The candidate identifier string to validate.reservedWordChecker - Optional predicate used to determine whether identifier is a reserved word; if null, reserved word checks are not performed.
Callers typically implement this predicate as a case-insensitive check (preferably using Locale.ROOT when performing case
conversions).true if identifier meets this method's definition of a simple MySQL identifier; false otherwise.