Package net.hectus.sql
Class AutoCatchingPostgreConnection
java.lang.Object
net.hectus.sql.PostgreConnection
net.hectus.sql.AutoCatchingPostgreConnection
Modified version of the
PostgreConnection
, 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 for
handling the exceptions isn't proper.-
Constructor Summary
ConstructorDescriptionAutoCatchingPostgreConnection
(String ip, int port, String databaseName, String username, String password, String table, Consumer<SQLException> exceptionHandling) AutoCatchingPostgreConnection
(String url, String username, String password, String table, Consumer<SQLException> exceptionHandling) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts a new row with a player and the 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 for the player or not.void
Generates a new statement to use for queries.<T> T
executeQuery
(String sql, Object @NotNull ... params) Executes a parameterized SQL query and returns the result.Get a value from a specific player's data, based on a playerUUID
and the column index.Get a value from a specific player's data, based on a playerUUID
and the column name.Object[]
getRowArray
(UUID uuid) Get a whole row from the table based on a playerUUID
.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.Get a whole row with column names and values from the table based on a playerUUID
.getRowMapsContaining
(Object object, String... checkedColumns) void
Removed a player's row based on hisUUID
.void
Set a value from a specific player's data to a new value, based on a playerUUID
and the column index.void
Set a value from a specific player's data to a new value, based on a playerUUID
and the column name.Methods inherited from class net.hectus.sql.PostgreConnection
changeTable, connection, table
-
Constructor Details
-
AutoCatchingPostgreConnection
public AutoCatchingPostgreConnection(String url, String username, String password, String table, Consumer<SQLException> exceptionHandling) throws SQLException - Throws:
SQLException
-
AutoCatchingPostgreConnection
public AutoCatchingPostgreConnection(String ip, int port, String databaseName, String username, String password, String table, Consumer<SQLException> exceptionHandling) throws SQLException - Throws:
SQLException
-
-
Method Details
-
closeConnection
public void closeConnection()Description copied from class:PostgreConnection
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 classPostgreConnection
-
createNewStatement
public void createNewStatement()Description copied from class:PostgreConnection
Generates a new statement to use for queries. Can be useful for clearing some memory.- Overrides:
createNewStatement
in classPostgreConnection
-
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:PostgreConnection
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 classPostgreConnection
- Type Parameters:
T
- 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:PostgreConnection
Get a whole row from the table based on a playerUUID
.- Overrides:
getRowArray
in classPostgreConnection
- Parameters:
uuid
- TheUUID
of the player to get the row from.- Returns:
- All
objects
of the row as anarray
.
-
getRowMap
Description copied from class:PostgreConnection
Get a whole row with column names and values from the table based on a playerUUID
.- Overrides:
getRowMap
in classPostgreConnection
- Parameters:
uuid
- TheUUID
of the player to get the row from.- Returns:
- All
objects
of the row as amap
.
-
getRow
Description copied from class:PostgreConnection
- Overrides:
getRow
in classPostgreConnection
- Parameters:
uuid
- TheUUID
of the player to get the row from.- Returns:
- The
result
of the query.
-
get
Description copied from class:PostgreConnection
Get a value from a specific player's data, based on a playerUUID
and the column name.- Overrides:
get
in classPostgreConnection
- Parameters:
uuid
- TheUUID
of the player to get the value from.column
- The column/value's name.- Returns:
- The
value
in the specified field.
-
get
Description copied from class:PostgreConnection
Get a value from a specific player's data, based on a playerUUID
and the column index.- Overrides:
get
in classPostgreConnection
- Parameters:
uuid
- TheUUID
of the player 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:PostgreConnection
Set a value from a specific player's data to a new value, based on a playerUUID
and the column name.- Overrides:
set
in classPostgreConnection
- Parameters:
uuid
- TheUUID
of the player to get the value from.column
- The column/value's name.newValue
- The new value for the specified field.
-
set
Description copied from class:PostgreConnection
Set a value from a specific player's data to a new value, based on a playerUUID
and the column index.- Overrides:
set
in classPostgreConnection
- Parameters:
uuid
- TheUUID
of the player 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:PostgreConnection
Inserts a new row with a player and the values into the table.- Overrides:
add
in classPostgreConnection
- Parameters:
uuid
- The player'sUUID
, which is the first column.values
- All other values for the new row. Has to be valid according to database settings, otherwise will throw the followingException
.
-
remove
Description copied from class:PostgreConnection
Removed a player's row based on hisUUID
.- Overrides:
remove
in classPostgreConnection
- Parameters:
uuid
- The player'sUUID
.
-
contains
Description copied from class:PostgreConnection
Checks whether the table contains an entry/row for the player or not.- Overrides:
contains
in classPostgreConnection
- Parameters:
uuid
- The player'sUUID
.- Returns:
- true if the table contains the player's
UUID
, false otherwise.
-
getRowArraysContaining
Description copied from class:PostgreConnection
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 classPostgreConnection
- 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:PostgreConnection
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 themaps
are each row's columns with their name first and then their value.- Overrides:
getRowMapsContaining
in classPostgreConnection
- 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.
-