Class SQLiteDatabase
java.lang.Object
me.github.simonplays15.betterbansystem.core.database.Database
me.github.simonplays15.betterbansystem.core.database.sqlite.SQLiteDatabase
- All Implemented Interfaces:
IDatabase
- Direct Known Subclasses:
CachedSQLiteDatabase
The SQLiteDatabase class represents a database connection and provides methods for
interacting with the database.
It extends the Database class and overrides several methods for executing SQL queries and
transactions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Commits the current transaction.void
Connects to the database using the given connection string, username, and password.void
Creates the necessary database tables if they don't already exist.void
createIndex
(String collectionName, String fieldName, boolean unique) Creates an index on a specified field in a collection.void
Deletes a record from the specified table using the given primary key and value.void
Disconnects from the database by closing the connection.executeQuery
(String queryString) Executes the given SQL query and returns a list of maps containing the query results.void
Inserts a new row into the specified table with the provided data.void
Executes a database query with the given query string.void
Rollbacks the current transaction and restores the auto-commit behavior of the database connection.Executes a SELECT SQL statement on the specified table with the given condition.Retrieves all records from the specified table.void
Starts a transaction by setting the auto commit value to false.void
update
(String tableName, String primaryKey, Object primaryKeyValue, @NotNull Map<String, Object> newData) Updates a record in the specified table.
-
Constructor Details
-
SQLiteDatabase
public SQLiteDatabase()
-
-
Method Details
-
connect
Connects to the database using the given connection string, username, and password.- Parameters:
connectionstring
- the connection string to connect to the databaseusername
- the username to authenticate with the databasepassword
- the password to authenticate with the database
-
createDatabaseAndTables
public void createDatabaseAndTables()Creates the necessary database tables if they don't already exist. -
disconnect
public void disconnect()Disconnects from the database by closing the connection. If the connection is already closed or null, no action is performed. Any exception that occurs during the process will be logged to the global logger. -
insert
Inserts a new row into the specified table with the provided data.- Parameters:
tableName
- the name of the table to insert intodata
- a map containing the column names as keys and the values to insert as values- Throws:
NullPointerException
- if tableName is null or if data is null or contains null keysSQLException
- if an error occurs while executing the SQL statement
-
update
public void update(String tableName, String primaryKey, Object primaryKeyValue, @NotNull @NotNull Map<String, Object> newData) Updates a record in the specified table.- Parameters:
tableName
- the name of the table to update the record inprimaryKey
- the name of the primary key columnprimaryKeyValue
- the value of the primary key for the record to updatenewData
- a map representing the new data to update the record with- Throws:
NullPointerException
- if tableName, primaryKey, primaryKeyValue, or newData is nullSQLException
- if a database access error occurs
-
delete
Deletes a record from the specified table using the given primary key and value.- Parameters:
tableName
- the name of the table to delete fromprimaryKey
- the name of the primary key columnprimaryKeyValue
- the value of the primary key for the record to delete
-
select
Executes a SELECT SQL statement on the specified table with the given condition. -
selectAll
Retrieves all records from the specified table.- Parameters:
tableName
- the name of the table to select records from.- Returns:
- a list of maps representing the records retrieved from the table. Each map contains the column names as keys and the corresponding column values as values.
-
executeQuery
Executes the given SQL query and returns a list of maps containing the query results.- Parameters:
queryString
- the SQL query to execute- Returns:
- a list of maps where each map represents a row from the query result, with column names as keys
-
query
Executes a database query with the given query string.- Parameters:
queryString
- The SQL query string to execute
-
createIndex
Creates an index on a specified field in a collection.- Parameters:
collectionName
- the name of the collectionfieldName
- the name of the field to create the index onunique
- indicates whether the index should be unique or not
-
startTransaction
public void startTransaction()Starts a transaction by setting the auto commit value to false. If an SQLException occurs, it is logged. -
commitTransaction
public void commitTransaction()Commits the current transaction.This method commits the changes made during the current transaction. It calls the
Connection.commit()
method to commit the changes and then sets the auto-commit mode back to true usingConnection.setAutoCommit(boolean)
.If an error occurs during the commit process, an error message will be logged using the
GlobalLogger
class. -
rollbackTransaction
public void rollbackTransaction()Rollbacks the current transaction and restores the auto-commit behavior of the database connection. If an SQLException occurs during the rollback, it is logged as an error.
-