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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe building part of building aDatabasestatic classA Builder to use to build aDatabase.static interfaceThe host database name or username part of building aDatabasestatic interfaceThe host URL part of building aDatabasestatic interfaceTheloggerpart of building aDatabasestatic interfaceThe max attempts or building part of building aDatabasestatic interfaceThe password part of building aDatabasestatic interfaceThe host port or host database or username part of building aDatabasestatic interfaceThe username part of building aDatabase - 
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringThe MySQL host database nameprivate final StringThe MySQL host urlprivate final com.github.tadukoo.util.logger.EasyLoggerTheloggerto use for loggingprivate final intThe maximum number of attempts to try a SQL transaction before giving upprivate final StringThe MySQL password for connecting to the databaseprivate final intThe MySQL host portprivate final StringThe MySQL username for connecting to the database - 
Constructor Summary
Constructors - 
Method Summary
Modifier and TypeMethodDescriptionstatic Database.Loggerbuilder()private Connectionconnect()Creates aConnectionto 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 aQueryobject for it from the given pieces.<ResultType>
ResultTypeexecuteTransaction(SQLTransaction<ResultType> transaction) Runs a SQL transaction.booleanexecuteUpdate(String name, String sql) Executes a single sql update and returns if it was a success.booleanExecutes sql updates and returns if they were a success.com.github.tadukoo.util.logger.EasyLoggerintvoidinsert(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).voidupdate(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 loggerTheloggerto 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- Theloggerto 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 
loggerto 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 aConnectionto 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 attemptmaxAttemptstimes until it works, before throwing aSQLExceptionif it doesn't work in that many attempts.- Type Parameters:
 ResultType- The type of result to be returned- Parameters:
 transaction- TheSQLTransactionto 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 aQueryobject 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- TheThrowingFunctionto 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 theUpdatesobject 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 versionto create theUpdatesobject.- 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, aInsertAndGetIDtransaction 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
 
 -