Package redempt.redlib.sql
Class SQLHelper
java.lang.Object
redempt.redlib.sql.SQLHelper
- All Implemented Interfaces:
Closeable
,AutoCloseable
Wraps a
Connection
and offers helpful methods that don't need to be surrounded in a try/catch-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
CallsSQLCache.clear()
on all caches owned by this SQLHelpervoid
close()
Closes the underlying connection this SQLHelper wrapsvoid
commit()
Flushes all caches and commits the transactioncreateCache
(String tableName, String columnName, String... primaryKeyNames) Creates and adds cache for a certain columnvoid
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passedint
executeUpdate
(String command, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passedvoid
CallsSQLCache.flush()
on all caches owned by this SQLHelpervoid
flushAndRemoveFromMatchingCaches
(String pattern, Object... primaryKeys) Finds matching caches by a pattern and flushes, then removes a specific entry from them.void
flushMatchingCaches
(String pattern, Object... primaryKeys) Finds matching caches by a pattern and flushes a specific entry from them.getMatchingCaches
(String pattern) Gets the caches matching a patternboolean
static Connection
Opens a connection to a MySQL databasestatic Connection
Opens a connection to a MySQL database at localhost:3306static Connection
openSQLite
(Path file) Opens a SQLite database fileprepareStatement
(String query, Object... fields) Prepares a statement, setting its fields to the elements of the vararg passed<T> List<T>
queryResultList
(String query, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning a list of values in the first column of each row in the resultsqueryResults
(String query, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed.queryResultStringList
(String query, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning a String list of values in the first column of each row in the results<T> T
querySingleResult
(String query, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the resultsbyte[]
querySingleResultBytes
(String query, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the results as a Bytes.querySingleResultLong
(String query, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the results as a Long.querySingleResultString
(String query, Object... fields) Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the results as a String.void
removeFromMatchingCaches
(String pattern, Object... primaryKeys) Finds matching caches by a pattern and removes a specific entry from them.void
setAutoCommit
(boolean autoCommit) Sets the wrapped connection's auto-commit property.void
setCommitInterval
(int ticks) Starts a task to call commit() on this SQLHelper every n ticks.
-
Constructor Details
-
SQLHelper
Constructs a SQLHelper from a Connection. Get the Connection using one of the static SQLHelper open methods.- Parameters:
connection
- The SQL Connection to wrap
-
-
Method Details
-
openSQLite
Opens a SQLite database file- Parameters:
file
- The path to the SQLite database file- Returns:
- The Connection to this SQLite database
-
openMySQL
public static Connection openMySQL(String ip, int port, String username, String password, String database) Opens a connection to a MySQL database- Parameters:
ip
- The IP address to connect toport
- The port to connect tousername
- The username to log in withpassword
- The password to log in withdatabase
- The database to use, will be created if it doesn't exist- Returns:
- The Connection to the MySQL database
-
openMySQL
Opens a connection to a MySQL database at localhost:3306- Parameters:
username
- The username to log in withpassword
- The password to log in withdatabase
- The database to use, will be created if it doesn't exist- Returns:
- The Connection to the MySQL database
-
createCache
Creates and adds cache for a certain column- Parameters:
tableName
- The name of the table to create the cache forcolumnName
- The name of the column to create the cache forprimaryKeyNames
- The primary keys used to access and mutate the column- Returns:
- The cache
-
flushMatchingCaches
Finds matching caches by a pattern and flushes a specific entry from them.- Parameters:
pattern
- The pattern used forgetMatchingCaches(String)
primaryKeys
- The primary keys used to access the entry
-
removeFromMatchingCaches
Finds matching caches by a pattern and removes a specific entry from them. Useful for saving targeted cached rows when a column in a certain table is changed- Parameters:
pattern
- The pattern used forgetMatchingCaches(String)
primaryKeys
- The primary keys used to access the entry
-
flushAndRemoveFromMatchingCaches
Finds matching caches by a pattern and flushes, then removes a specific entry from them.- Parameters:
pattern
- The pattern used forgetMatchingCaches(String)
primaryKeys
- The primary keys used to access the entry
-
getMatchingCaches
Gets the caches matching a pattern- Parameters:
pattern
- The pattern to match. Should be formatted as "tableName.primaryKeyColumnName". Use * to indicate all for either tableName or columnName. Use | to indicate or. Primary key column name matches any primary key with the given column name. Useful if you are updating a value in a table and want to flush/remove targeted values from the cache. Example: *.name|team- Returns:
- The list of matching caches
-
getCaches
- Returns:
- The list of caches for this SQLHelper
-
flushAllCaches
public void flushAllCaches()CallsSQLCache.flush()
on all caches owned by this SQLHelper -
clearAllCaches
public void clearAllCaches()CallsSQLCache.clear()
on all caches owned by this SQLHelper -
execute
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed- Parameters:
command
- The SQL command to executefields
- A vararg of the fields to set in the prepared statement
-
executeUpdate
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed- Parameters:
command
- The SQL command to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- The number of updated rows
-
querySingleResult
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the results- Type Parameters:
T
- The type to cast the return value to- Parameters:
query
- The SQL query to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- The value in the first column of the first row of the returned results, or null if none is present
-
querySingleResultString
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the results as a String.- Parameters:
query
- The SQL query to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- The String in the first column of the first row of the returned results, or null if none is present
Note: This method exists because
ResultSet.getObject(int)
can return an Integer if the String in the column can be parsed into one.
-
querySingleResultBytes
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the results as a Bytes.- Parameters:
query
- The SQL query to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- The bytes in the first column of the first row of the returned results, or null if none is present
-
querySingleResultLong
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning the value in the first column of the first row in the results as a Long.- Parameters:
query
- The SQL query to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- The String in the first column of the first row of the returned results, or null if none is present
Note: This method exists because
ResultSet.getObject(int)
can return an Integer if the Long in the column can be parsed into one.
-
queryResultList
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning a list of values in the first column of each row in the results- Type Parameters:
T
- The type to populate the list with and return- Parameters:
query
- The SQL query to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- A list of the value in the first column of each row returned by the query
-
queryResultStringList
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed, returning a String list of values in the first column of each row in the results- Parameters:
query
- The SQL query to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- A String list of the value in the first column of each row returned by the query
Note: This method exists because
ResultSet.getObject(int)
can return an Integer if the String in the column can be parsed into one.
-
queryResults
Executes a SQL query as a prepared statement, setting its fields to the elements of the vararg passed. Returns aSQLHelper.Results
, which wraps aResultSet
for easier use- Parameters:
query
- The SQL query to executefields
- A vararg of the fields to set in the prepared statement- Returns:
- The results of the query
-
getConnection
- Returns:
- The Connection this SQLHelper wraps
-
setAutoCommit
public void setAutoCommit(boolean autoCommit) Sets the wrapped connection's auto-commit property. Calling this method will automatically disable the task started bysetCommitInterval(int)
.- Parameters:
autoCommit
- The auto-commit property - whether it will commit with every command
-
isAutoCommit
public boolean isAutoCommit()- Returns:
- The auto-commit property of the wrapped connection
-
setCommitInterval
public void setCommitInterval(int ticks) Starts a task to call commit() on this SQLHelper every n ticks. Pass -1 to disable. Automatically sets autoCommit to false.- Parameters:
ticks
- The number of ticks between commits, or -1 to disable
-
commit
public void commit()Flushes all caches and commits the transaction -
prepareStatement
Prepares a statement, setting its fields to the elements of the vararg passed- Parameters:
query
- The SQL query to preparefields
- A vararg of the fields to set in the prepared statement- Returns:
- The PreparedStatement with its fields set
-
close
public void close()Closes the underlying connection this SQLHelper wraps- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-