Class Database
java.lang.Object
com.github.tadukoo.database.mysql.Database
A class used to connect to a MySQL database and make queries, updates, etc. to it.
- Since:
- Alpha v.0.1
- Version:
- Alpha v.0.3
- Author:
- Logan Ferree (Tadukoo)
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
The building part of building aDatabase
static class
A Builder to use to build aDatabase
.static interface
The host database name or username part of building aDatabase
static interface
The host URL part of building aDatabase
static interface
Thelogger
part of building aDatabase
static interface
The max attempts or building part of building aDatabase
static interface
The password part of building aDatabase
static interface
The host port or host database or username part of building aDatabase
static interface
The username part of building aDatabase
-
Field Summary
Modifier and TypeFieldDescriptionprivate final String
The MySQL host database nameprivate final String
The MySQL host urlprivate final com.github.tadukoo.util.logger.EasyLogger
Thelogger
to use for loggingprivate final int
The maximum number of attempts to try a SQL transaction before giving upprivate final String
The MySQL password for connecting to the databaseprivate final int
The MySQL host portprivate final String
The MySQL username for connecting to the database -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Database.Logger
builder()
private Connection
connect()
Creates aConnection
to a MySQL database with the url and login information that was set in the constructor of this Database class.<ResultType>
ResultTypeexecuteQuery
(String name, String sql, com.github.tadukoo.util.functional.function.ThrowingFunction<ResultSet, ResultType, SQLException> convertFromResultSet) Executes a sql query after building aQuery
object for it from the given pieces.<ResultType>
ResultTypeexecuteTransaction
(SQLTransaction<ResultType> transaction) Runs a SQL transaction.boolean
executeUpdate
(String name, String sql) Executes a single sql update and returns if it was a success.boolean
Executes sql updates and returns if they were a success.com.github.tadukoo.util.logger.EasyLogger
int
void
insert
(String table, Collection<String> cols, Collection<Object> values) Executes a single sql insert statement.insertAndGetID
(String table, String idColumnName, Collection<String> cols, Collection<Object> values) Executes a sql insert statement and then performs a query to retrieve an id (useful if the ID is auto-incremented).void
update
(String table, Collection<String> cols, Collection<Object> values, Collection<String> whereCols, Collection<Object> whereValues) Executes a single sql update statement
This sends the table, cols, values, whereCols, and whereValues toSQLSyntaxUtil.formatUpdateStatement(String, Collection, Collection, Collection, Collection)
to create the update statement and then usesexecuteUpdate(String, String)
to run it
-
Field Details
-
logger
private final com.github.tadukoo.util.logger.EasyLogger loggerThelogger
to use for logging -
host
The MySQL host url -
port
private final int portThe MySQL host port -
databaseName
The MySQL host database name -
username
The MySQL username for connecting to the database -
password
The MySQL password for connecting to the database -
maxAttempts
private final int maxAttemptsThe maximum number of attempts to try a SQL transaction before giving up
-
-
Constructor Details
-
Database
private Database(com.github.tadukoo.util.logger.EasyLogger logger, String host, int port, String databaseName, String username, String password, int maxAttempts) Constructs a new Database with the given parameters- Parameters:
logger
- Thelogger
to use for logginghost
- The MySQL host urlport
- The MySQL host portdatabaseName
- The MySQL host database nameusername
- The MySQL username for connecting to the databasepassword
- The MySQL password for connecting to the databasemaxAttempts
- The maximum number of attempts to try a SQL transaction before giving up
-
-
Method Details
-
builder
-
getLogger
public com.github.tadukoo.util.logger.EasyLogger getLogger()- Returns:
- The
logger
to use for logging
-
getConnectionURL
- Returns:
- The connection URL (includes host, port, databaseName, but not login credentials)
-
getMaxAttempts
public int getMaxAttempts()- Returns:
- The maximum number of attempts to try a SQL transaction before giving up
-
connect
Creates aConnection
to a MySQL database with the url and login information that was set in the constructor of this Database class.- Returns:
- The Connection that's been created
- Throws:
SQLException
- If anything goes wrong
-
executeTransaction
public <ResultType> ResultType executeTransaction(SQLTransaction<ResultType> transaction) throws SQLException Runs a SQL transaction. Will attemptmaxAttempts
times until it works, before throwing aSQLException
if it doesn't work in that many attempts.- Type Parameters:
ResultType
- The type of result to be returned- Parameters:
transaction
- TheSQLTransaction
to run- Returns:
- The result from the transaction
- Throws:
SQLException
- If anything goes wrong
-
executeQuery
public <ResultType> ResultType executeQuery(String name, String sql, com.github.tadukoo.util.functional.function.ThrowingFunction<ResultSet, ResultType, throws SQLExceptionSQLException> convertFromResultSet) Executes a sql query after building aQuery
object for it from the given pieces. Returns the result of the query.- Type Parameters:
ResultType
- The type of result to be returned- Parameters:
name
- The name to use for the query (for debugging purposes - may be null)sql
- The sql query string to runconvertFromResultSet
- TheThrowingFunction
to use to run the query- Returns:
- The result from the query
- Throws:
SQLException
- If anything goes wrong
-
executeUpdates
public boolean executeUpdates(String transactionName, List<String> names, List<String> sqls) throws SQLException Executes sql updates and returns if they were a success. This version builds theUpdates
object using the given parameters.- Parameters:
transactionName
- The name for the overall transactionnames
- The names to use for the updates (optional - used for debugging)sqls
- The sql update statements to run- Returns:
- If it succeeded or not
- Throws:
SQLException
- If anything goes wrong
-
executeUpdate
Executes a single sql update and returns if it was a success.
This version sends the name and statement tothe plural version
to create theUpdates
object.- Parameters:
name
- The name to use for the update (optional - used for debugging)sql
- The sql update statement to run- Returns:
- If it succeeded or not
- Throws:
SQLException
- If anything goes wrong
-
insert
public void insert(String table, Collection<String> cols, Collection<Object> values) throws SQLException Executes a single sql insert statement.
This sends the table, cols, and values toSQLSyntaxUtil.formatInsertStatement(String, Collection, Collection)
to create the insert statement and then usesexecuteUpdate(String, String)
to run it- Parameters:
table
- The name of the table to insert intocols
- The names of the columns to insert intovalues
- The values to be inserted- Throws:
SQLException
- If anything goes wrong
-
insertAndGetID
public Integer insertAndGetID(String table, String idColumnName, Collection<String> cols, Collection<Object> values) throws SQLException Executes a sql insert statement and then performs a query to retrieve an id (useful if the ID is auto-incremented).
To do this, aInsertAndGetID
transaction object is created using the given parameters- Parameters:
table
- The name of the table to insert intoidColumnName
- The column name for the ID columncols
- The column names for the columns to use in the insertvalues
- The values to be inserted- Returns:
- The ID found from the newly inserted data
- Throws:
SQLException
- If anything goes wrong
-
update
public void update(String table, Collection<String> cols, Collection<Object> values, Collection<String> whereCols, Collection<Object> whereValues) throws SQLException Executes a single sql update statement
This sends the table, cols, values, whereCols, and whereValues toSQLSyntaxUtil.formatUpdateStatement(String, Collection, Collection, Collection, Collection)
to create the update statement and then usesexecuteUpdate(String, String)
to run it- Parameters:
table
- The name of the table to be updatedcols
- The names of the columns to be updatedvalues
- The values to use in the updatewhereCols
- The names of the columns for the where statement (can be empty/null)whereValues
- The values to use for the where statement (can be empty/null)- Throws:
SQLException
- If anything goes wrong
-