Package com.ptsmods.mysqlw
Class Database
java.lang.Object
com.ptsmods.mysqlw.Database
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addColumn
(String table, String name, ColumnStructure<?> structure, @Nullable String after) Adds a new column to the given table.addColumnAsync
(String table, String name, ColumnStructure<?> structure, @Nullable String after) Adds a new column to the given table asynchronously.void
Calls a procedure with the given parameters.Asynchronously calls a procedure with the given parameters.static <T> T
checkNotNull
(T reference, Object errorMessage) static Database
Makes a new connection to an SQLite database or creates it if it does not yet exist.static Database
Makes a connection to a MySQL database.static Database
connect
(Connection connection) Wraps an SQL connection in a Database.int
Counts columns in a table.int
count
(String table, String what, QueryCondition condition) Counts columns in a table.countAsync
(String table, String what) Counts columns in a table asynchronously.countAsync
(String table, String what, QueryCondition condition) Counts columns in a table asynchronously.void
createIndex
(String table, TableIndex index) Create a new index on an existing column in an existing table.createIndexAsync
(String table, TableIndex index) Create a new index on an existing column in an existing table asynchronously.void
createProcedure
(String name, ProcedureParameter[] parameters, BlockBuilder procedure) Creates a new procedure with the given statements.createProcedureAsync
(String name, ProcedureParameter[] parameters, BlockBuilder procedure) Asynchronously creates a new procedure with the given statements.Creates a new statement.void
createTable
(TablePreset preset) Creates a table from a preset.createTableAsync
(TablePreset preset) Creates a table from a preset asynchronously.void
createTrigger
(String name, String table, TriggeringEvent event, BlockBuilder trigger) Creates a new trigger with the given statements.createTriggerAsync
(String name, String table, TriggeringEvent event, BlockBuilder trigger) Asynchronously creates a new trigger with the given statements.int
delete
(String table, QueryCondition condition) Deletes rows matching the given condition or all when no condition given.int
delete
(String table, QueryCondition condition, int limit) Deletes rows matching the given condition or all when no condition given.deleteAsync
(String table, QueryCondition condition) Deletes rows matching the given condition or all when no condition given asynchronously.deleteAsync
(String table, QueryCondition condition, int limit) Deletes rows matching the given condition or all when no condition given asynchronously.boolean
doLog()
void
Drops (completely removes from existence) a table from this database.Drops (completely removes from existence) a table from this database asynchronously.void
dropColumn
(String table, String column) Drops a column.dropColumnAsync
(String table, String column) Drops a column asynchronouslystatic String
static String
Puts the given String in quotes and escapes any quotes in it to avoid SQL injection.static String
Replaces all single quotes in the string with two single quotes to have MySQL read it as a single quote rather than a string end.static String[]
escapeQuotes
(String[] sa) The same asescapeQuotes(String)
but does it for a whole array of Strings.boolean
Executes a query and returns a boolean value which can mean anything.executeAsync
(String query) Executes a query and returns a boolean value which can mean anything asynchronously.executeQuery
(String query) Executes a query and returns a ResultSet.executeQueryAsync
(String query) Executes a query and returns a ResultSet asynchronously.int
executeUpdate
(String query) Executes a query and returns an integer value which often denotes the amount of rows affected.executeUpdateAsync
(String query) Executes a query and returns an integer value which often denotes the amount of rows affected asynchronously.static String
getAsString
(CharSequence seq) Gets aCharSequence
as a String, in case of aQueryFunction
this returns its function, in case of an asterisk this returns an asterisk, in all other cases this returns the givenCharSequence
but surrounded by graves.static String
Converts an Object to a String to be used in queries.Returns the connection to the dataserver.getCreateQuery
(String table) getCreateQueryAsync
(String table) static @Nullable Database
getDatabase
(Connection connection) Gets the Database that wraps this connection.static Database
getDatabase
(ResultSet set) static <T> T
getFromString
(String s, Class<T> clazz) Converts a StringgetLog()
getName()
Attempts to get the name of the database currently in use.getType()
int
Inserts new data into the table.int
Inserts new data into the table.int
Inserts new data into the table.insertAsync
(String table, String[] columns, Object[] values) Inserts new data into the table asynchronously.insertAsync
(String table, String[] columns, List<Object[]> values) Inserts new data into the table asynchronously.insertAsync
(String table, String column, Object value) Inserts new data into the table asynchronously.insertBuilder
(String table, String... columns) Creates a newInsertBuilder
to build insert queries with.int
insertIgnore
(String table, String[] columns, Object[] values, String keyColumn) Inserts new data or inserts the given key into the keyColumn (which already has that value so it basically ignores it) when a row with the given key already exists.int
insertIgnore
(String table, String column, Object value) Inserts new data or edits old data when a row with the given value for the given column already exists.insertIgnoreAsync
(String table, String[] columns, Object[] values, String keyColumn) Inserts new data or inserts the given key into the keyColumn (which already has that value so it basically ignores it) when a row with the given key already exists asynchronously.insertIgnoreAsync
(String table, String column, Object value) Inserts new data or edits old data when a row with the given value for the given column already exists asynchronously.int
insertUpdate
(String table, String[] columns, Object[] values, Map<String, Object> duplicateValues, String keyColumn) Inserts new data or edits old data when a row with the given key already exists.int
insertUpdate
(String table, String column, Object value, Object duplicateValue) Inserts new data or edits old data when a row with the given value for the given column already exists.insertUpdateAsync
(String table, String[] columns, Object[] values, Map<String, Object> duplicateValues, String keyColumn) Inserts new data or edits old data when a row with the given key already exists asynchronously.insertUpdateAsync
(String table, String column, Object value, Object duplicateValue) Inserts new data or edits old data when a row with the given value for the given column already exists asynchronously.static void
loadConnector
(Database.RDBMS type, @Nullable String version, File file, boolean useCache) Downloads the latest version of the connector for the given type and attempts to add it to the classpath.
It is not recommended you rely on this, but if, for example, you offer your users a choice whether to use MySQL or SQLite and you do not want to make your jar file huge, there is always this option.void
logOrThrow
(String msg, SQLException e) void
modifyColumn
(String table, String name, ColumnStructure<?> structure) Modifies a column's definition.modifyColumnAsync
(String table, String name, ColumnStructure<?> structure) Modifies a column's definition asynchronously.prepareStatement
(String query) Prepares a new statement.static String
Reads a String starting with a single quote until the next quote.static <T> void
registerTypeConverter
(Class<T> clazz, Function<T, String> converterTo, Function<String, T> converterFrom) Register a type converterTo used to determine how to convert an object of the givenClass
to a String which can be used in MySQL queries.int
Replaces data in a table when a row with the same value for the primary key column as the value given already exists.int
Replaces data in a table when a row with the same value for the primary key column as the value given already exists.int
Replaces data in a table when a row with the same value for the primary key column as the value given already exists.replaceAsync
(String table, String[] columns, Object[] values) Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.replaceAsync
(String table, String[] columns, List<Object[]> values) Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.replaceAsync
(String table, String column, Object value) Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.Runs the given runnable on the set executor usingCompletableFuture
s.<T> CompletableFuture<T>
Runs the given supplier on the set executor usingCompletableFuture
s.select
(String table, CharSequence column) Runs a select query and returns parsed output.select
(String table, CharSequence[] columns) Runs a select query and returns parsed output.select
(String table, CharSequence[] columns, QueryCondition condition) Runs a select query and returns parsed output.select
(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output.select
(String table, CharSequence column, QueryCondition condition) Runs a select query and returns parsed output.select
(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output.selectAsync
(String table, CharSequence column) Runs a select query and returns parsed output asynchronously.selectAsync
(String table, CharSequence[] columns) Runs a select query and returns parsed output asynchronously.selectAsync
(String table, CharSequence[] columns, QueryCondition condition) Runs a select query and returns parsed output asynchronously.selectAsync
(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output asynchronously.selectAsync
(String table, CharSequence column, QueryCondition condition) Runs a select query and returns parsed output asynchronously.selectAsync
(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output asynchronously.selectBuilder
(SelectBuilder select) Creates a newSelectBuilder
to use for selecting data.selectBuilder
(String table) Creates a newSelectBuilder
to use for selecting data.selectRaw
(String table, CharSequence column) Runs a select query and returns the raw output.selectRaw
(String table, CharSequence[] columns) Runs a select query and returns the raw output.selectRaw
(String table, CharSequence[] columns, QueryCondition condition) Runs a select query and returns the raw output.selectRaw
(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output.selectRaw
(String table, CharSequence column, QueryCondition condition) Runs a select query and returns the raw output.selectRaw
(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output.selectRawAsync
(String table, CharSequence column) Runs a select query and returns the raw output asynchronously.selectRawAsync
(String table, CharSequence[] columns) Runs a select query and returns the raw output asynchronously.selectRawAsync
(String table, CharSequence[] columns, QueryCondition condition) Runs a select query and returns the raw output asynchronously.selectRawAsync
(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output asynchronously.selectRawAsync
(String table, CharSequence column, QueryCondition condition) Runs a select query and returns the raw output asynchronously.selectRawAsync
(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output asynchronously.selectVariable
(String variable) Selects a single variable stored in the RDBMS.selectVariableAsync
(String variable) Asynchronously selects a single variable stored in the RDBMS.void
setErrorHandler
(Consumer<Throwable> errorHandler) Sets the error handler used whenever an asynchronous call throws an error.void
setExecutor
(Executor executor) Sets theExecutor
used to run tasks asynchronously.void
setLogging
(boolean doLog) Sets whether exceptions should be logged or thrown.
When this is set tofalse
, allSQLException
s will be thrown wrapped in aSilentSQLException
.static <K,
V> Map<K, V> singletonMap
(K key, V value) boolean
tableExists
(String name) Checks if a table exists.tableExistsAsync
(String name) Checks if a table exists asynchronously.toString()
void
Truncates (clears) a table.truncateAsync
(String table) Truncates (clears) a table asynchronously.int
update
(String table, String column, Object value, QueryCondition condition) Updates data in a table.int
Updates data in a table.updateAsync
(String table, String column, Object value, QueryCondition condition) Updates data in a table asynchronously.updateAsync
(String table, Map<String, Object> updates, QueryCondition condition) Updates data in a table asynchronously.
-
Method Details
-
loadConnector
public static void loadConnector(Database.RDBMS type, @Nullable @Nullable String version, File file, boolean useCache) throws IOException Downloads the latest version of the connector for the given type and attempts to add it to the classpath.
It is not recommended you rely on this, but if, for example, you offer your users a choice whether to use MySQL or SQLite and you do not want to make your jar file huge, there is always this option.- Parameters:
type
- The type of the connector to download.version
- The version of the connector to download. If null, automatically downloads the latest one. In case ofMySQL
, this version should correspond with the version of the server you're trying to connect to.file
- The file to download to.useCache
- Whether to use a cached file if the given file already exists. If the given file does not appear to be a connector of the given type, a new version will be downloaded nonetheless.- Throws:
IllegalArgumentException
- If the given type isDatabase.RDBMS.UNKNOWN
.IOException
- If anything went wrong while downloading the file.
-
connect
public static Database connect(String host, int port, String name, String username, String password) throws SQLException Makes a connection to a MySQL database.- Parameters:
host
- The hostname of this database. Often localhostport
- The port this dataserver runs on. Often 3306name
- The name of this database. An attempt to create this database will be made if it does not yet exist.username
- The username to log in with.password
- The password that goes with the username. Can be null if there isn't one.- Returns:
- A Database with which you can do anything.
- Throws:
SQLException
- If an error occurred while either connecting or creating the database.
-
connect
Makes a new connection to an SQLite database or creates it if it does not yet exist.- Parameters:
file
- The database file to connect to.- Returns:
- A Database with which you can do anything.
- Throws:
SQLException
- If an error occurred while either connecting or creating the database.
-
connect
Wraps an SQL connection in a Database. Allows you to connect to any type of database.
THIS FEATURE IS UNSUPPORTED.
- Parameters:
connection
- The connection to wrap.- Returns:
- A new, probably unstable, Database.
- See Also:
-
getDatabase
Gets the Database that wraps this connection.- Parameters:
connection
- The connection- Returns:
- The Database that wraps this connection or null.
-
getDatabase
-
getLog
-
doLog
public boolean doLog() -
setLogging
public void setLogging(boolean doLog) Sets whether exceptions should be logged or thrown.
When this is set tofalse
, allSQLException
s will be thrown wrapped in aSilentSQLException
.- Parameters:
doLog
- Whether to log or throw exceptions.
-
logOrThrow
- Throws:
SilentSQLException
-
getExecutor
- Returns:
- The
Executor
used to run tasks asynchronously.
-
setExecutor
Sets theExecutor
used to run tasks asynchronously.- Parameters:
executor
- The new default executor to use- See Also:
-
getErrorHandler
- Returns:
- The default error handler used whenever an asynchronous call throws an error.
-
setErrorHandler
Sets the error handler used whenever an asynchronous call throws an error.- Parameters:
errorHandler
- The error handler to use
-
getType
-
getName
Attempts to get the name of the database currently in use.- Returns:
- The name of the currently in use database, or the cached name if it could not be gotten.
- Throws:
SilentSQLException
-
getConnection
Returns the connection to the dataserver.- Returns:
- The connection to the dataserver.
-
createStatement
Creates a new statement.DO NOT FORGET TO CLOSE THIS.
- Returns:
- A new statement which must be closed once finished.
- Throws:
SilentSQLException
-
prepareStatement
Prepares a new statement.- Parameters:
query
- The query to use in this statement. Use question marks as argument placeholders.- Returns:
- A prepared statement which can be used to easily insert or update data.
-
runAsync
Runs the given supplier on the set executor usingCompletableFuture
s.- Type Parameters:
T
- The type the given supplier returns.- Parameters:
sup
- The supplier to run.- Returns:
- A invalid @link CompletableFuture.
-
runAsync
Runs the given runnable on the set executor usingCompletableFuture
s.- Parameters:
run
- The runnable to run.- Returns:
- A invalid @link CompletableFuture.
-
count
Counts columns in a table.- Parameters:
table
- The table to count them in.what
- What columns to count.- Returns:
- The amount of results found or
-1
if an error occurred. - Throws:
SilentSQLException
- See Also:
-
count
Counts columns in a table.- Parameters:
table
- The table to count them in.what
- What columns to count.condition
- The condition the row must meet to be counted.- Returns:
- The amount of results found or
-1
if an error occurred. - Throws:
SilentSQLException
- See Also:
-
countAsync
Counts columns in a table asynchronously.- Parameters:
table
- The table to count them in.what
- What columns to count.- Returns:
- The amount of results found or
-1
if an error occurred. - See Also:
-
countAsync
Counts columns in a table asynchronously.- Parameters:
table
- The table to count them in.what
- What columns to count.condition
- The condition the row must meet to be counted.- Returns:
- The amount of results found or
-1
if an error occurred. - See Also:
-
truncate
Truncates (clears) a table.- Parameters:
table
- The table to truncate.- See Also:
-
truncateAsync
Truncates (clears) a table asynchronously.- Parameters:
table
- The table to truncate.- See Also:
-
delete
Deletes rows matching the given condition or all when no condition given.- Parameters:
table
- The table to delete rows fromcondition
- The condition rows must meet in order to be deleted- Returns:
- The amount of rows affected
- See Also:
-
delete
Deletes rows matching the given condition or all when no condition given.- Parameters:
table
- The table to delete rows fromcondition
- The condition rows must meet in order to be deletedlimit
- The maximum amount of rows to delete- Returns:
- The amount of rows affected.
- Throws:
IllegalStateException
- When limit > 0 and type isSQLite
as SQLite does not support delete limits.- See Also:
-
deleteAsync
Deletes rows matching the given condition or all when no condition given asynchronously.- Parameters:
table
- The table to delete rows fromcondition
- The condition rows must meet in order to be deleted- Returns:
- The amount of rows affected.
- See Also:
-
deleteAsync
Deletes rows matching the given condition or all when no condition given asynchronously.- Parameters:
table
- The table to delete rows fromcondition
- The condition rows must meet in order to be deletedlimit
- The maximum amount of rows to delete- Returns:
- The amount of rows affected.
- See Also:
-
selectBuilder
Creates a newSelectBuilder
to use for selecting data.- Parameters:
table
- The table to select from.- Returns:
- A new
SelectBuilder
.
-
selectBuilder
Creates a newSelectBuilder
to use for selecting data.- Parameters:
select
- The select query to select from.- Returns:
- A new
SelectBuilder
.
-
selectVariable
Selects a single variable stored in the RDBMS.- Parameters:
variable
- The name of the variable- Returns:
- The value of the variable
- See Also:
-
selectVariableAsync
Asynchronously selects a single variable stored in the RDBMS.- Parameters:
variable
- The name of the variable- Returns:
- The value of the variable
- See Also:
-
selectRaw
Runs a select query and returns the raw output. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.column
- The column to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRawAsync
Runs a select query and returns the raw output asynchronously. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.column
- The column to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRaw
Runs a select query and returns the raw output. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.column
- The column to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRawAsync
public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence column, QueryCondition condition) Runs a select query and returns the raw output asynchronously. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.column
- The column to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRaw
public ResultSet selectRaw(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.column
- The column to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRawAsync
public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output asynchronously. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.column
- The column to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRaw
Runs a select query and returns the raw output. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.columns
- The columns to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRawAsync
Runs a select query and returns the raw output asynchronously. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.columns
- The columns to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRaw
Runs a select query and returns the raw output. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.columns
- The columns to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRawAsync
public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence[] columns, QueryCondition condition) Runs a select query and returns the raw output asynchronously. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.columns
- The columns to select.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRaw
public ResultSet selectRaw(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.columns
- The columns to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
selectRawAsync
public CompletableFuture<ResultSet> selectRawAsync(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns the raw output asynchronously. The statement used for this query isnot closed
so make sure to close it withset.getStatement().close()
.- Parameters:
table
- The table to select from.columns
- The columns to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- A raw ResultSet that must be closed after use.
- See Also:
-
select
Runs a select query and returns parsed output.- Parameters:
table
- The table to select from.column
- The column to select.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
selectAsync
Runs a select query and returns parsed output asynchronously.- Parameters:
table
- The table to select from.column
- The column to select.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
select
Runs a select query and returns parsed output.- Parameters:
table
- The table to select from.column
- The column to select.condition
- The condition rows must meet in order to be selected.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
selectAsync
public CompletableFuture<SelectResults> selectAsync(String table, CharSequence column, QueryCondition condition) Runs a select query and returns parsed output asynchronously.- Parameters:
table
- The table to select from.column
- The column to select.condition
- The condition rows must meet in order to be selected.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
select
public SelectResults select(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output.- Parameters:
table
- The table to select from.column
- The column to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
selectAsync
public CompletableFuture<SelectResults> selectAsync(String table, CharSequence column, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output asynchronously.- Parameters:
table
- The table to select from.column
- The column to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
select
Runs a select query and returns parsed output.- Parameters:
table
- The table to select from.columns
- The columns to select.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
selectAsync
Runs a select query and returns parsed output asynchronously.- Parameters:
table
- The table to select from.columns
- The columns to select.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
select
Runs a select query and returns parsed output.- Parameters:
table
- The table to select from.columns
- The columns to select.condition
- The condition rows must meet in order to be selected.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
selectAsync
public CompletableFuture<SelectResults> selectAsync(String table, CharSequence[] columns, QueryCondition condition) Runs a select query and returns parsed output asynchronously.- Parameters:
table
- The table to select from.columns
- The columns to select.condition
- The condition rows must meet in order to be selected.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
select
public SelectResults select(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output.- Parameters:
table
- The table to select from.columns
- The columns to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
selectAsync
public CompletableFuture<SelectResults> selectAsync(String table, CharSequence[] columns, QueryCondition condition, QueryOrder order, QueryLimit limit) Runs a select query and returns parsed output asynchronously.- Parameters:
table
- The table to select from.columns
- The columns to select.condition
- The condition rows must meet in order to be selected.order
- What column to order by and in what direction.limit
- The limit of rows returned, including the offset at which these rows are selected from the entire result.- Returns:
- Parsed data in the form of
SelectResults
. - See Also:
-
insertBuilder
Creates a newInsertBuilder
to build insert queries with.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into.- Returns:
- A new
InsertBuilder
.
-
insert
Inserts new data into the table.- Parameters:
table
- The table to insert into.column
- The column to insert a value into.value
- The value to insert into the column.- Returns:
- The amount of rows affected (added).
- See Also:
-
insertAsync
Inserts new data into the table asynchronously.- Parameters:
table
- The table to insert into.column
- The column to insert a value into.value
- The value to insert into the column.- Returns:
- The amount of rows affected (added).
- See Also:
-
insert
Inserts new data into the table.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into.values
- The values to insert into the columns.- Returns:
- The amount of rows affected (added).
- See Also:
-
insertAsync
Inserts new data into the table asynchronously.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into.values
- The values to insert into the columns.- Returns:
- The amount of rows affected (added).
- See Also:
-
insert
Inserts new data into the table.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into.values
- The values to insert into the columns. Each array in this list is a new row to be inserted.- Returns:
- The amount of rows affected (added).
- See Also:
-
insertAsync
public CompletableFuture<Integer> insertAsync(String table, String[] columns, List<Object[]> values) Inserts new data into the table asynchronously.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into.values
- The values to insert into the columns. Each array in this list is a new row to be inserted.- Returns:
- The amount of rows affected (added).
- See Also:
-
insertUpdate
Inserts new data or edits old data when a row with the given value for the given column already exists. It's like replace, but only replaces a column instead of the whole row.- Parameters:
table
- The table to insert into.column
- The column to insert a value into.value
- The value to insert.duplicateValue
- The value to insert when the column already has this value.- Returns:
- The amount of rows affected.
- See Also:
-
insertUpdateAsync
public CompletableFuture<Integer> insertUpdateAsync(String table, String column, Object value, Object duplicateValue) Inserts new data or edits old data when a row with the given value for the given column already exists asynchronously. It's like replace, but only replaces a column instead of the whole row.- Parameters:
table
- The table to insert into.column
- The column to insert a value into.value
- The value to insert.duplicateValue
- The value to insert when the column already has this value.- Returns:
- The amount of rows affected.
- See Also:
-
insertUpdate
public int insertUpdate(String table, String[] columns, Object[] values, Map<String, Object> duplicateValues, String keyColumn) throws SilentSQLExceptionInserts new data or edits old data when a row with the given key already exists. It's like replace, but only replaces a couple columns instead of the whole row.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into, one of these should be aPRIMARY KEY
column.values
- The values to insert into the columns.duplicateValues
- The columns to update and the values to update them with when a row with the given key already exists.keyColumn
- The name of the PRIMARY KEY column. Only has to be set when the type of this Database isSQLite
, can benull
otherwise.- Returns:
- The amount of rows affected.
- Throws:
SilentSQLException
- See Also:
-
insertUpdateAsync
public CompletableFuture<Integer> insertUpdateAsync(String table, String[] columns, Object[] values, Map<String, Object> duplicateValues, String keyColumn) throws SilentSQLExceptionInserts new data or edits old data when a row with the given key already exists asynchronously. It's like replace, but only replaces a couple columns instead of the whole row.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into, one of these should be aPRIMARY KEY
column.values
- The values to insert into the columns.duplicateValues
- The columns to update and the values to update them with when a row with the given key already exists.keyColumn
- The name of the PRIMARY KEY column. Only has to be set when the type of this Database isSQLite
, can benull
otherwise.- Returns:
- The amount of rows affected.
- Throws:
SilentSQLException
- See Also:
-
insertIgnore
Inserts new data or edits old data when a row with the given value for the given column already exists. It's like replace, but only replaces a column instead of the whole row.- Parameters:
table
- The table to insert into.column
- The column to insert a value into. This must be a PRIMARY KEY column.value
- The value to insert.- Returns:
- The amount of rows affected.
- See Also:
-
insertIgnoreAsync
Inserts new data or edits old data when a row with the given value for the given column already exists asynchronously. It's like replace, but only replaces a column instead of the whole row.- Parameters:
table
- The table to insert into.column
- The column to insert a value into. This must be a PRIMARY KEY column.value
- The value to insert.- Returns:
- The amount of rows affected.
- See Also:
-
insertIgnore
Inserts new data or inserts the given key into the keyColumn (which already has that value so it basically ignores it) when a row with the given key already exists.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into, one of these should be aPRIMARY KEY
column.values
- The values to insert into the columns.keyColumn
- The PRIMARY KEY column that's used to determine whether to ignore the insertion. This column should also be present in columns and a value for it should be present in values.- Returns:
- The amount of rows affected.
- See Also:
-
insertIgnoreAsync
public CompletableFuture<Integer> insertIgnoreAsync(String table, String[] columns, Object[] values, String keyColumn) Inserts new data or inserts the given key into the keyColumn (which already has that value so it basically ignores it) when a row with the given key already exists asynchronously.- Parameters:
table
- The table to insert into.columns
- The columns to insert values into, one of these should be aPRIMARY KEY
column.values
- The values to insert into the columns.keyColumn
- The PRIMARY KEY column that's used to determine whether to ignore the insertion. This column should also be present in columns and a value for it should be present in values.- Returns:
- The amount of rows affected.
- See Also:
-
update
Updates data in a table.- Parameters:
table
- The table to update.column
- The column to updatevalue
- The new value of the column.condition
- The condition rows must meet in order to be updated.- Returns:
- The amount of rows affected.
- See Also:
-
updateAsync
public CompletableFuture<Integer> updateAsync(String table, String column, Object value, QueryCondition condition) Updates data in a table asynchronously.- Parameters:
table
- The table to update.column
- The column to updatevalue
- The new value of the column.condition
- The condition rows must meet in order to be updated.- Returns:
- The amount of rows affected.
- See Also:
-
update
Updates data in a table.- Parameters:
table
- The table to update.updates
- The columns and their corresponding values.condition
- The condition rows must meet in order to be updated.- Returns:
- The amount of rows affected.
- See Also:
-
updateAsync
public CompletableFuture<Integer> updateAsync(String table, Map<String, Object> updates, QueryCondition condition) Updates data in a table asynchronously.- Parameters:
table
- The table to update.updates
- The columns and their corresponding values.condition
- The condition rows must meet in order to be updated.- Returns:
- The amount of rows affected.
- See Also:
-
replace
Replaces data in a table when a row with the same value for the primary key column as the value given already exists.- Parameters:
table
- The table to replace rows in.column
- The column to replace.value
- The value to update.- Returns:
- The amount of rows affected.
- See Also:
-
replaceAsync
Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.- Parameters:
table
- The table to replace rows in.column
- The column to replace.value
- The value to update.- Returns:
- The amount of rows affected.
- See Also:
-
replace
Replaces data in a table when a row with the same value for the primary key column as the value given already exists.- Parameters:
table
- The table to replace rows in.columns
- The columns to replace.values
- The values to update.- Returns:
- The amount of rows affected.
- See Also:
-
replaceAsync
Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.- Parameters:
table
- The table to replace rows in.columns
- The columns to replace.values
- The values to update.- Returns:
- The amount of rows affected.
- See Also:
-
replace
Replaces data in a table when a row with the same value for the primary key column as the value given already exists.- Parameters:
table
- The table to replace rows in.columns
- The columns to replace.values
- The values to update.- Returns:
- The amount of rows affected.
- See Also:
-
replaceAsync
public CompletableFuture<Integer> replaceAsync(String table, String[] columns, List<Object[]> values) Replaces data in a table when a row with the same value for the primary key column as the value given already exists asynchronously.- Parameters:
table
- The table to replace rows in.columns
- The columns to replace.values
- The values to update.- Returns:
- The amount of rows affected.
- See Also:
-
drop
Drops (completely removes from existence) a table from this database.- Parameters:
table
- The name of the table to drop.- See Also:
-
dropAsync
Drops (completely removes from existence) a table from this database asynchronously.- Parameters:
table
- The name of the table to drop.- See Also:
-
execute
Executes a query and returns a boolean value which can mean anything. No need to close any statements here.- Parameters:
query
- The query to execute.- Returns:
- A boolean value which can mean anything.
- Throws:
SilentSQLException
- See Also:
-
executeAsync
Executes a query and returns a boolean value which can mean anything asynchronously. No need to close any statements here.- Parameters:
query
- The query to execute.- Returns:
- A boolean value which can mean anything.
- See Also:
-
executeUpdate
Executes a query and returns an integer value which often denotes the amount of rows affected.- Parameters:
query
- The query to execute.- Returns:
- An integer value often denoting the amount of rows affected.
- Throws:
SilentSQLException
- See Also:
-
executeUpdateAsync
Executes a query and returns an integer value which often denotes the amount of rows affected asynchronously.- Parameters:
query
- The query to execute.- Returns:
- An integer value often denoting the amount of rows affected.
- See Also:
-
executeQuery
Executes a query and returns a ResultSet. Most often used with the SELECT query.DO NOT FORGET TO CLOSE THE STATEMENT.
This can be done withset.getStatement().close()
. Not doing so will eventually result in memory leaks.- Parameters:
query
- The query to execute.- Returns:
- The ResultSet containing all the data this query returned.
- Throws:
SilentSQLException
- See Also:
-
executeQueryAsync
Executes a query and returns a ResultSet asynchronously. Most often used with the SELECT query.DO NOT FORGET TO CLOSE THE STATEMENT.
This can be done withset.getStatement().close()
. Not doing so will eventually result in memory leaks.- Parameters:
query
- The query to execute.- Returns:
- The ResultSet containing all the data this query returned.
- See Also:
-
createTable
Creates a table from a preset.- Parameters:
preset
- The preset to build.- See Also:
-
createTableAsync
Creates a table from a preset asynchronously.- Parameters:
preset
- The preset to build.- See Also:
-
tableExists
Checks if a table exists.- Parameters:
name
- The name of the table.- Returns:
- Whether a table by the given name exists.
- Throws:
SilentSQLException
- See Also:
-
tableExistsAsync
Checks if a table exists asynchronously.- Parameters:
name
- The name of the table.- Returns:
- Whether a table by the given name exists.
- See Also:
-
getCreateQuery
- Parameters:
table
- The table to get the creation query of.- Returns:
- The query used to create this table.
- See Also:
-
getCreateQueryAsync
- Parameters:
table
- The table to get the creation query of.- Returns:
- The query used to create this table asynchronously.
- See Also:
-
createIndex
Create a new index on an existing column in an existing table. This is the only way to create indices on SQLite.- Parameters:
table
- The table to create the index on.index
- The index to create.- See Also:
-
createIndexAsync
Create a new index on an existing column in an existing table asynchronously. This is the only way to create indices on SQLite.- Parameters:
table
- The table to create the index on.index
- The index to create.- See Also:
-
addColumn
public void addColumn(String table, String name, ColumnStructure<?> structure, @Nullable @Nullable String after) Adds a new column to the given table.- Parameters:
table
- The table to add the column toname
- The name of the new columnstructure
- The structure of the new columnafter
- The column after which to add this column or null to add it to the beginning.- See Also:
-
addColumnAsync
public CompletableFuture<Void> addColumnAsync(String table, String name, ColumnStructure<?> structure, @Nullable @Nullable String after) Adds a new column to the given table asynchronously.- Parameters:
table
- The table to add the column toname
- The name of the new columnstructure
- The structure of the new columnafter
- The column after which to add this column or null to add it to the beginning.- See Also:
-
modifyColumn
Modifies a column's definition.- Parameters:
table
- The table to modify the column onname
- The name of the column to modifystructure
- The new structure of the column- See Also:
-
modifyColumnAsync
public CompletableFuture<Void> modifyColumnAsync(String table, String name, ColumnStructure<?> structure) Modifies a column's definition asynchronously.- Parameters:
table
- The table to modify the column onname
- The name of the column to modifystructure
- The new structure of the column- See Also:
-
dropColumn
Drops a column.- Parameters:
table
- The table to drop the column fromcolumn
- The column to drop- See Also:
-
dropColumnAsync
Drops a column asynchronously- Parameters:
table
- The table to drop the column fromcolumn
- The column to drop- See Also:
-
createTrigger
Creates a new trigger with the given statements.- Parameters:
name
- The name of the triggertable
- The table to create the trigger onevent
- The event which will trigger this triggertrigger
- The statements to execute when this trigger is triggered- See Also:
-
createTriggerAsync
public CompletableFuture<Void> createTriggerAsync(String name, String table, TriggeringEvent event, BlockBuilder trigger) Asynchronously creates a new trigger with the given statements.- Parameters:
name
- The name of the triggertable
- The table to create the trigger onevent
- The event which will trigger this triggertrigger
- The statements to execute when this trigger is triggered- Returns:
- A CompletableFuture
- See Also:
-
createProcedure
Creates a new procedure with the given statements.- Parameters:
name
- The name of the procedureparameters
- The parameters of the procedureprocedure
- The statements to execute when this procedure is called- See Also:
-
createProcedureAsync
public CompletableFuture<Void> createProcedureAsync(String name, ProcedureParameter[] parameters, BlockBuilder procedure) Asynchronously creates a new procedure with the given statements.- Parameters:
name
- The name of the procedureparameters
- The parameters of the procedureprocedure
- The statements to execute when this procedure is called- Returns:
- A CompletableFuture
- See Also:
-
call
Calls a procedure with the given parameters.- Parameters:
procedure
- The name of the procedure to callparameters
- The parameters to pass when calling the procedure- See Also:
-
callAsync
Asynchronously calls a procedure with the given parameters.- Parameters:
procedure
- The name of the procedure to callparameters
- The parameters to pass when calling the procedure- Returns:
- A CompletableFuture
- See Also:
-
toString
-
enquote
Puts the given String in quotes and escapes any quotes in it to avoid SQL injection.- Parameters:
s
- The String to enquote.- Returns:
- The given String surrounded by quotes.
-
escapeQuotes
Replaces all single quotes in the string with two single quotes to have MySQL read it as a single quote rather than a string end.- Parameters:
s
- The String to escape.- Returns:
- A String in which all single quotes are now two single quotes.
-
engrave
-
escapeQuotes
The same asescapeQuotes(String)
but does it for a whole array of Strings.- Parameters:
sa
- The String array.- Returns:
- The same array given except all Strings have been escaped. Null values in the array are retained.
-
getAsString
Gets aCharSequence
as a String, in case of aQueryFunction
this returns its function, in case of an asterisk this returns an asterisk, in all other cases this returns the givenCharSequence
but surrounded by graves.- Parameters:
seq
- The sequence to get as String.- Returns:
- A String, either a
QueryFunction
's function or the givenCharSequence
surrounded by graves.
-
getAsString
Converts an Object to a String to be used in queries. The default cases are as follows:- Null:
null
- A boolean: String representation of said boolean in uppercase
- Any number: String representation of said number
- Byte array: a hex String representing the given bytes
QueryFunction
: theQueryFunction
's function- Type registered with
registerTypeConverter(Class, Function, Function)
: the result of the registered type converter - Anything else: an
enquoted
String representation
- Parameters:
o
- The object to convert.- Returns:
- A String representation of the given object.
- Null:
-
getFromString
Converts a String- Type Parameters:
T
- The generic type of the object.- Parameters:
s
- The String to parse.clazz
- The type of the object you wish to parse.- Returns:
- An object
-
registerTypeConverter
public static <T> void registerTypeConverter(Class<T> clazz, Function<T, String> converterTo, Function<String, T> converterFrom) Register a type converterTo used to determine how to convert an object of the givenClass
to a String which can be used in MySQL queries.- Type Parameters:
T
- The type of objects to accept.- Parameters:
clazz
- The type of objects this converterTo can accept, objects extending this class must be registered separately.converterTo
- The function accepting the given type and outputting its String representation.
-
readQuotedString
Reads a String starting with a single quote until the next quote.- Parameters:
s
- The String to read.- Returns:
- The String between the first set of quotes found.
-
singletonMap
-
checkNotNull
-