Class BaseService<T extends BaseEntity>
- java.lang.Object
-
- com.github.collinalpert.java2db.services.BaseService<T>
-
- Direct Known Subclasses:
BaseCodeAndDescriptionService
public class BaseService<T extends BaseEntity> extends java.lang.ObjectClass that provides base functionality for all service classes. Every service class must extend this class.- Author:
- Collin Alpert
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseService()Constructor for the base class of all services.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanany()Checks if a table has at least one row.booleanany(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Checks if a value matching the condition exists in the table.longcount()An overload of thecount(SqlPredicate)method.longcount(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Counts the rows that have an id (which should usually be every row) and match a certain condition.voidcreate(java.util.List<T> instances)Creates multiple entities on the database.longcreate(T instance)Creates this Java entity on the database.voidcreate(T... instances)Creates a variable amount of entities on the database.PaginationResult<T>createPagination(int entriesPerPage)Creates a pagination structure that splits the entire table into multiple pages.CacheablePaginationResult<T>createPagination(int entriesPerPage, java.time.Duration cacheExpiration)Creates a pagination structure that splits the entire table into multiple pages.PaginationResult<T>createPagination(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate, int entriesPerPage)Creates a pagination structure that splits the result of a query into multiple pages.CacheablePaginationResult<T>createPagination(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate, int entriesPerPage, java.time.Duration cacheExpiration)Creates a cached pagination structure that splits the result of a query into multiple pages.protected Query<T>createQuery()voiddelete(long id)Deletes a row by an id.voiddelete(long... ids)Deletes multiple rows with the corresponding ids.voiddelete(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Deletes rows based on a condition.voiddelete(java.util.List<T> entities)Deletes a list of entities at once.voiddelete(T instance)Deletes the corresponding row on the database.voiddelete(T... entities)Deletes multiple entities at once.java.util.List<T>getAll()java.util.List<T>getAll(int limit)Gets all values from the table but limits the result.java.util.List<T>getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy)Gets all values from the table and orders them in an ascending order.java.util.List<T>getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy, int limit)Gets all values from the table, orders them in an ascending order and limits the result.java.util.List<T>getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy, OrderTypes sortingType)Gets all values from the table and orders them in the specified order.java.util.List<T>getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy, OrderTypes sortingType, int limit)Gets all values from the table, orders them in a specific order and limits the result.java.util.Optional<T>getById(long id)protected Query<T>getMultiple(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Retrieves list of entities which match the predicate.protected java.util.Optional<T>getSingle(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Retrieves a single entity which matches the predicate.booleanhasDuplicates(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> column)Checks if duplicate values exist for a specific column in a table.voidtruncateTable()Truncates the corresponding table on the database.<R> voidupdate(long entityId, com.github.collinalpert.lambda2sql.functions.SqlFunction<T,R> column, R newValue)Updates a specific column for a record in a table.voidupdate(T instance)Updates this entity's row on the database.
-
-
-
Method Detail
-
create
public long create(T instance) throws java.sql.SQLException
Creates this Java entity on the database.- Parameters:
instance- The instance to create on the database.- Returns:
- The id of the newly created record.
- Throws:
java.sql.SQLException- if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type or a foreign key constraint.
-
create
@SafeVarargs public final void create(T... instances) throws java.sql.SQLException
Creates a variable amount of entities on the database.- Parameters:
instances- The instances to create.- Throws:
java.sql.SQLException- if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type or a foreign key constraint.- See Also:
create(List)
-
create
public void create(java.util.List<T> instances) throws java.sql.SQLException
Creates multiple entities on the database. It is recommended to use this method instead of iterating over the list and calling a normalcreate(BaseEntity)on each entity separately.- Parameters:
instances- The list of entities to create on the database.- Throws:
java.sql.SQLException- if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type or a foreign key constraint.
-
count
public long count()
An overload of thecount(SqlPredicate)method. It will count all the rows that have an id, since this should be a reasonable criteria. Use with caution, as this call can take quite a while.- Returns:
- The amount of rows in this table.
-
count
public long count(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Counts the rows that have an id (which should usually be every row) and match a certain condition. Note that COUNT operations usually are very slow.- Parameters:
predicate- The condition to test for.- Returns:
- The number of rows matching the condition.
-
any
public boolean any()
Checks if a table has at least one row.- Returns:
Trueif at least one row exists in the table,falseif not.
-
any
public boolean any(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Checks if a value matching the condition exists in the table.- Parameters:
predicate- The condition to check for.- Returns:
Trueif the predicate matches one or more records,falseif not.
-
hasDuplicates
public boolean hasDuplicates(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> column)
Checks if duplicate values exist for a specific column in a table.- Parameters:
column- The column to check for duplicate values for.- Returns:
Trueif there is at least one duplicate value in the specified column,falseotherwise.
-
createQuery
protected Query<T> createQuery()
- Returns:
- a
Queryobject with which a DQL statement can be built, using operations like order by, limit etc. If you do not a plainQuery, please consider using thegetSingle(SqlPredicate),getMultiple(SqlPredicate)orgetAll()methods.
-
getSingle
protected java.util.Optional<T> getSingle(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Retrieves a single entity which matches the predicate. It isprotectedas it is only meant for use in methods of the respective service. This is to keep good programming practice and create descriptive methods for what kind of data you are getting.- Parameters:
predicate- TheSqlPredicateto add constraints to a DQL query.- Returns:
- An entity matching the result of the query.
-
getMultiple
protected Query<T> getMultiple(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Retrieves list of entities which match the predicate. It isprotectedas it is only meant for use in methods of the respective service. This is to keep good programming practice and create descriptive methods for what kind of data you are getting.- Parameters:
predicate- TheSqlPredicateto add constraints to a DQL statement.- Returns:
- A list of entities matching the result of the query.
-
getById
public java.util.Optional<T> getById(long id)
- Parameters:
id- The id of the desired entity.- Returns:
- Gets an entity by its id.
-
getAll
public java.util.List<T> getAll()
- Returns:
- All entities in this table.
-
getAll
public java.util.List<T> getAll(int limit)
Gets all values from the table but limits the result.- Parameters:
limit- The maximum of records to return.- Returns:
- A list with the maximum size of the parameter specified.
-
getAll
public java.util.List<T> getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy)
Gets all values from the table and orders them in an ascending order.- Parameters:
orderBy- The property to order by.- Returns:
- A list of all records ordered by a specific property in an ascending order.
-
getAll
public java.util.List<T> getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy, OrderTypes sortingType)
Gets all values from the table and orders them in the specified order.- Parameters:
orderBy- The property to order by.sortingType- The order direction. Can be either ascending or descending.- Returns:
- A list of all records ordered by a specific property in the specified order.
-
getAll
public java.util.List<T> getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy, int limit)
Gets all values from the table, orders them in an ascending order and limits the result.- Parameters:
orderBy- The property to order by.limit- The maximum records to return.- Returns:
- A list with the maximum size of the parameter specified and in an ascending order.
-
getAll
public java.util.List<T> getAll(com.github.collinalpert.lambda2sql.functions.SqlFunction<T,?> orderBy, OrderTypes sortingType, int limit)
Gets all values from the table, orders them in a specific order and limits the result.- Parameters:
orderBy- The property to order by.sortingType- The order direction. Can be either ascending or descending.limit- The maximum records to return.- Returns:
- A list with the maximum size of the parameter specified and in an ascending order.
-
createPagination
public PaginationResult<T> createPagination(int entriesPerPage)
Creates a pagination structure that splits the entire table into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called.- Parameters:
entriesPerPage- The number of entries to be displayed on each page.- Returns:
- A pagination which splits up an entire table into multiple pages.
-
createPagination
public PaginationResult<T> createPagination(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate, int entriesPerPage)
Creates a pagination structure that splits the result of a query into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called. The predicate to filter by.- Parameters:
predicate- The predicate to filter by.entriesPerPage- The number of entries to be displayed on each page.- Returns:
- A pagination which splits the result of a condition into multiple pages.
-
createPagination
public CacheablePaginationResult<T> createPagination(int entriesPerPage, java.time.Duration cacheExpiration)
Creates a pagination structure that splits the entire table into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called. When pages are retrieved, they are stored in a cache, so they can be retrieved from there the next time they are requested. Note that this will work, until the specifiedcacheExpirationis up. After that, the next time a page is requested, it will be reloaded from the database.- Parameters:
entriesPerPage- The number of entries to be displayed on each page.cacheExpiration- The time a page is valid in the cache. If a page is requested and in the cache, but expired, it will be retrieved from the database again and re-stored in the cache.- Returns:
- A pagination which displays a entire table
-
createPagination
public CacheablePaginationResult<T> createPagination(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate, int entriesPerPage, java.time.Duration cacheExpiration)
Creates a cached pagination structure that splits the result of a query into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called. When pages are retrieved, they are stored in a cache, so they can be retrieved from there the next time they are requested. Note that this will work, until the specifiedcacheExpirationis up. After that, the next time a page is requested, it will be reloaded from the database.- Parameters:
predicate- The predicate to filter by.entriesPerPage- The number of entries to be displayed on each page.cacheExpiration- The time a page is valid in the cache. If a page is requested and in the cache, but expired, it will be retrieved from the database again and re-stored in the cache.- Returns:
- A pagination which allows the developer to retrieve specific pages from the result.
-
update
public void update(T instance) throws java.sql.SQLException
Updates this entity's row on the database.- Parameters:
instance- The instance to update on the database.- Throws:
java.sql.SQLException- if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type.
-
update
public <R> void update(long entityId, com.github.collinalpert.lambda2sql.functions.SqlFunction<T,R> column, R newValue) throws java.sql.SQLExceptionUpdates a specific column for a record in a table.- Type Parameters:
R- The data type of the column. It must be the same as the data type of the new value.- Parameters:
entityId- The id of the record.column- The column to update.newValue- The new value of the column.- Throws:
java.sql.SQLException- if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type.
-
delete
public void delete(T instance) throws java.sql.SQLException
Deletes the corresponding row on the database.- Parameters:
instance- The instance to delete on the database.- Throws:
java.sql.SQLException- for example because of a foreign key constraint.
-
delete
public void delete(long id) throws java.sql.SQLExceptionDeletes a row by an id.- Parameters:
id- The row with this id to delete.- Throws:
java.sql.SQLException- for example because of a foreign key constraint.
-
delete
public void delete(java.util.List<T> entities) throws java.sql.SQLException
Deletes a list of entities at once.- Parameters:
entities- The list of entities to delete.- Throws:
java.sql.SQLException- for example because of a foreign key constraint.
-
delete
@SafeVarargs public final void delete(T... entities) throws java.sql.SQLException
Deletes multiple entities at once.- Parameters:
entities- A variable amount of entities.- Throws:
java.sql.SQLException- for example because of a foreign key constraint.- See Also:
delete(List)
-
delete
public void delete(long... ids) throws java.sql.SQLExceptionDeletes multiple rows with the corresponding ids.- Parameters:
ids- The ids to delete the rows by.- Throws:
java.sql.SQLException- for example because of a foreign key constraint.
-
delete
public void delete(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate) throws java.sql.SQLException
Deletes rows based on a condition.- Parameters:
predicate- The condition to delete by.- Throws:
java.sql.SQLException- in case the condition cannot be applied or if a foreign key constraint fails.
-
truncateTable
public void truncateTable() throws java.sql.SQLExceptionTruncates the corresponding table on the database.- Throws:
java.sql.SQLException- for example because a foreign key references this table. Note that you will not be able to truncate a table, even if no foreign key references a row in this table. TRUNCATE is a DDL command and cannot check if rows are being referenced or not.
-
-