public final class Repository.Record
extends java.util.HashMap<java.lang.String,java.lang.Object>
In general this code is fairly flexible as to the type of objects stored in the record. The type of object returned from the DB depends pretty much on the DB layer and the DB schema. Note that different DB drivers will handle the same schema in different ways, for example different mysql drivers/versions returned Integers or Longs for the same field.
Record supports a default conversion between integer and Date properties/Fields
based on the Repository Resolution property. The resolution defaults to 1 second
but can be set using the repository.resolution.
Records are always created empty.
They can then either be populated using a ResultSet or created from
scratch using put/get methods. An existing database record can be
retrieved by using the setID method on an empty Record.
In all cases no changes are written to
the database until the commit() method is called.
The class also supports get/set Property methods that support
additional type conversions and casting. These are more useful than the
put/get calls in the Map interface
as the type of Object returned from the map may change if the database schema (or JDBC driver)
is changed.
For Example:
In many cases the values of a database field are actually codes denoting one of a
finite set of objects.In this case a class implementing
Repository res = Repository.getInstance(ctx,"my_table");
Record rec = res.new Record();
rec.setProperty("Number",12.0);
rec.commit(); // create record
rec.setProperty("Number",24.0);
rec.commit(); // update record
Record rec2 = res.new Record();
rec2.setID(rec.getID()); // retrieve new copy from DB by ID.
double d = rec2.getDoubleProperty("Number"); // should be 24.0
TypeProducer should be
used to express this mapping. There are special getProperty and setProperty
calls that use this interface.
Java Enum types can be supported using EnumProducer
where the text value of the Enum will be stored in the database.
however much of the existing code base uses sub classes of BasicType to implement
type-safe enumeration.
The intention is that this class is more database facing and that model classes contain an instance of Record rather than sub-classing it.
![]() |
![]() |
![]() |
![]() |
| Constructor and Description |
|---|
Record() |
| Modifier and Type | Method and Description |
|---|---|
void |
backup() |
void |
clear()
reset to uninitialised state.
|
java.lang.Object |
clone() |
boolean |
commit()
commit changes made to this object to the
|
void |
delete()
delete the corresponding database entry and restore the Record to
uninitialised state.
|
boolean |
equals(java.util.Map<java.lang.String,java.lang.Object> r)
Compare with a Map of values for all the fields they have in common
test for equality.
|
int |
findDuplicate()
Get the id of an identical (up to id) record in the database
normally called on an uncommitted record so only the set fields
are used to generate the query
This is not efficient but can be used when merging data.
|
int |
findDuplicate(int id,
java.util.Set<java.lang.String> fields,
boolean check_all)
Get the id of an identical (up to id) record in the database
normally called on an uncommitted record so only the set fields
are used to generate the query.
|
boolean |
getBooleanProperty(java.lang.String name)
Query a property that should contain a single Y/N character and retrun
this as a boolean.
|
boolean |
getBooleanProperty(java.lang.String name,
boolean default_value)
Query a property that should contain a single Y/N character and retrun
this as a boolean.
|
java.util.Date |
getDateProperty(java.lang.String name) |
double |
getDoubleProperty(java.lang.String name)
get a property as a double value
|
double |
getDoubleProperty(java.lang.String name,
double default_value) |
float |
getFloatProperty(java.lang.String name)
get a property as a float value
|
float |
getFloatProperty(java.lang.String name,
float default_value) |
int |
getID()
get the unique id value for this Record
|
int |
getIntProperty(java.lang.String name) |
int |
getIntProperty(java.lang.String name,
int default_value) |
long |
getLongProperty(java.lang.String name)
get a property as a long value
|
long |
getLongProperty(java.lang.String name,
long default_value) |
java.lang.Number |
getNumberProperty(java.lang.String name) |
java.lang.Object |
getProperty(java.lang.String name)
Returns the value associated with this database column name
Where possible you should use one of the get
|
<F,D> F |
getProperty(TypeProducer<F,D> t)
Return the .Value associated with the specified TypeProducer
|
<F,D> F |
getProperty(TypeProducer<F,D> t,
F def)
Return the .Value associated with the specified TypeProducer
|
Repository |
getRepository()
get the enclosing Repository
|
java.lang.Object |
getRequiredProperty(java.lang.String name)
Returns the value associated with this database column name Unlike
getProperty the returned value should never be null to additional error
reporting is invoked if it is.
|
StreamData |
getStreamDataProperty(java.lang.String name) |
java.lang.String |
getStringProperty(java.lang.String name)
get a property as a string.
|
java.lang.String |
getStringProperty(java.lang.String name,
java.lang.String default_value)
get a property value as a string with a default value to return if the
property is not set or the string is empty
|
java.util.Map<java.lang.String,java.lang.Object> |
getValues()
Get a map of the contents without the UniqueID field
|
boolean |
isLocked() |
void |
lock()
mark the record as read-only/locked
This is intended for when
|
java.lang.Object |
put(java.lang.String key,
java.lang.Object value) |
void |
putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m) |
java.lang.Object |
remove(java.lang.String key) |
void |
set(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m)
Like putAll but does full synchronisation removing fields that are
missing as well.
|
Repository.Record |
setID(int id2)
This method returns a reference to itself so we can initialise the record
as part of a constructor.
|
protected void |
setInitialID(int id) |
void |
setOptionalProperty(java.lang.String name,
java.lang.Object value)
Set a property where it is acceptable for the property not to exist in the
underlying database.
|
<F,D> void |
setOptionalProperty(TypeProducer<? super F,D> t,
F v)
Set the property associated with a
TypeProducer
where it is acceptable for the property not to exist in the
underlying database |
void |
setProperty(java.lang.String name,
boolean val)
store a Y/N value based on boolean input.
|
void |
setProperty(java.lang.String name,
double val)
set a property to a double value
|
void |
setProperty(java.lang.String name,
float val)
set a property to an float value
|
void |
setProperty(java.lang.String name,
int val)
set a property to an integer value
|
void |
setProperty(java.lang.String name,
long val)
set a property to an long value
|
void |
setProperty(java.lang.String key,
java.lang.Object value)
Store data in record
|
<F,D> void |
setProperty(TypeProducer<? super F,D> t,
F v)
Set the property associated with a
TypeProducer |
protected void |
setValue(java.lang.StringBuilder buff,
java.sql.PreparedStatement stmt,
int pos,
java.lang.String key)
output the value of a field to a prepared statement
Note that
StreamData objects are added as binary streams. |
java.lang.String |
toString() |
compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuespublic void clear()
clear in interface java.util.Map<java.lang.String,java.lang.Object>clear in class java.util.HashMap<java.lang.String,java.lang.Object>public java.lang.Object clone()
clone in class java.util.HashMap<java.lang.String,java.lang.Object>public boolean commit()
throws DataFault
DataFaultpublic java.util.Map<java.lang.String,java.lang.Object> getValues()
public void delete()
throws ConsistencyError,
DataFault
ConsistencyErrorDataFaultpublic boolean equals(java.util.Map<java.lang.String,java.lang.Object> r)
r - Map to compare topublic final boolean getBooleanProperty(java.lang.String name)
name - String name of propertypublic final boolean getBooleanProperty(java.lang.String name,
boolean default_value)
name - String name of propertydefault_value - value to return for missing propertypublic final java.util.Date getDateProperty(java.lang.String name)
public final double getDoubleProperty(java.lang.String name)
name - String Property to getpublic final double getDoubleProperty(java.lang.String name,
double default_value)
public final float getFloatProperty(java.lang.String name)
name - String Property to getpublic final float getFloatProperty(java.lang.String name,
float default_value)
public final int getIntProperty(java.lang.String name)
public final int getIntProperty(java.lang.String name,
int default_value)
public final int getID()
throws ConsistencyError
ConsistencyError - if id not defined yetpublic final long getLongProperty(java.lang.String name)
name - String Property to getpublic final long getLongProperty(java.lang.String name,
long default_value)
public final java.lang.Number getNumberProperty(java.lang.String name)
public final <F,D> F getProperty(TypeProducer<F,D> t)
F - The type of value returnedD - Type stored in DBt - public final <F,D> F getProperty(TypeProducer<F,D> t, F def)
F - The type of value returnedD - Type stored in DBt - def - default resultpublic final java.lang.Object getProperty(java.lang.String name)
name - The name of the fieldpublic final java.lang.Object getRequiredProperty(java.lang.String name)
name - The name of the fieldpublic final StreamData getStreamDataProperty(java.lang.String name) throws DataFault
DataFaultpublic final java.lang.String getStringProperty(java.lang.String name)
name - Property to getpublic final java.lang.String getStringProperty(java.lang.String name,
java.lang.String default_value)
name - default_value - public Repository getRepository()
public java.lang.Object put(java.lang.String key,
java.lang.Object value)
put in interface java.util.Map<java.lang.String,java.lang.Object>put in class java.util.HashMap<java.lang.String,java.lang.Object>public void putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m)
putAll in interface java.util.Map<java.lang.String,java.lang.Object>putAll in class java.util.HashMap<java.lang.String,java.lang.Object>public java.lang.Object remove(java.lang.String key)
public void set(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m)
m - Map to sync toprotected void setInitialID(int id)
public Repository.Record setID(int id2) throws DataException
id2 - DataExceptionpublic final void setProperty(java.lang.String key,
java.lang.Object value)
key - value - public final void setOptionalProperty(java.lang.String name,
java.lang.Object value)
name - value - public final <F,D> void setProperty(TypeProducer<? super F,D> t, F v)
TypeProducerF - Type corresponding to the TypeProducerD - Type stored in Databaset - the BasicType to setv - The Value to set.public final <F,D> void setOptionalProperty(TypeProducer<? super F,D> t, F v)
TypeProducer
where it is acceptable for the property not to exist in the
underlying databaseF - Type corresponding to TypeProducerD - Type stored in Databaset - the BasicType to setv - The Value to set.public final void setProperty(java.lang.String name,
boolean val)
name - String property to setval - boolean valueRepository.convertBoolean(Object, Boolean)public final void setProperty(java.lang.String name,
double val)
name - String property to set.val - public final void setProperty(java.lang.String name,
float val)
name - String property to set.val - public final void setProperty(java.lang.String name,
int val)
name - String property to set.val - public final void setProperty(java.lang.String name,
long val)
name - String property to set.val - protected final void setValue(java.lang.StringBuilder buff,
java.sql.PreparedStatement stmt,
int pos,
java.lang.String key)
throws java.sql.SQLException
StreamData objects are added as binary streams.buff - query buffer. We append additional info to this to provide
better debug messagesstms - PreparedStatement fo populatepos - statement position to setkey - Field to outputjava.sql.SQLExceptionpublic int findDuplicate()
throws ConsistencyError,
DataFault
ConsistencyErrorDataFaultpublic int findDuplicate(int id,
java.util.Set<java.lang.String> fields,
boolean check_all)
throws ConsistencyError,
DataFault
id - to match (if greater than zero)fields - Set of field names to check.check_all - Should all fields be checked not just the set/dirty fields.ConsistencyErrorDataFaultpublic java.lang.String toString()
toString in class java.util.AbstractMap<java.lang.String,java.lang.Object>public void lock()
throws DataFault
DataFaultpublic boolean isLocked()