Interface DatabasePojo
- All Superinterfaces:
com.github.tadukoo.util.pojo.MappedPojo
- All Known Implementing Classes:
AbstractDatabasePojo
public interface DatabasePojo
extends com.github.tadukoo.util.pojo.MappedPojo
Database Pojo represents a
MappedPojo
that can be used with a Database
, to store and retrieve
values from the Database
as needed.- Version:
- Alpha v.0.3
- Author:
- Logan Ferree (Tadukoo)
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
addColumnDef
(ColumnDefinition columnDef) Adds the givenColumnDefinition
to the Map ofcolumn definitions
default void
addColumnDef
(ColumnDefinition columnDef, Object value) Adds the givenColumnDefinition
to the Map ofcolumn definitions
and sets the column value to the given valuedefault void
addForeignKey
(ForeignKeyConstraint foreignKey) Adds the givenforeign key
to this pojodefault void
addSubPojo
(SubPojoDefinition subPojoDef, DatabasePojo subPojo, ForeignKeyConstraint foreignKey) Adds the info for a subPojo using the given parametersdefault void
createTable
(Database database) Create a table for this pojo on the givenDatabase
default <P extends DatabasePojo>
List<P>Performs a search for data on pojos of this type in the givenDatabase
.default <P extends DatabasePojo>
List<P>doSearch
(Database database, Class<P> clazz, Collection<String> subPojosToUse) Performs a search for data on pojos of this type in the givenDatabase
.default ColumnDefinition
getColumnDefByKey
(String key) Retrieve aColumnDefinition
from the Map using the given keydefault com.github.tadukoo.util.functional.function.ThrowingFunction<ResultSet,
Boolean, SQLException> default <P extends DatabasePojo>
com.github.tadukoo.util.functional.function.ThrowingFunction<ResultSet,List<P>, SQLException> getResultSetListFunc
(Class<P> clazz) default SubPojoDefinition
getSubPojoDefBySubPojoKey
(String subPojoKey) default void
retrieveValues
(Database database, boolean retrieveSubPojos) Retrieves all the values for the pojo from the database, using the stored id column valuedefault void
retrieveValues
(Database database, Object idColumnValue, boolean retrieveSubPojos) Retrieves all the values for the pojo from the database, using the given id column valuevoid
Should be called in the constructor of aDatabasePojo
and be used to create thecolumn definitions
default Integer
storeValues
(Database database, boolean storeSubPojos) Stores the values from thisDatabasePojo
into the givenDatabase
.Methods inherited from interface com.github.tadukoo.util.pojo.MappedPojo
clear, getItem, getKeys, getListItem, getListItemNoThrow, getListItemNoThrow, getMap, getPojoItem, getPojoItemNoThrow, getPojoItemNoThrow, hasItem, hasKey, isEmpty, removeItem, setItem
-
Method Details
-
getTableName
String getTableName()- Returns:
- The name of the table for this pojo
-
getIDColumnName
String getIDColumnName()- Returns:
- The name of the ID column for this pojo
-
getColumnDefs
Map<String,ColumnDefinition> getColumnDefs()- Returns:
- A Map of all the
column definitions
in this pojo
-
getSubPojoDefs
Map<String,SubPojoDefinition> getSubPojoDefs()- Returns:
- A Map of all
subPojo definitions
in this pojo
-
getSubPojoKeys
- Returns:
- The keys for any pojos stored inside this pojo
-
getSubPojoDefBySubPojoKey
- Parameters:
subPojoKey
- The subPojoKey to use to find a subPojoDefinition- Returns:
- The
SubPojoDefinition
for the given subPojoKey
-
getForeignKeys
List<ForeignKeyConstraint> getForeignKeys()- Returns:
- The List of
foreign keys
for this pojo
-
addForeignKey
Adds the givenforeign key
to this pojo- Parameters:
foreignKey
- Theforeign key
to be added to the pojo
-
addSubPojo
default void addSubPojo(SubPojoDefinition subPojoDef, DatabasePojo subPojo, ForeignKeyConstraint foreignKey) Adds the info for a subPojo using the given parameters- Parameters:
subPojoDef
- TheSubPojoDefinition
to be added to this pojosubPojo
- The subPojo itself to be stored (may be null)foreignKey
- Theforeign key
for the subPojo (to use when setting up the table)
-
addColumnDef
Adds the givenColumnDefinition
to the Map ofcolumn definitions
- Parameters:
columnDef
- TheColumnDefinition
to add to the Map
-
addColumnDef
Adds the givenColumnDefinition
to the Map ofcolumn definitions
and sets the column value to the given value- Parameters:
columnDef
- TheColumnDefinition
to add to the Mapvalue
- The value to set for the column
-
setDefaultColumnDefs
void setDefaultColumnDefs()Should be called in the constructor of aDatabasePojo
and be used to create thecolumn definitions
-
getColumnDefKeys
- Returns:
- The Set of
column definition
keys
-
getColumnDefByKey
Retrieve aColumnDefinition
from the Map using the given key- Parameters:
key
- The key for theColumnDefinition
- Returns:
- The
ColumnDefinition
from the Map
-
createTable
Create a table for this pojo on the givenDatabase
- Parameters:
database
- TheDatabase
to create the table on- Throws:
SQLException
- If something goes wrong in creating the table
-
retrieveValues
Retrieves all the values for the pojo from the database, using the stored id column value- Parameters:
database
- TheDatabase
to retrieve values fromretrieveSubPojos
- Whether to also retrieve values for subPojos or not- Throws:
SQLException
- If anything goes wrong in retrieving values
-
retrieveValues
default void retrieveValues(Database database, Object idColumnValue, boolean retrieveSubPojos) throws SQLException Retrieves all the values for the pojo from the database, using the given id column value- Parameters:
database
- TheDatabase
to retrieve values fromidColumnValue
- The ID column value to be usedretrieveSubPojos
- Whether to also retrieve values for subPojos or not- Throws:
SQLException
- If anything goes wrong in retrieving values
-
getResultSetFunc
default com.github.tadukoo.util.functional.function.ThrowingFunction<ResultSet,Boolean, getResultSetFunc()SQLException> - Returns:
- A
ThrowingFunction
to use to extract a singleDatabasePojo
from aResultSet
-
getResultSetListFunc
default <P extends DatabasePojo> com.github.tadukoo.util.functional.function.ThrowingFunction<ResultSet,List<P>, getResultSetListFuncSQLException> (Class<P> clazz) ThisThrowingFunction
is used to extract a List ofDatabasePojos
from aResultSet
(e.g. from a search).- Type Parameters:
P
- TheDatabasePojo
class to be used for the List- Parameters:
clazz
- TheDatabasePojo
class to be used for the List- Returns:
- A
ThrowingFunction
that extracts a List ofDatabasePojos
from aResultSet
-
storeValues
Stores the values from thisDatabasePojo
into the givenDatabase
. Specifying storeSubPojos will also store values on any subPojos stored in this pojo- Parameters:
database
- TheDatabase
to store values instoreSubPojos
- Whether to store values on subPojos or not- Returns:
- The new ID for the
DatabasePojo
if it was freshly inserted to theDatabase
- Throws:
SQLException
- If anything goes wrong in storing values
-
doSearch
default <P extends DatabasePojo> List<P> doSearch(Database database, Class<P> clazz, boolean useSubPojos) throws SQLException Performs a search for data on pojos of this type in the givenDatabase
. The given class is for which class of pojos to return and the boolean determines whether to use all the subPojos stored in this pojotrue
in this search or use none of themfalse
- Type Parameters:
P
- TheDatabasePojo
class to use for the returned pojos- Parameters:
database
- TheDatabase
to do the search onclazz
- TheDatabasePojo
class to use for the returned pojosuseSubPojos
- Whether to use all subPojostrue
or nonefalse
in the search- Returns:
- A List of pojos found from the search
- Throws:
SQLException
- If anything goes wrong in doing the search
-
doSearch
default <P extends DatabasePojo> List<P> doSearch(Database database, Class<P> clazz, Collection<String> subPojosToUse) throws SQLException Performs a search for data on pojos of this type in the givenDatabase
. The given class is for which class of pojos to return and the collection of subPojo strings is for subPojoKeys of subPojos to be used in the search (so long as they're non-null)- Type Parameters:
P
- TheDatabasePojo
class to use for the returned pojos- Parameters:
database
- TheDatabase
to do the search onclazz
- TheDatabasePojo
class to use for the returned pojossubPojosToUse
- The subPojoKeys of subPojos to use in the search- Returns:
- A List of pojos found from the search
- Throws:
SQLException
- If anything goes wrong in doing the search
-