Class StringHelper
- java.lang.Object
-
- org.teichert.databaseexplorer.utils.StringHelper
-
public final class StringHelper extends Object
Methods for String manipulation and other useful methods about strings.
-
-
Field Summary
Fields Modifier and Type Field Description static String
PATTERN_SQL_COMMENTS
static Pattern
patternComments
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
createTableName(String catalog, String schema, String name)
Returns the full tablename with catalog.schema.tablename.static String
createTableName(SQLTable table)
Returns the full tablename with catalog.schema.tablename.static boolean
endsWith(String string, String... suffixes)
Checks if the string ends with one of the given suffixes.static String
escapeSQL(String sql)
Escapes all comments and quotes inside the given sql.static List<String>
extractAllJoinedTableNames(String statement)
Gets and extracts all joined tableNames from the sql starting from the given startPos.static String
extractSingleStatement(String sql, int startPos)
Extracts any sql and return a single statement.static SQLTable
extractTableFromSQL(String sql)
Returns and extract the SQLTable from the given SQL.
The table is always the table after the "FROM" keyword.
If no table is found or the sql is empty, it will return null.static SQLTable
extractTableFromSQL(String statement, int startPos)
Extracs the firstSQLTable
from the given sql-statement starting at the position startPos form the statement.static String
extractTableName(String statement, String sql, int startPos)
Gets and extract the tableName from the sql starting from the given startPos.static Map<String,String>
getAllAliases(String sql)
Finds out all aliases within the given sql and returns a map with:
key = alias-name value = original namestatic List<String>
getByRegexGroup(String str, String regex)
Returns all matched groups from the str by the regex-expression.static String
getExecutionPath()
Returns the execution-path of the program, depending on the OS.static String
getLimitValueFromSQL(String sql)
Returns the amount-value from an "limit" within the sql.
For Example select limit 500 * from table,
then it will return "500".
if nothing found it will returnnull
.static String
getOrDefault(String value, String defaultValue)
Returns the value if this is not empty or null, otherwise it return the defaultValue.
This method is only an alternative for chaining string-values together.static String
getRegexForAliasFinding()
static String
getStartsWith(String str, String... startsWiths)
Gets the found startsWith from the array, to check if the str startWith one of these words.
If nothing found it returnsnull
.static List<String>
getStatementsFromSQL(String sql)
Returns all statements, from the given SQL, in a List.static int
indexOf(String str, int startPos, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.static int
indexOf(String str, String regex)
IndexOf-variant that uses regular expressions for searching.static int
indexOf(String str, String regex, boolean toEndOfExpression)
IndexOf-variant that uses regular expressions for searching.static int
indexOf(String str, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.static boolean
isEmpty(String str)
Checks if the string str is null or empty.static boolean
isNotEmpty(String str)
Checks if the string str is not null and not empty.static int
lastIndexOf(String str, int startPos, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.static int
lastIndexOf(String str, String regex)
IndexOf-variant that uses regular expressions for searching.static int
lastIndexOf(String str, String regex, boolean toEndOfExpression)
IndexOf-variant that uses regular expressions for searching.static int
lastIndexOf(String str, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.static boolean
looksLikeUTF8(byte[] pText)
Checks if the given byte-array is like a utf8 stringstatic String
prepareCSV(javafx.collections.ObservableList<SQLTableValue[]> rowValues, Map<String,Object> mapOfConfigs, SQLTable table)
Prepares a csv-string converted by the given row-values.
The csv-metadata like the text-separator comes fromConfigurationExportTableConstants
.static String
prepareSQL(String sql)
Prepares the given SQL.static String
replaceLast(String find, String replace, String string)
static String
replaceRegion(String text, int start, int till, String replacement)
Replaces the region between start and till with the replacement.static String
replaceVariables(String text, Object... objects)
Replaces all variables within the text with the given objects in a loop and returns the result.
For Example:
Input: text="hallo {0} ich bin {1} {2}", objects="hans", "max", "mustermann"
Output: hallo hans ich bin max mustermannstatic String
replaceWhitespaces(String str)
Replaces all whitespaces withing the string str.static boolean
startsWith(String string, String... prefixes)
Checks if the string starts with one of the given suffixes.static String
toJSON(Object obj)
static int
transformToLocalPos(int caretPos, String statement, String sql)
Transforms the position of the statement in relation to the whole sql and its caretPosition.
For example:
Statement1: SELECT * FROM TABLE1;
Statement2: SELECT *[|] FROM TABLE2
The [|] represents the caret position then the caretPosition has a value of 30 but the transformed position will be 9
So the transformed position will always be the local positon of the statement.
-
-
-
Field Detail
-
PATTERN_SQL_COMMENTS
public static final String PATTERN_SQL_COMMENTS
- See Also:
- Constant Field Values
-
patternComments
public static final Pattern patternComments
-
-
Method Detail
-
toJSON
public static String toJSON(Object obj) throws com.fasterxml.jackson.core.JsonProcessingException
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
createTableName
public static String createTableName(String catalog, String schema, String name)
Returns the full tablename with catalog.schema.tablename.- Parameters:
catalog
-schema
-name
-- Returns:
-
createTableName
public static String createTableName(SQLTable table)
Returns the full tablename with catalog.schema.tablename.- Parameters:
table
-- Returns:
-
replaceVariables
public static String replaceVariables(String text, Object... objects)
Replaces all variables within the text with the given objects in a loop and returns the result.
For Example:
Input: text="hallo {0} ich bin {1} {2}", objects="hans", "max", "mustermann"
Output: hallo hans ich bin max mustermann- Parameters:
text
-objects
-- Returns:
-
getAllAliases
public static Map<String,String> getAllAliases(String sql)
Finds out all aliases within the given sql and returns a map with:
key = alias-name value = original name- Parameters:
sql
-- Returns:
-
getRegexForAliasFinding
public static String getRegexForAliasFinding()
-
getOrDefault
public static String getOrDefault(String value, String defaultValue)
Returns the value if this is not empty or null, otherwise it return the defaultValue.
This method is only an alternative for chaining string-values together.- Parameters:
value
-defaultValue
-- Returns:
-
getStatementsFromSQL
public static List<String> getStatementsFromSQL(String sql)
Returns all statements, from the given SQL, in a List.- Parameters:
sql
-- Returns:
-
extractTableFromSQL
public static SQLTable extractTableFromSQL(String sql)
Returns and extract the SQLTable from the given SQL.
The table is always the table after the "FROM" keyword.
If no table is found or the sql is empty, it will return null.- Parameters:
sql
-- Returns:
-
getByRegexGroup
public static List<String> getByRegexGroup(String str, String regex)
Returns all matched groups from the str by the regex-expression.- Parameters:
str
-regex
-- Returns:
-
extractTableFromSQL
public static SQLTable extractTableFromSQL(String statement, int startPos)
Extracs the firstSQLTable
from the given sql-statement starting at the position startPos form the statement.- Parameters:
statement
-startPos
-- Returns:
-
getStartsWith
public static String getStartsWith(String str, String... startsWiths)
Gets the found startsWith from the array, to check if the str startWith one of these words.
If nothing found it returnsnull
.- Parameters:
str
-startsWiths
-- Returns:
-
prepareCSV
public static String prepareCSV(javafx.collections.ObservableList<SQLTableValue[]> rowValues, Map<String,Object> mapOfConfigs, SQLTable table)
Prepares a csv-string converted by the given row-values.
The csv-metadata like the text-separator comes fromConfigurationExportTableConstants
.- Parameters:
rowValues
-mapOfConfigs
-table
-- Returns:
-
extractTableName
public static String extractTableName(String statement, String sql, int startPos)
Gets and extract the tableName from the sql starting from the given startPos.- Parameters:
sql
-startPos
-- Returns:
-
transformToLocalPos
public static int transformToLocalPos(int caretPos, String statement, String sql)
Transforms the position of the statement in relation to the whole sql and its caretPosition.
For example:
Statement1: SELECT * FROM TABLE1;
Statement2: SELECT *[|] FROM TABLE2
The [|] represents the caret position then the caretPosition has a value of 30 but the transformed position will be 9
So the transformed position will always be the local positon of the statement.- Parameters:
caretPos
-statement
-sql
-- Returns:
-
extractAllJoinedTableNames
public static List<String> extractAllJoinedTableNames(String statement)
Gets and extracts all joined tableNames from the sql starting from the given startPos.- Parameters:
statement
-- Returns:
-
isEmpty
public static boolean isEmpty(String str)
Checks if the string str is null or empty.- Parameters:
str
-- Returns:
-
isNotEmpty
public static boolean isNotEmpty(String str)
Checks if the string str is not null and not empty.- Parameters:
str
-- Returns:
-
extractSingleStatement
public static String extractSingleStatement(String sql, int startPos)
Extracts any sql and return a single statement.- Parameters:
sql
- = the whole sql which has to be extracted a single statement of it.startPos
- = for example the caret-position in a text-editor.- Returns:
-
indexOf
public static int indexOf(String str, String regex)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.regex
- = Pattern to locate.- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
indexOf
public static int indexOf(String str, String regex, boolean toEndOfExpression)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.regex
- = Pattern to locate.toEndOfExpression
- = should the index return-value be the start or end of the expression- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
indexOf
public static int indexOf(String str, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.regex
- = Pattern to locate.toEndOfExpression
- = should the index return-value be the start or end of the expressionsubstractExpressionLength
- = should the length of the found group be substracted by the found positionIndex- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
indexOf
public static int indexOf(String str, int startPos, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.startPos
- = Start-positionregex
- = Pattern to locate.toEndOfExpression
- = should the index return-value be the start or end of the expressionsubstractExpressionLength
- = should the length of the found group be substracted by the found positionIndex- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
lastIndexOf
public static int lastIndexOf(String str, String regex)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.regex
- = Pattern to locate.- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
lastIndexOf
public static int lastIndexOf(String str, String regex, boolean toEndOfExpression)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.regex
- = Pattern to locate.toEndOfExpression
- = should the index return-value be the start or end of the expression- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
lastIndexOf
public static int lastIndexOf(String str, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.regex
- = Pattern to locate.toEndOfExpression
- = should the index return-value be the start or end of the expressionsubstractExpressionLength
- = should the length of the found group be substracted by the found positionIndex- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
lastIndexOf
public static int lastIndexOf(String str, int startPos, String regex, boolean toEndOfExpression, boolean substractExpressionLength)
IndexOf-variant that uses regular expressions for searching.- Parameters:
str
- = String in which to search for the pattern.startPos
- = startPositionregex
- = Pattern to locate.toEndOfExpression
- = should the index return-value be the start or end of the expressionsubstractExpressionLength
- = should the length of the found group be substracted by the found positionIndex- Returns:
- The index of the requested pattern, if found; (-1) otherwise.
-
getLimitValueFromSQL
public static String getLimitValueFromSQL(String sql)
Returns the amount-value from an "limit" within the sql.
For Example select limit 500 * from table,
then it will return "500".
if nothing found it will returnnull
.- Parameters:
sql
-- Returns:
-
endsWith
public static boolean endsWith(String string, String... suffixes)
Checks if the string ends with one of the given suffixes.- Parameters:
string
-suffixes
-- Returns:
-
startsWith
public static boolean startsWith(String string, String... prefixes)
Checks if the string starts with one of the given suffixes.- Parameters:
string
-prefixes
-- Returns:
-
getExecutionPath
public static String getExecutionPath()
Returns the execution-path of the program, depending on the OS.- Returns:
-
prepareSQL
public static String prepareSQL(String sql)
Prepares the given SQL. That means to remove all singleline/multiline comments from the SQL.- Parameters:
sql
- = SQL to prepare
-
replaceWhitespaces
public static String replaceWhitespaces(String str)
Replaces all whitespaces withing the string str.- Parameters:
str
-- Returns:
-
replaceRegion
public static String replaceRegion(String text, int start, int till, String replacement)
Replaces the region between start and till with the replacement.- Parameters:
text
-start
-till
-replacement
-- Returns:
-
looksLikeUTF8
public static boolean looksLikeUTF8(byte[] pText)
Checks if the given byte-array is like a utf8 string- Parameters:
pText
-- Returns:
-
-