|
Holger's Java API |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.antelmann.sql.BaseRowMapper<T>
public abstract class BaseRowMapper<T extends DBEntry>
serves as a base class for implementations of ObjectRowMapper. The methods left to be implemented directly depend on the class to be mapped:
generateNewID()-method relies on an IDFactory to be set for
this instance; otherwise, this method must be overridden, too (unless
throwing an UnsupportedOperationException is acceptable).
SQLMapFilter,
JDBCRowStore| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from interface com.antelmann.util.Converter |
|---|
Converter.Adapter<T1,T2> |
| Field Summary | |
|---|---|
protected Database<Connection> |
db
|
protected ConnectionHelper |
helper
|
| Constructor Summary | |
|---|---|
BaseRowMapper(Database<Connection> db,
MappingInfo mi,
IDFactory idFactory,
Class<T> type,
Object... topics)
allows to initialize this MappingInfo with the given one. |
|
BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn)
the idColumn is also used as the displayColumn |
|
BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
IDFactory idFactory,
Class<T> type)
|
|
BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
String displayColumn)
|
|
BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
String displayColumn,
IDFactory idFactory,
Class<T> type)
|
|
BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
String displayColumn,
IDFactory idFactory,
Class<T> type,
String query)
this constructor is eventually called by all others |
|
| Method Summary | ||
|---|---|---|
void |
addColumnsForTopic(Object topic,
String... columns)
adds all given columns as applicable for the given topic |
|
void |
applyFromMapping(MappingInfo mi,
Object... topics)
applies the info from the given mapping to this mapping (excluding table, id, display and query properties). |
|
protected void |
checkExistence(DBReference ref)
|
|
T |
convert(RowItem obj)
|
|
RowItem |
convertBack(T obj)
|
|
protected SQLMapFilter<T> |
convertTopicFilter(TopicMapFilter<?> tfilter)
|
|
Map<String,Object> |
emptyStringsToNull(Map<String,Object> map)
convenience method; replaces all empty String values with null |
|
DefaultDBReference |
extractReference(RowItem ri,
Object idTopic,
Object classTopic,
Object stringTopic,
Class<?> defaultClass)
convenience method to extract a DBReference from topics
while optionally being able to provide a defaultClass or an EntryClassMapper. |
|
Stub<T> |
extractStub(ResultSet rs,
Class<T> typeClass)
this is used to generate the Stub from a row, using the displayColumn from the mapper. |
|
|
extractStub(RowItem ri,
Object idTopic,
Object stringTopic,
Class<S> type)
convenience method |
|
Object |
generateNewID()
returns the return value of the given IDFactory or throws UnsupportedOperationException if no IDFactory was supplied |
|
protected abstract Map<String,Object> |
generateRowProperties(T entry)
generates a map that contains all columns mapped to their values, so that the map can be used to store the object into a row. |
|
protected ResultSet |
getAll(String[] columns)
returns all table rows with the given columns as a ResultSet; if the parameter is null or empty, all columns of the table (or tableQuery if applicable) are retrieved |
|
Hashtable<String,String> |
getColumnDisplayMapping()
provides direct access to the underlying data to individualize the column display information |
|
String[] |
getColumnNames()
returns all the columns that are retrieved by the mapper when querying the database); the column names for the ID column and display column are included in the returned array. |
|
String[] |
getColumnsForTopic(Object topic)
allows to determine whether there are any columns that are applicable for the given topic |
|
int |
getColumnSize(String column)
allows to determine the size of the given column name. |
|
Connection |
getConnection()
|
|
ConnectionHelper |
getConnectionHelper()
|
|
String |
getDisplayColumn()
for use in stubs (in case a single column is used as a display value) |
|
String |
getDisplayNameForColumn(String column)
returns a name suitable for display in a GUI if different from the column name used in the database |
|
Class<T> |
getEntryClass()
|
|
EntryClassMapper |
getEntryClassMapper()
|
|
String |
getIDColumn()
specifies the column where the ID of the entry is mapped to |
|
IDFactory |
getIDFactory()
|
|
Object |
getMappedValue(Object topic,
RowItem ri)
convenience method: returns null if the topic is not mapped (or if the mapped value is indeed null) |
|
String |
getMappingForTopic(Object topic)
If the topic does not exist but is a String that is an existing column, it is returned as such. |
|
protected Integer |
getNextIntID()
a convenience implementation for easily providing a way of generating new IDs for tables using an integer column as ID column. |
|
static IDFactory |
getNextIntIDFactory(ConnectionHelper helper,
String tableName,
String idColumn)
|
|
static IDFactory |
getNextIntIDFactory(ConnectionHelper helper,
String tableName,
String idColumn,
PatternExtractor<Object,Number> numberExtractor)
|
|
Filter<? super T> |
getPreStoreFilter()
|
|
Set<String> |
getReadOnlyColumns()
allows to specify columns that are never written back to the database (useful for columns that are part of a query) |
|
String[] |
getRestrictMappedColumns()
allows to limit the columns retrieved from a ResultSet when loading the data from a table query. |
|
ResultSet |
getResultSet(Filter<? super T> filter)
if no special filter for more effectively obtaining a ResultSet can be used (or if the filter is null), getAll() is called. |
|
int |
getSQLTypeforColumn(String column)
allows to determine the SQL-type for a given column name |
|
protected String[] |
getStubColumns()
allows to limit the columns in the select statement for retrieving stubs; this implementation returns an array containing the id column and - if applicable - the display column. |
|
String |
getStubQuery()
|
|
ResultSet |
getStubResultSet(Filter<? super Stub<?>> filter)
supports the same functionality as getResultSet(Filter); additional support for SQLStubFilter |
|
String[] |
getSupportedTopicColumns()
returns all topics supported by getColumnsForTopic(Object) |
|
String[] |
getTableColumnNames()
|
|
String |
getTableName()
provides the name of the table that stores the data for the store |
|
String |
getTableQuery()
if querying involves something different than just selecting all columns of the table, this method returns the string used to replace the table name in a query. |
|
protected String |
getTableQueryForComboFilter(SQLComboFilter<?> filter)
this implementation first looks for a mapping for SQLCombiFilter.class and then (if no mapping was set) returns the tableQuery if non-null and the tableName otherwise. |
|
TransactionCounter |
getTransactionCounter()
returns the TransactionCounter instance that is used with this instance. |
|
String |
getUniqueRandomString()
a convenience implementation for easily providing a way of generating new IDs for tables using a random String as an ID. |
|
void |
handleAfterStore(T entry,
boolean isInsert)
does nothing unless overridden |
|
protected void |
initMapping()
responsible for initializing and caching the meta data for the MappingInfo interface;
note that this method is indirectly called by the constructor |
|
boolean |
isMappedTopic(Object topic)
|
|
boolean |
isTranslate()
|
|
Object |
javaToSql(Object obj,
int sqlType,
int precision)
implementation based on ConnectionHelper.javaToSql(Object, int, int);
used in MappedEntryMapper |
|
Object |
javaToSql(Object value,
String column)
used to convert java values to their sql representation for filtering purposes |
|
protected void |
lockTable()
locks the table in a database specific way so that certain methods are prevented from running into deadlocks. |
|
void |
mapReference(DBReference ref,
Map<String,Object> map,
Object idTopic,
Object classTopic,
Object stringTopic)
#see mapReference(DBReference, Map, Object, Object, Object, Class) |
|
void |
mapReference(DBReference ref,
Map<String,Object> map,
Object idTopic,
Object classTopic,
Object stringTopic,
Class<?> refType)
convenience method to map a DBReference to the given topics
while optionally being able to provide an EntryClassMapper
If the topic does not exist but is a String that is an existing column, it is used as such. |
|
RowItem |
mapToRow(T entry)
uses the abstract generateRowProperties(DBEntry) method to generate the RowItem |
|
ArrayList<String> |
removeColumnTopic(Object topic)
removes all columns from the mapping for the given topic |
|
void |
setColumnsForTopic(Object topic,
String... columns)
|
|
protected void |
setDisplayColumn(String displayColumn)
|
|
void |
setEntryClass(Class<T> type)
this must never be null if the idFactory is non-null (or if a TopicMapFilter is used and the store overrides the MappingInfo) |
|
void |
setEntryClassMapper(EntryClassMapper ecm)
|
|
void |
setIDFactory(IDFactory idFactory,
Class<T> type)
|
|
boolean |
setMappedValue(Object topic,
Map<String,Object> map,
Object value)
convenience method: sets the given value corresponding to the topic on the map only if the topic is mapped and the column is not read-only. |
|
void |
setNextIntIdAsFactory(Class<T> type)
sets a factory that uses getNextIntID() for generateNewID() |
|
void |
setPreStoreFilter(Filter<? super T> filter)
allows to specify a filter that is used before each update/insert |
|
void |
setReadOnlyColumns(String... columns)
allows to set the read-only columns in a potentially more convenient way |
|
void |
setRestrictedMappedColumns(String... columns)
restricts the columns that are actually retrieved when instantiating the RowItem; the other columns can still be queried upon, but they will not be available upon creating the instance. |
|
void |
setStubQuery(String stubQuery)
allows to specify a special query for retrieving stubs. |
|
protected void |
setTableQuery(String tableQuery)
allows to set a query for mapping the data instead of just using the table name. |
|
protected void |
setTableQuery(String tableQuery,
String newIdColumn)
in addition to setting a table query, this also allows to change the id column (often required to ensure the uniqueness of the id column in a query that may contain the column multiple times). |
|
String |
setTopic(Object topic,
String mapping)
allows to define information for getMappingForTopic(Object) |
|
void |
setTranslate(boolean flag)
allows to set the flag that determines whether a filter map is 'translated' before use. |
|
|
sqlToJava(Object obj,
Class<X> requiredJavaType)
implementation based on ConnectionHelper.sqlToJava(Object, Class);
used in MappedEntryMapper |
|
Map<String,Object> |
toSQLMap(Map<String,Object> restrictions)
used for convenience to convert values from a filtering map |
|
void |
verifyBeforeDelete(Object id)
does nothing unless overridden |
|
void |
verifyBeforeStore(T entry,
boolean isInsert)
checks whether the filter (if non-null) accepts the entry. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface com.antelmann.sql.ObjectRowMapper |
|---|
instantiate |
| Field Detail |
|---|
protected final Database<Connection> db
protected final ConnectionHelper helper
| Constructor Detail |
|---|
public BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn)
throws DatabaseException
DatabaseException
public BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
IDFactory idFactory,
Class<T> type)
throws DatabaseException
DatabaseException
public BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
String displayColumn)
throws DatabaseException
DatabaseException
public BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
String displayColumn,
IDFactory idFactory,
Class<T> type)
throws DatabaseException
DatabaseException
public BaseRowMapper(Database<Connection> db,
String tableName,
String idColumn,
String displayColumn,
IDFactory idFactory,
Class<T> type,
String query)
throws DatabaseException
DatabaseException
public BaseRowMapper(Database<Connection> db,
MappingInfo mi,
IDFactory idFactory,
Class<T> type,
Object... topics)
throws DatabaseException
MappingInfo instance is also a
DefaultMappingInfo, the topics and read-only columns are initialized accordingly as well.
DatabaseException| Method Detail |
|---|
public T convert(RowItem obj)
throws IllegalArgumentException
convert in interface Converter<RowItem,T extends DBEntry>IllegalArgumentException
public RowItem convertBack(T obj)
throws IllegalArgumentException,
UnsupportedOperationException
convertBack in interface Converter<RowItem,T extends DBEntry>IllegalArgumentException
UnsupportedOperationException
public void applyFromMapping(MappingInfo mi,
Object... topics)
BaseRowMapper(Database, MappingInfo, IDFactory, Class, Object...)
@Warning(value="called by the constructor")
protected void initMapping()
throws DatabaseException
MappingInfo interface;
note that this method is indirectly called by the constructor
DatabaseExceptionsetTableQuery(String)public final Filter<? super T> getPreStoreFilter()
public void setPreStoreFilter(Filter<? super T> filter)
verifyBeforeStore(DBEntry, boolean)public final String getStubQuery()
public void setStubQuery(String stubQuery)
setTableQuery(String)
public final boolean isTranslate()
setTranslate(boolean)public void setTranslate(boolean flag)
getStubResultSet(Filter),
getResultSet(Filter)public final EntryClassMapper getEntryClassMapper()
public void setEntryClassMapper(EntryClassMapper ecm)
public Set<String> getReadOnlyColumns()
public void setReadOnlyColumns(String... columns)
public String setTopic(Object topic,
String mapping)
getMappingForTopic(Object)
mapping - if null, the given topic is removed from the map
getMappingForTopic(Object)public ArrayList<String> removeColumnTopic(Object topic)
getColumnsForTopic(Object)public String[] getSupportedTopicColumns()
getColumnsForTopic(Object)
public void setColumnsForTopic(Object topic,
String... columns)
getColumnsForTopic(Object)
public void addColumnsForTopic(Object topic,
String... columns)
getColumnsForTopic(Object)public Hashtable<String,String> getColumnDisplayMapping()
getDisplayNameForColumn(String)public final ConnectionHelper getConnectionHelper()
public final Connection getConnection()
getConnection in interface ObjectRowMapper<T extends DBEntry>public final String getTableName()
MappingInfo
getTableName in interface MappingInfoMappingInfo.getTableQuery()public final String getTableQuery()
MappingInfoMappingInfo.getTableName() is used in all cases.
If non-null, this variable would have a value like '(select a.field1, b.* from a, b where a.id=b.id) as t1'.
getTableQuery in interface MappingInfoMappingInfo.getTableName()public final String getIDColumn()
MappingInfo
getIDColumn in interface MappingInfopublic final String getDisplayColumn()
MappingInfo
getDisplayColumn in interface MappingInfoprotected final void setDisplayColumn(String displayColumn)
@Warning(value="called by the constructor")
protected void setTableQuery(String tableQuery)
throws DatabaseException
initMapping() after setting the data.
Since a query of the form '(select ...) as t1' may not be query optimized in certain
databases, this implementation also supports a plain select statement to act as a query.
DatabaseException
@Warning(value="called by the constructor")
protected void setTableQuery(String tableQuery,
String newIdColumn)
throws DatabaseException
DatabaseExceptionsetTableQuery(String)
public void verifyBeforeStore(T entry,
boolean isInsert)
throws DatabaseException,
SQLException
NotApplicableDataWarning is thrown.
When overriding this method, it is advised to call this super implementation.
verifyBeforeStore in interface ObjectRowMapper<T extends DBEntry>isInsert - if true, the entry is about to be inserted; if false, it is an update
DatabaseException
SQLExceptionsetPreStoreFilter(Filter)
public void verifyBeforeDelete(Object id)
throws DatabaseException,
SQLException
verifyBeforeDelete in interface ObjectRowMapper<T extends DBEntry>WrongDataException - if the deletion is not allowed for semantical reasons
DatabaseException
SQLException
public void handleAfterStore(T entry,
boolean isInsert)
throws DatabaseException,
SQLException
handleAfterStore in interface ObjectRowMapper<T extends DBEntry>isInsert - if true, the entry is about to be inserted; if false, it is an update
DatabaseException
SQLExceptionpublic final IDFactory getIDFactory()
public final Class<T> getEntryClass()
public void setEntryClass(Class<T> type)
public void setIDFactory(IDFactory idFactory,
Class<T> type)
public void setNextIntIdAsFactory(Class<T> type)
throws NullPointerException
getNextIntID() for generateNewID()
type - must not be null
NullPointerException
public Object generateNewID()
throws DatabaseException,
UnsupportedOperationException
generateNewID in interface ObjectRowMapper<T extends DBEntry>DatabaseException
UnsupportedOperationExceptionDBClassStore.generateNewID(),
SQLNumberGenerator,
TransactionalNumberGenerator
protected abstract Map<String,Object> generateRowProperties(T entry)
throws WrongDataException
WrongDataException
public RowItem mapToRow(T entry)
throws WrongDataException
generateRowProperties(DBEntry) method to generate the RowItem
mapToRow in interface ObjectRowMapper<T extends DBEntry>WrongDataExceptiongetReadOnlyColumns()
public Stub<T> extractStub(ResultSet rs,
Class<T> typeClass)
throws SQLException
getStubColumns() to ensure that these columns are generally present.
extractStub in interface ObjectRowMapper<T extends DBEntry>SQLExceptionprotected String[] getStubColumns()
extractStub(ResultSet, Class)
protected ResultSet getAll(String[] columns)
throws SQLException
SQLException
public ResultSet getStubResultSet(Filter<? super Stub<?>> filter)
throws SQLException
getResultSet(Filter); additional support for SQLStubFilter
getStubResultSet in interface ObjectRowMapper<T extends DBEntry>SQLExceptionprotected String getTableQueryForComboFilter(SQLComboFilter<?> filter)
getMappingForTopic(Object)
public ResultSet getResultSet(Filter<? super T> filter)
throws SQLException
getAll() is called.
Special filters supported by this implementation include:
SQLDirectQueryEntryFilter, SQLMapFilter, FullTextSearchFilter, SQLCombiFilter, TopicMapFilter and PreparedStatementEntryFilter.
getResultSet in interface ObjectRowMapper<T extends DBEntry>SQLExceptionPreparedStatementEntryFilter,
SQLDirectQueryEntryFilter,
FullTextSearchFilter,
SQLMapFilter,
TopicMapFilter,
SQLComboFilter,
SQLFetchSizeFilterprotected SQLMapFilter<T> convertTopicFilter(TopicMapFilter<?> tfilter)
public boolean isMappedTopic(Object topic)
public String getMappingForTopic(Object topic)
getMappingForTopic in interface MappingInfosetTopic(Object, String)public String[] getColumnsForTopic(Object topic)
MappingInfo
getColumnsForTopic in interface MappingInfopublic String[] getColumnNames()
MappingInfo
getColumnNames in interface MappingInfoMappingInfo.getTableName(),
MappingInfo.getTableQuery(),
MappingInfo.getIDColumn(),
MappingInfo.getDisplayColumn()
public String[] getTableColumnNames()
throws DatabaseException
DatabaseException
public int getSQLTypeforColumn(String column)
throws IllegalArgumentException
MappingInfo
getSQLTypeforColumn in interface MappingInfoIllegalArgumentException - if the given column is not knownTypes
public int getColumnSize(String column)
throws IllegalArgumentException
MappingInfo
getColumnSize in interface MappingInfoIllegalArgumentException - if the given column is not knownpublic String getDisplayNameForColumn(String column)
MappingInfo
getDisplayNameForColumn in interface MappingInfopublic TransactionCounter getTransactionCounter()
getNextIntID()
method.
protected void lockTable()
throws SQLException
SQLExceptionsetNextIntIdAsFactory(Class),
getNextIntID(),
ConnectionHelper.lockTable(boolean, String...)
@Warning(value={"to avoid deadlocks/inconsistencies this method would have to explicitly write-lock the table (which is database-specific) before accessing it","this method must be called synchronized on the transaction (which is usually the case when callled by generateNewID())"})
protected Integer getNextIntID()
throws DatabaseException
DatabaseExceptionTransactionalNumberGenerator
public static IDFactory getNextIntIDFactory(ConnectionHelper helper,
String tableName,
String idColumn)
public static IDFactory getNextIntIDFactory(ConnectionHelper helper,
String tableName,
String idColumn,
PatternExtractor<Object,Number> numberExtractor)
public String getUniqueRandomString()
UUID.toString()public Map<String,Object> emptyStringsToNull(Map<String,Object> map)
public void setRestrictedMappedColumns(String... columns)
public String[] getRestrictMappedColumns()
ObjectRowMapper
getRestrictMappedColumns in interface ObjectRowMapper<T extends DBEntry>ResultSet when extracting the data into a RowItem; null may be returned.ObjectRowMapper.instantiate(RowItem)public Map<String,Object> toSQLMap(Map<String,Object> restrictions)
public Object javaToSql(Object value,
String column)
public <X> X sqlToJava(Object obj,
Class<X> requiredJavaType)
ConnectionHelper.sqlToJava(Object, Class);
used in MappedEntryMapper
public Object javaToSql(Object obj,
int sqlType,
int precision)
ConnectionHelper.javaToSql(Object, int, int);
used in MappedEntryMapper
public Object getMappedValue(Object topic,
RowItem ri)
isMappedTopic(Object)
public boolean setMappedValue(Object topic,
Map<String,Object> map,
Object value)
javaToSql(Object, String)
public <S extends DBEntry> Stub<S> extractStub(RowItem ri,
Object idTopic,
Object stringTopic,
Class<S> type)
throws InconsistentContentException,
ClassNotFoundException
InconsistentContentException
ClassNotFoundExceptionextractReference(RowItem, Object, Object, Object, Class)
public DefaultDBReference extractReference(RowItem ri,
Object idTopic,
Object classTopic,
Object stringTopic,
Class<?> defaultClass)
throws InconsistentContentException,
ClassNotFoundException
DBReference from topics
while optionally being able to provide a defaultClass or an EntryClassMapper.
If the topic does not exist but is a String that is an existing column, it is used as such.
InconsistentContentException
ClassNotFoundExceptionmapReference(DBReference, Map, Object, Object, Object),
setEntryClassMapper(EntryClassMapper)
public void mapReference(DBReference ref,
Map<String,Object> map,
Object idTopic,
Object classTopic,
Object stringTopic)
mapReference(DBReference, Map, Object, Object, Object, Class)
public void mapReference(DBReference ref,
Map<String,Object> map,
Object idTopic,
Object classTopic,
Object stringTopic,
Class<?> refType)
DBReference to the given topics
while optionally being able to provide an EntryClassMapper
If the topic does not exist but is a String that is an existing column, it is used as such.
ref - the reference to be mappedmap - the column map that is filled by this methodidTopic - the topic that specifies the id column for the reference idclassTopic - the topic that specifies the class column for the reference classstringTopic - the topic that specifies the string column for the reference stringrefType - if non-null, the method will check whether the ref class confirms to the specified type
if no classTopic was given for mappingextractReference(RowItem, Object, Object, Object, Class),
setEntryClassMapper(EntryClassMapper)
protected void checkExistence(DBReference ref)
throws DatabaseException
DatabaseException
|
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||