I
- a type of ID of the records stored in the tableR
- a result type of a read operation by a single IDW
- a type of stored records@Internal
public abstract class AbstractTable<I,R,W>
extends java.lang.Object
implements io.spine.logging.Logging
This class is responsible for storing all the information about a table including:
PRIMARY KEY
;
A table provides a sufficient API for performing the database interaction. However, it never performs any validation or data transformation, but only invokes the appropriate queries.
TableColumn
Modifier | Constructor and Description |
---|---|
protected |
AbstractTable(java.lang.String name,
IdColumn<I> idColumn,
DataSourceWrapper dataSource,
TypeMapping typeMapping) |
Modifier and Type | Method and Description |
---|---|
protected com.google.common.collect.ImmutableMap<java.lang.String,java.lang.Object> |
columnDefaults()
Obtains the map of column defaults for this table.
|
protected abstract WriteQuery |
composeInsertQuery(I id,
W record) |
protected abstract SelectQuery<R> |
composeSelectQuery(I id) |
protected abstract WriteQuery |
composeUpdateQuery(I id,
W record) |
protected boolean |
containsRecord(I id)
Checks if the table contains a record with given ID.
|
void |
create()
Creates current table in the database if it does not exist yet.
|
protected DataSourceWrapper |
dataSource() |
boolean |
delete(I id)
Deletes a row in the table corresponding to the given ID.
|
void |
deleteAll()
Deletes all the records from the table.
|
protected IdColumn<I> |
idColumn() |
java.util.Iterator<I> |
index()
Retrieves the table index.
|
void |
insert(I id,
W record)
Inserts the record into the table using the specified ID.
|
protected java.lang.String |
name() |
R |
read(I id)
Reads a record with the given ID from the table.
|
protected abstract java.util.List<? extends TableColumn> |
tableColumns() |
void |
update(I id,
W record)
Updates the record with the specified ID for the table.
|
void |
write(I id,
W record)
Performs a write operation on the table.
|
protected AbstractTable(java.lang.String name, IdColumn<I> idColumn, DataSourceWrapper dataSource, TypeMapping typeMapping)
protected abstract java.util.List<? extends TableColumn> tableColumns()
public void create()
Equivalent to an SQL expression:
CREATE TABLE IF NOT EXISTS $TableName ( $Columns );
protected boolean containsRecord(I id)
id
- an ID to checktrue
if there is a record with such ID in the table, false
otherwisepublic R read(I id)
id
- an ID to search bynull
if there is no record with given IDpublic void write(I id, W record)
If the table contains a record with the given ID,
the operation is treated as an UPDATE
, otherwise - as an INSERT
.
id
- an ID to write the record underrecord
- the record to writepublic java.util.Iterator<I> index()
Iterator
over the table IDsStorage.index()
public void insert(I id, W record)
id
- an ID for the recordrecord
- a record to insertpublic void update(I id, W record)
id
- an ID of the recordrecord
- a new state of the recordprotected com.google.common.collect.ImmutableMap<java.lang.String,java.lang.Object> columnDefaults()
protected java.lang.String name()
protected DataSourceWrapper dataSource()
protected abstract WriteQuery composeInsertQuery(I id, W record)
protected abstract WriteQuery composeUpdateQuery(I id, W record)
protected abstract SelectQuery<R> composeSelectQuery(I id)
public boolean delete(I id)
id
- an ID to search bytrue
if the row was deleted successfully,
false
if the row was not foundpublic void deleteAll()