public class StoreUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String |
MISSING_VALUE |
static String |
PASSWORD_PLACEHOLDER |
static String |
PASSWORDFIELD_PREFIX |
Modifier and Type | Method and Description |
---|---|
static void |
addEventHandler(PersistentEventHandler<? extends Persistent> handler,
Class<?>... classes)
Add an event handler for the passed persistent classes.
|
static Persistent |
base(Persistent o)
Return the base object of a Persistent o.
|
static boolean |
canBeUsedForSelect(String objectname)
Returns true if the passed objectname exists in the database schema and can be used as select target.
|
void |
checkDataLength(Persistent o)
Check the content of the fields of a persistent against the length constraint of the columns in the database.
|
static void |
cleanDbFieldV() |
static Persistent |
createObjectFromJson(JSONObject job)
Create a persistent object from a json object.
|
static int |
dbAttrPos(Persistent o,
String fieldname) |
static String |
dbAttrs(Persistent o) |
static String |
dbAttrs(Persistent o,
String prefix) |
static int |
dbAttrsCount(Persistent o) |
static List<Field> |
dbFields(Persistent o)
Returns the fields of the object which have corresponding fields in the database.
|
static List<Field> |
dbMultiValuedFields(Persistent o)
Returns the multivalued fields (Lists, Maps, Sets) of the object which have corresponding fields in the database.
|
static void |
executeScript(String content,
PrintWriter res) |
static void |
executeScript(String content,
PrintWriter res,
boolean tryAvoid) |
static void |
executeSQLFile(String commands,
PrintWriter res) |
static int |
executeSQLFile(String commands,
PrintWriter res,
boolean tryAvoid) |
static boolean |
existsColumn(String tablename,
String columnname)
Returns true if the passed table/view exists in the current db-schema and the passed column exists in this table/view.
|
static boolean |
existsConstraint(String tablename,
String constraintname) |
static boolean |
existsIndex(String tablename,
String indexname)
Returns true if the passed index exists in the current db-schema for the passed table.
|
static boolean |
existsObject(Set<String> objectNameSet,
String name) |
static boolean |
existsSchemaObject(String objectname,
String objectType)
Returns true if the passed object exists in the current db-schema.
|
static boolean |
existsTable(String tablename)
Returns true if the passed table exists in the current db-schema.
|
static boolean |
existsView(String viewname)
Returns true if the passed view exists in the current db-schema.
|
static BigDecimal |
getBigDecimal(ResultSet rs,
int columnIndex)
Get a BigDecimal from a column of the current row of a ResultSet.
|
static BigDecimal |
getBigDecimal(ResultSet rs,
String columnName)
Get a BigDecimal from a column of the current row of a ResultSet.
|
static BigInteger |
getBigInteger(ResultSet rs,
int columnIndex)
Get a BigInteger from a column of the current row of a ResultSet.
|
static BigInteger |
getBigInteger(ResultSet rs,
String columnName)
Get a BigInteger from a column of the current row of a ResultSet.
|
static byte[] |
getBlob(ResultSet rs,
int columnIndex)
Get a byte array from a Blob column of the current row of a ResultSet.
|
static InputStream |
getBlobStream(ResultSet rs,
int columnIndex)
Get a String from a Blob column of the current row of a ResultSet.
|
static String |
getBlobString(ResultSet rs,
int columnIndex)
Get a String from a Blob column of the current row of a ResultSet.
|
static String |
getClob(ResultSet rs,
int columnIndex)
Get a String from a Clob column of the current row of a ResultSet.
|
static byte[] |
getClobBytes(ResultSet rs,
int columnIndex)
Get a byte array from a Clob column of the current row of a ResultSet.
|
static DatabaseMetaData |
getDatabaseMetaData()
Retrieves a DatabaseMetaData object that contains metadata about the database
to which the store is connected.
|
static Date |
getDate(ResultSet rs,
int columnIndex)
Get a Date from a column of the current row of a ResultSet.
|
static Date |
getDate(ResultSet rs,
String columnName)
Get a Date from a column of the current row of a ResultSet.
|
static Store |
getDefaultStore() |
static <T extends Persistent> |
getEventHandlers(Class<Persistent> c)
Get a List of
PersistentEventHandler for a specified class c. |
static List<List<Object>> |
getExportedKeys(String tablename)
Retrieves a description of the foreign key columns that reference the given table's
primary key columns (the foreign keys exported by a table).
|
static int |
getFetchSize() |
static Field |
getField(Persistent o,
String fieldname) |
static Object |
getFromRS(ResultSet rs,
int sqlType,
int i) |
static Object |
getFromRS(ResultSet rs,
ResultSetMetaData metaData,
int i) |
static List<List<Object>> |
getImportedKeys(String tablename)
Retrieves a description of the primary key columns that are referenced by the given table's
foreign key columns (the primary keys imported by a table).
|
static Map<String,String> |
getIndexInfo(String tablename) |
static String |
getIndexNameForColumns(String tablename,
String columns,
boolean uniqueOnly)
Returns the name of the index, if one exists for the passed columns of the passed table.
|
static int |
getMaxConstraintIndexNameLength() |
static int |
getMaxTableNameLength() |
static Persistent |
getNewInstance(Class<?> c)
Create a new instance of c. c can be an interface, then
mapInterface(Class) is used to determine the appropriate implementation class. |
static Persistent |
getNewInstance(String className)
Create a new instance of a class c specified via the className .
|
static <T extends Persistent> |
getObject(Object obj)
Returns the
Persistent , if obj is a string like: <classname>:<oid> |
static Set<String> |
getObjectNames(String namePattern,
String objectType)
Returns a set of names of database objects (VIEWs/TABLEs/...) conforming to the name pattern in the current db-schema.
|
static ResultSetMetaData |
getTableDefinition(Class<? extends Persistent> c)
Deprecated.
|
static Set<String> |
getTableNames(String tableNamePattern)
Returns a set of names of tables conforming to the name pattern in the current db-schema.
|
static List<String> |
getTableTypes()
Get the table types of the database. c.f.
|
static Set<String> |
getViewNames(String viewNamePattern)
Returns a set of names of views conforming to the name pattern in the current db-schema.
|
static boolean |
hasClassAttribute(Class<?> c) |
static boolean |
hasDbField(Class<?> c,
String fieldName)
Returns true, if the Class has a field with the given name which corresponds to a column in the database.
|
static boolean |
isSingleInterface(Class<?> c)
Determines if an interface c hides a single implementation (e.g. com.groiss.org.User covers com.dec.avw.core.User)
|
static boolean |
isSystemOid(long oid)
Returns true if the given oid is the oid of a system object (>0, <1000).
|
static <P> Class<Persistent> |
mapInterface(Class<P> c)
Get the Persistent class which is implemented by the a Class c.
|
static Class<Persistent> |
mapInterface(String className)
Get the Persistent class which is implemented by a class with the given classname.
|
static String |
mapInterfaceToClassName(String className)
Get the name of the Persistent class which is implemented by a class with the given classname.
|
static boolean |
mayBeReferenced(String className)
Check, if for this class a foreign key can be defined.
|
static double |
noNaN(double d) |
static float |
noNaN(float f) |
static List<String> |
oidList(List<?> l,
int splitsize)
Creates a list of strings of oids from a list of objects.
|
static PreparedStatement |
prepareStatement(String stmtText)
Prepare a statement against the current connection
The statement is also visible in the DBConnections overview.
|
static void |
removeEventHandler(PersistentEventHandler<?> handler)
Remove a
PersistentEventHandler . |
static void |
setBigDecimal(PreparedStatement stmt,
int pos,
BigDecimal value)
Get a parameter in a PreparedStatement from a BigDecimal.
|
static void |
setBigInteger(PreparedStatement stmt,
int pos,
BigInteger value)
Get a parameter in a PreparedStatement from a BigDecimal.
|
static void |
setBindVars(PreparedStatement stmt,
Object... bindVars)
Set the bind variables of a prepared statement based on an object array.
|
static void |
setFetchsize(int rows) |
static void |
setFieldToValue(Persistent o,
Field ff,
Object val,
Map<String,? extends Object> req)
Set a field of an object
|
static boolean |
setFieldToValue(Persistent o,
Field ff,
Object value,
Map<String,? extends Object> req,
boolean dontLocalizeNumbers)
Set a field of an object
|
static void |
setFromRS(Persistent o,
ResultSet rs)
Set the members of the persistent object to the corresponding values of the current row of the result set.
|
static void |
setFromRS(Persistent o,
ResultSet rs,
boolean addToCache)
Set the members of the persistent object to the corresponding values of the current row of the result set.
|
static void |
setFromRSByName(Persistent o,
ResultSet rs)
Set the members of the persistent object to the corresponding values of the current row of the result set.
|
static void |
setFromRSByName(Persistent o,
ResultSet rs,
boolean addToCache)
Set the members of the persistent object to the corresponding values of the current row of the result set.
|
static boolean |
setNumber(Persistent o,
Field ff,
String val,
boolean dontLocalizeNumbers)
Set a field to a numeric value, the value is given as String.
|
static void |
setValues(Persistent o,
Map<String,? extends Object> req) |
static void |
setValues(Persistent o,
Map<String,? extends Object> req,
Map<String,Short> fieldmodes) |
static boolean |
setValues2(Persistent o,
Map<String,? extends Object> req,
Map<String,Short> fieldmodes,
boolean dontLocalizeNumbers) |
static void |
stmtFinished()
Mark a statement (generated with
prepareStatement(java.lang.String) ) as finished. |
static <T extends Enum<T>> |
toEnum(Class<T> clazz,
Object value)
Maps a value to an instance of an Enum class
|
static JSONObject |
toJson(Persistent pers)
Convert a persistent to JSON
|
static JSONObject |
toJsonAsReference(Persistent pers) |
static Map<String,Object> |
toMap(Persistent pers)
Convert a persistent to a Map
|
static Map<String,Object> |
toMapAsReference(Persistent pers) |
static String |
toNative(String arg) |
static String |
toNativeScript(String arg) |
static String |
toString(Persistent obj)
Generates a String representation to identify a persistent object.
|
static String |
transformCase(DatabaseMetaData dbmd,
String id) |
static String |
transformCase(String id) |
static ApplicationException |
wrapSQLException(int errNum,
Throwable e)
Wraps an exception ; tries to discriminate between sql constraint violations and other exceptions
when sqlexception && sqlstate=23* ==> integrity coinstraint, (appex 478,e)
when sqlexception && sqlstate!
|
public static final String MISSING_VALUE
public static final String PASSWORD_PLACEHOLDER
public static final String PASSWORDFIELD_PREFIX
public static Persistent getNewInstance(Class<?> c)
mapInterface(Class)
is used to determine the appropriate implementation class.c
- the class or interfacepublic static Persistent getNewInstance(String className)
mapInterface(String)
is used to determine the appropriate implementation class.className
- the name of the class or interfacepublic static Persistent base(Persistent o)
View
, its base object is returned, otherwise, o is returned.o
- the View Objectpublic static List<Field> dbFields(Persistent o)
public static boolean hasDbField(Class<?> c, String fieldName)
public static List<Field> dbMultiValuedFields(Persistent o)
public static void cleanDbFieldV()
public static String dbAttrs(Persistent o, String prefix)
public static int dbAttrsCount(Persistent o)
public static String dbAttrs(Persistent o)
public static boolean hasClassAttribute(Class<?> c)
public static int dbAttrPos(Persistent o, String fieldname)
public static void setValues(Persistent o, Map<String,? extends Object> req)
public static void setValues(Persistent o, Map<String,? extends Object> req, Map<String,Short> fieldmodes)
public static boolean setValues2(Persistent o, Map<String,? extends Object> req, Map<String,Short> fieldmodes, boolean dontLocalizeNumbers)
public static boolean setNumber(Persistent o, Field ff, String val, boolean dontLocalizeNumbers) throws Exception
o
- the target objectff
- the field which reveices the valueval
- the valuedontLocalizeNumbers
- if true, don't use the localized number formatterException
public static void setFieldToValue(Persistent o, Field ff, Object val, Map<String,? extends Object> req) throws Exception
o
- ff
- val
- req
- Exception
public static boolean setFieldToValue(Persistent o, Field ff, Object value, Map<String,? extends Object> req, boolean dontLocalizeNumbers) throws Exception
o
- the object to fillff
- the object's fieldvalue
- the value to assignreq
- all other valuesdontLocalizeNumbers
- false
if value may be in a locale-dependent formatException
public static void setFromRSByName(Persistent o, ResultSet rs) throws Exception
o
- The persistent objectrs
- the ResultSetException
public static void setFromRSByName(Persistent o, ResultSet rs, boolean addToCache) throws Exception
o
- The persistent objectrs
- the ResultSetaddToCache
- if true, the object is added to the Thread-Cache.Exception
public static void setFromRS(Persistent o, ResultSet rs) throws Exception
o
- The persistent objectrs
- the ResultSetException
public static void setFromRS(Persistent o, ResultSet rs, boolean addToCache) throws Exception
o
- The persistent objectrs
- the ResultSetaddToCache
- if true, the object is added to the Thread-Cache.Exception
public static Object getFromRS(ResultSet rs, ResultSetMetaData metaData, int i) throws Exception
Exception
public static Object getFromRS(ResultSet rs, int sqlType, int i) throws Exception
Exception
public static Field getField(Persistent o, String fieldname)
public static void addEventHandler(PersistentEventHandler<? extends Persistent> handler, Class<?>... classes)
Persistent
otherwise
no registration for that class will be performed and an error will be logged (but no Exception will be thrown).
The determination of applicable event handlers during read, insert, update and delete operations
is done via the runtime class o.getClass()
of the object o.
In general, registrations for interfaces, abstract classes or superclasses will not work; there will be a log entry at level ERROR of the form "PersistentEventHandler <classname> is NOT being registered ..." in such a case.
One exception from this general rule is if the interface can be mapped to
a well defined single implementation class (i.e. if isSingleInterface(Class)
returns true like for interface com.groiss.org.User).
Another exception is the Persistent
interface itself. Handlers registered for Persistent
will be called for
all objects implementing Persistent. Such handlers must decide themselves if they are applicable or not, e.g. by
using if (o instanceof class/interface) {...}
.
handler
- the PersistentEventHandler
to be called when instances of the classes are manipulated via the store.classes
- the runtime classes for which the handler will be invoked.public static void removeEventHandler(PersistentEventHandler<?> handler)
PersistentEventHandler
. All handlers with the same class as handler get removed.handler
- an instance of the handlers to be removedpublic static <T extends Persistent> List<PersistentEventHandler<T>> getEventHandlers(Class<Persistent> c)
PersistentEventHandler
for a specified class c.c
- the classpublic static Date getDate(ResultSet rs, String columnName) throws SQLException
rs
- the ResultSetcolumnName
- the name of the columnSQLException
public static Date getDate(ResultSet rs, int columnIndex) throws SQLException
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
public static BigDecimal getBigDecimal(ResultSet rs, String columnName) throws SQLException
rs
- the ResultSetcolumnName
- the name of the columnSQLException
public static BigDecimal getBigDecimal(ResultSet rs, int columnIndex) throws SQLException
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
public static void setBigDecimal(PreparedStatement stmt, int pos, BigDecimal value) throws SQLException
stmt
- the PreparedStatementpos
- the number of the parameter in the stmtvalue
- the BigDecimalSQLException
public static BigInteger getBigInteger(ResultSet rs, String columnName) throws SQLException
rs
- the ResultSetcolumnName
- the name of the columnSQLException
public static BigInteger getBigInteger(ResultSet rs, int columnIndex) throws SQLException
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
public static void setBigInteger(PreparedStatement stmt, int pos, BigInteger value) throws SQLException
stmt
- the PreparedStatementpos
- the number of the parameter in the stmtvalue
- the BigDecimalSQLException
public static byte[] getClobBytes(ResultSet rs, int columnIndex) throws Exception
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
Exception
public static byte[] getBlob(ResultSet rs, int columnIndex) throws Exception
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
Exception
public static String getClob(ResultSet rs, int columnIndex) throws Exception
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
Exception
public static String getBlobString(ResultSet rs, int columnIndex) throws Exception
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
Exception
public static InputStream getBlobStream(ResultSet rs, int columnIndex) throws Exception
rs
- the ResultSetcolumnIndex
- the index of the columnSQLException
Exception
public static PreparedStatement prepareStatement(String stmtText) throws SQLException
stmtFinished()
to let is disappear from this overview.stmtText
- the text of the SQL statementSQLException
public static void stmtFinished()
prepareStatement(java.lang.String)
) as finished.public static void setBindVars(PreparedStatement stmt, Object... bindVars)
stmt
- the prepared statementbindVars
- the list of bind variables, may contain Nil
NullExpression
and wrappers.public static boolean existsTable(String tablename) throws Exception
Only ordinary tables (objects with type 'TABLE') are considered; objects with other types like 'SYNONYM' or 'SYSTEM TABLE' will not be found.
tablename
- the name of the table to be checked.true
if a table with the given name exists, else false
Exception
public static boolean canBeUsedForSelect(String objectname)
objectname
- the name of the objecttrue
if the object can be used in a "select ... from objectname" statement, else false
public static boolean existsView(String viewname) throws Exception
Only ordinary views (objects with type 'VIEW') are considered; objects with other types like 'SYNONYM' or 'MATERIALIZED VIEW' will not be found.
viewname
- the name of the view to be checked.true
if a view with the given name exists, else false
Exception
public static boolean existsSchemaObject(String objectname, String objectType) throws Exception
objectType
- type of object (e.g "VIEW" or "TABLE") like returned from DatabaseMetaData.getTableTypes()
,
null
returns all types of objectstrue
if an object with the given name (and type) exists, else false
Exception
public static boolean existsColumn(String tablename, String columnname) throws Exception
tablename
- the name of the table/view to be checked.columnname
- the name of the column to be checked.true
if a the specified column exists in the specified table, else false
Exception
public static Set<String> getTableNames(String tableNamePattern) throws Exception
Only ordinary tables (objects with type 'TABLE') are considered; objects with other types like 'SYNONYM' or 'SYSTEM TABLE' will not be found.
tableNamePattern
- the pattern for the table names to match against.Exception
public static Set<String> getViewNames(String viewNamePattern) throws Exception
Only ordinary views (objects with type 'VIEW') are considered; objects with other types like 'SYNONYM' or 'MATERIALIZED VIEW' will not be found.
viewNamePattern
- the pattern for the view names to match against.Exception
public static Set<String> getObjectNames(String namePattern, String objectType) throws Exception
namePattern
- the pattern for the object names to match against.objectType
- type of object (e.g "VIEW" or "TABLE") like returned from DatabaseMetaData.getTableTypes()
,
null
returns all types of objectsException
public static List<String> getTableTypes() throws Exception
DatabaseMetaData.getTableTypes()
Exception
public static List<List<Object>> getExportedKeys(String tablename) throws Exception
tablename
- the name of the tableException
public static boolean existsConstraint(String tablename, String constraintname) throws Exception
Exception
public static List<List<Object>> getImportedKeys(String tablename) throws Exception
tablename
- the name of the tableException
public static boolean existsIndex(String tablename, String indexname) throws Exception
indexname
- the name of the index to be checked.tablename
- the name of the table to be checked.true
if a the specified index exists in the specified table, else false
Exception
public static String getIndexNameForColumns(String tablename, String columns, boolean uniqueOnly) throws Exception
columns
- comma separated list of column names.tablename
- the name of the table to be checked.uniqueOnly
- when true, return only indices for unique values;
when false, return indices regardless of whether unique or notException
public static Map<String,String> getIndexInfo(String tablename) throws Exception
Exception
public static String transformCase(DatabaseMetaData dbmd, String id) throws Exception
Exception
public static int getMaxTableNameLength()
public static int getMaxConstraintIndexNameLength()
public static <P> Class<Persistent> mapInterface(Class<P> c)
P
- c
- interface to determine the underlying class forpublic static Class<Persistent> mapInterface(String className) throws ClassNotFoundException
User
interface to the com.dec.avw.core.User class.
If c is no such mappable interface, then an instance of the class named className is returned (cast to a Persistent)className
- the name of the interface to determine the underlying class forClassNotFoundException
public static String mapInterfaceToClassName(String className)
User
interface to the com.dec.avw.core.User class.
If c is no such mappable interface, then className is returned.className
- the name of the interface to determine the underlying className forpublic static boolean isSingleInterface(Class<?> c)
c
- the interface to checkpublic static boolean mayBeReferenced(String className) throws ClassNotFoundException
className
- name of a classClassNotFoundException
public static void executeScript(String content, PrintWriter res) throws Exception
Exception
public static void executeScript(String content, PrintWriter res, boolean tryAvoid) throws Exception
Exception
public static void executeSQLFile(String commands, PrintWriter res) throws Exception
Exception
public static int executeSQLFile(String commands, PrintWriter res, boolean tryAvoid) throws Exception
Exception
public static <T extends Persistent> T getObject(Object obj)
Persistent
, if obj is a string like: <classname>:<oid>obj
- A string like <classname>:<oid>.Persistent
public static String toString(Persistent obj)
obj
- the persistent objectpublic static boolean isSystemOid(long oid)
oid
- public static double noNaN(double d)
public static float noNaN(float f)
public static <T extends Enum<T>> T toEnum(Class<T> clazz, Object value)
T
- The resulting Enum Typeclazz
- the enum Classvalue
- the value to MapDefinedEnum
, the definedValue() is matched against,
if not, Enum.name() is matched against (so then it is basically the same as Enum.valueOf(Class, String)
.public static void setFetchsize(int rows)
public static int getFetchSize()
public static Store getDefaultStore()
@Deprecated public static ResultSetMetaData getTableDefinition(Class<? extends Persistent> c)
c
- the Persistent classpublic static JSONObject toJson(Persistent pers) throws Exception
Exception
public static Persistent createObjectFromJson(JSONObject job)
public static Map<String,Object> toMap(Persistent pers) throws Exception
Exception
public static JSONObject toJsonAsReference(Persistent pers)
public static Map<String,Object> toMapAsReference(Persistent pers)
public static ApplicationException wrapSQLException(int errNum, Throwable e)
when sqlexception && sqlstate=23* ==> integrity coinstraint, (appex 478,e)
when sqlexception && sqlstate!=23* ==> appex(errNum,e)
when appex ==> appex
otherwise: ==> new Appex(errNum,e)
errNum
- e
- public static DatabaseMetaData getDatabaseMetaData() throws SQLException
SQLException
- if a database access error occurspublic static List<String> oidList(List<?> l, int splitsize)
l
- the list of objectssplitsize
- the maximum number of entries per list element (chunk size). If 0 is used, a suitable predefined size
is being used.public void checkDataLength(Persistent o)
o
- the persistent object to check@enterprise 10.0.39049 Copyright © 2024 FREQUENTIS AG. All Rights Reserved.