Class AutoCatchingSQLConnection<T>
java.lang.Object
com.marcpg.libpg.data.database.sql.SQLConnection<T>
com.marcpg.libpg.data.database.sql.AutoCatchingSQLConnection<T>
- Type Parameters:
T
- The primary key's type.
- Direct Known Subclasses:
DummySQLConnection
Modified version of the
SQLConnection
, but with the key difference
of throwing no exceptions and instead handling them automatically inside the
methods, using a code snipped provided in the constructor or optionally modified
using changeExceptionHandling(Consumer)
. Can break if the code if the
way of handling the exceptions is invalid.- Since:
- 0.0.6
-
Nested Class Summary
Nested classes/interfaces inherited from class com.marcpg.libpg.data.database.sql.SQLConnection
SQLConnection.DatabaseType
-
Constructor Summary
ConstructorDescriptionAutoCatchingSQLConnection
(SQLConnection.DatabaseType type, String ip, int port, String databaseName, String username, String password, String table, String primaryKeyName, Consumer<SQLException> exceptionHandling) Creates a connection to a SQL compatible database, that automatically catches SQL exceptions.AutoCatchingSQLConnection
(SQLConnection.DatabaseType type, String url, String username, String password, String table, String primaryKeyName, Consumer<SQLException> exceptionHandling) Creates a connection to a SQL compatible database, that automatically catches SQL exceptions. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts a new row with the specified values into the table.void
changeExceptionHandling
(Consumer<SQLException> exceptionHandling) Changes what should be done with theSQLException
s, when they are caught.void
Closes the connection to the database.boolean
Checks whether the table contains an entry/row with the primary key or not.void
Generates a new statement to use for queries.<T2> T2
executeQuery
(String sql, Object @NotNull ... params) Executes a parameterized SQL query and returns the result.Get a specified column, based on the primary key and the column index.Get a specified column, based on the primary key and the column name.Get all rows of the table, where thelist
represents the rows and the arrays are each row's columns.Get theresult
of getting a row based on the primary key.Object[]
getRowArray
(T primaryKey) Get a whole row from the table based on the primary key.getRowArraysContaining
(Object object, String... checkedColumns) Get all rows of the table containing a specific value in any specified column as a List of arrays, where thelist
represents the rows and the arrays are each row's columns.Collection<Object[]>
getRowArraysMatching
(String wherePredicate, Object @NotNull ... replacements) Get all rows matching a specified WHERE predicate as a Collection of arrays, where thelist
represents the rows and the arrays are each row's columns with their name first and then their value.Get a whole row with column names and values from the table based on the primary key.getRowMapsContaining
(Object object, String... checkedColumns) getRowMapsMatching
(String wherePredicate, Object @NotNull ... replacements) Get all rows matching a specified WHERE predicate as a Collection of arrays, where thecollection
represents the rows and the arrays are each row's columns with their name first and then their value.void
Removed a row based on the primary key.void
Set a value of a specified row to a new value, based on the primary key and the column index.void
Set a value of a specified row to a new value, based on the primary key and the column name.Methods inherited from class com.marcpg.libpg.data.database.sql.SQLConnection
changeTable, connection, executeQuery, get, get, getAllRowArrays, getAllRowMaps, getRow, getRowArray, getRowArraysContaining, getRowArraysMatching, getRowMap, getRowMapsContaining, getRowMapsMatching, primaryKeyName, table
-
Constructor Details
-
AutoCatchingSQLConnection
public AutoCatchingSQLConnection(SQLConnection.DatabaseType type, String url, String username, String password, String table, String primaryKeyName, Consumer<SQLException> exceptionHandling) throws SQLException, ClassNotFoundException Creates a connection to a SQL compatible database, that automatically catches SQL exceptions.- Parameters:
type
- The database type.url
- The URL to the database (jdbc:type://ip:port/name)username
- Username of the account to access the database with.password
- Password of the account to access the database with.table
- The table in the database that will accessed. Can be changed later on usingSQLConnection.changeTable(String)
.primaryKeyName
- The primary key's name.exceptionHandling
- What should be done with exceptions, in the rare case of them occurring.- Throws:
SQLException
- if the connection wasn't successful, which is likely due to a wrong URL or wrong credentials.ClassNotFoundException
- if the dependency of the database type is missing.- See Also:
-
AutoCatchingSQLConnection
public AutoCatchingSQLConnection(SQLConnection.DatabaseType type, String ip, int port, String databaseName, String username, String password, String table, String primaryKeyName, Consumer<SQLException> exceptionHandling) throws SQLException, ClassNotFoundException Creates a connection to a SQL compatible database, that automatically catches SQL exceptions.- Parameters:
type
- The database type.ip
- The IP of the database. Can be localhost.port
- The port that the database runs on. If set to 0, will use the database type'sdefault port
.databaseName
- The database to name, this should connect to.username
- Username of the account to access the database with.password
- Password of the account to access the database with.table
- The table in the database that will accessed. Can be changed later on usingSQLConnection.changeTable(String)
.primaryKeyName
- The primary key's name.exceptionHandling
- What should be done with exceptions, in the rare case of them occurring.- Throws:
SQLException
- if the connection wasn't successful, which is likely due to a wrong URL or wrong credentials.ClassNotFoundException
- if the dependency of the database type is missing.- See Also:
-
-
Method Details
-
closeConnection
public void closeConnection()Description copied from class:SQLConnection
Closes the connection to the database. Recommended to use this before shutting down the program, to not cause any connection issues with the database.- Overrides:
closeConnection
in classSQLConnection<T>
-
createNewStatement
public void createNewStatement()Description copied from class:SQLConnection
Generates a new statement to use for queries. Can be useful for clearing some memory.- Overrides:
createNewStatement
in classSQLConnection<T>
-
changeExceptionHandling
Changes what should be done with theSQLException
s, when they are caught. No usage case for normal users.- Parameters:
exceptionHandling
- The new way of handling theSQLException
s.
-
executeQuery
Description copied from class:SQLConnection
Executes a parameterized SQL query and returns the result. When the result has multiple values, it will choose the first column index.- Overrides:
executeQuery
in classSQLConnection<T>
- Type Parameters:
T2
- The type of the result to be returned.- Parameters:
sql
- The parameterized query to be executed.params
- The parameters that should be set into the sql query input.- Returns:
- The result of the query execution, or
null
if it's empty.
-
getRowArray
Description copied from class:SQLConnection
Get a whole row from the table based on the primary key.- Overrides:
getRowArray
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to get the row from.- Returns:
- All
objects
of the row as anarray
.
-
getRowMap
Description copied from class:SQLConnection
Get a whole row with column names and values from the table based on the primary key.- Overrides:
getRowMap
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to get the row from.- Returns:
- All
objects
of the row as amap
.
-
getRow
Description copied from class:SQLConnection
Get theresult
of getting a row based on the primary key.- Overrides:
getRow
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to get the row from.- Returns:
- The
result
of the query.
-
get
Description copied from class:SQLConnection
Get a specified column, based on the primary key and the column name.- Overrides:
get
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to get the value from.column
- The column/value's name.- Returns:
- The
value
in the specified field.
-
get
Description copied from class:SQLConnection
Get a specified column, based on the primary key and the column index.- Overrides:
get
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to get the value from.column
- The column/value's index (starting at 1).- Returns:
- The
value
in the specified field.
-
set
Description copied from class:SQLConnection
Set a value of a specified row to a new value, based on the primary key and the column name.- Overrides:
set
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to get the value from.column
- The column/value's name.newValue
- The new value for the specified field.
-
set
Description copied from class:SQLConnection
Set a value of a specified row to a new value, based on the primary key and the column index.- Overrides:
set
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to get the value from.column
- The column/value's index (starting at 1).newValue
- The new value for the specified field.
-
add
Description copied from class:SQLConnection
Inserts a new row with the specified values into the table.- Overrides:
add
in classSQLConnection<T>
- Parameters:
values
- All values for the new row. The keys are the column names and the values are the values. Has to be valid according to database settings, otherwise throws anException
.
-
remove
Description copied from class:SQLConnection
Removed a row based on the primary key.- Overrides:
remove
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to remove.
-
contains
Description copied from class:SQLConnection
Checks whether the table contains an entry/row with the primary key or not.- Overrides:
contains
in classSQLConnection<T>
- Parameters:
primaryKey
- The primary key to check for.- Returns:
- true if the table contains the primary key, false otherwise.
-
getAllRowArrays
Description copied from class:SQLConnection
Get all rows of the table, where thelist
represents the rows and the arrays are each row's columns.- Overrides:
getAllRowArrays
in classSQLConnection<T>
- Returns:
- A
list
containing all columns as an array.
-
getAllRowMaps
Description copied from class:SQLConnection
Get all rows of the table, where thelist
represents the rows and themaps
are each row's columns with their name first and then their value.- Overrides:
getAllRowMaps
in classSQLConnection<T>
- Returns:
- A
map
containing all columns with their names and values.
-
getRowArraysContaining
Description copied from class:SQLConnection
Get all rows of the table containing a specific value in any specified column as a List of arrays, where thelist
represents the rows and the arrays are each row's columns.- Overrides:
getRowArraysContaining
in classSQLConnection<T>
- Parameters:
object
- What object must be contained in at least one of the rows.checkedColumns
- All columns that are checked for the object.- Returns:
- A
list
containing all columns as an array.
-
getRowMapsContaining
Description copied from class:SQLConnection
Get all rows of the table containing a specific value in any specified column as a List of maps, where thelist
represents the rows and themaps
are each row's columns with their name first and then their value.- Overrides:
getRowMapsContaining
in classSQLConnection<T>
- Parameters:
object
- What object must be contained in at least one of the rows.checkedColumns
- All columns that are checked for the object.- Returns:
- A
map
containing all columns with their names and values.
-
getRowArraysMatching
public Collection<Object[]> getRowArraysMatching(String wherePredicate, Object @NotNull ... replacements) Description copied from class:SQLConnection
Get all rows matching a specified WHERE predicate as a Collection of arrays, where thelist
represents the rows and the arrays are each row's columns with their name first and then their value.- Overrides:
getRowArraysMatching
in classSQLConnection<T>
- Parameters:
wherePredicate
- The SQLWHERE
predicate, likecolumn = ? AND another_column = ?
. This is inserted right after theWHERE
.replacements
- What objects the question marks should be replaced with.- Returns:
- All rows that match the WHERE predicate as arrays.
-
getRowMapsMatching
public Collection<Map<String,Object>> getRowMapsMatching(String wherePredicate, Object @NotNull ... replacements) Description copied from class:SQLConnection
Get all rows matching a specified WHERE predicate as a Collection of arrays, where thecollection
represents the rows and the arrays are each row's columns with their name first and then their value.- Overrides:
getRowMapsMatching
in classSQLConnection<T>
- Parameters:
wherePredicate
- The SQLWHERE
predicate, likecolumn = ? AND another_column = ?
. This is inserted right after theWHERE
.replacements
- What objects the question marks should be replaced with.- Returns:
- All rows that match the WHERE predicate as arrays.
-