Package ch.autumo.commons.utils.database
Class DBUtils
java.lang.Object
ch.autumo.commons.utils.database.DBUtils
DB Utils.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final org.slf4j.LoggerLogger.static intMaximum of referenced records to be loaded.static final intSorting of foreign entities by ID.static final intSorting of foreign entities by display value. -
Method Summary
Modifier and TypeMethodDescriptionstatic intCount rows of type clz (entity class).static intCount rows of table.static voidDelete a record.static voidDelete a many-to-many-relation record.static voidDelete a record.static voidDelete a record.static voidDelete a record.static voiddelete(Connection conn, Model model, Set<String> foreignDbKeys) Delete a many-to-many-relation record.static voiddelete(Connection conn, Entity entity) Delete a record.static voiddelete(Connection conn, String entity, int id) Delete a record.static StringescapeValuesForDb(String value) Escape single value for DB.static StringgetBooleanDatabaseMappingValue(boolean value) Get correct DB value for a boolean.static StringgetDisplayValue(Class<?> entityClass, int id) Get display value: <ID:displayValue>.getDisplayValues(Class<?> entityClass) Get display values: <ID:displayValue>.getDisplayValues(Class<?> entityClass, int amount) Get display values: <ID:displayValue>.getDisplayValues(Class<?> entityClass, int amount, int sortType) Get display values: <ID:displayValue>.static StringAccess result set value, no escape of value.static IntegerInsert new entity.static Integerinsert(Connection conn, Entity entity, String columns, String values) Insert new entity.static booleanisSqlBinaryType(int sqlType) Check if this SQL type is a html-input binary type.static booleanisSqlBooelanType(int sqlType) Check if this SQL type is a html-input boolean type.static booleanisSqlDateTimeType(int sqlType) Check if this SQL type is a html-input date type.static booleanisSqlNumberType(int sqlType) Check if this SQL type is a html-input number type.static booleanisSqlTextType(int sqlType) Check if this SQL type is a html-input text type.static ConnectionGet an new global DB connection.static Timestampnow()Now time-stamp.Select a records of type entityClass (entity class).static voidRetire a global DB connection.static ModelselectRecord(Class<?> entityClass, int id) Select a record of type clz (entity class).selectRecords(Class<?> entityClass) Select a records of type entityClass (entity class).selectRecords(Class<?> entityClass, int amount) Select a records of type entityClass (entity class).selectRecords(Class<?> entityClass, int amount, int sortType) Select a records of type entityClass (entity class).selectRecords(Class<?> entityClass, String condition, Object[] values) Select a records of type entityClass (entity class).selectRecords(Class<?> entityClass, String condition, Object[] values, int amount) Select a records of type entityClass (entity class).selectRecords(Class<?> entityClass, String condition, Object[] values, int amount, int sortType) Select a records of type entityClass (entity class).static TimestampGet time-stamp.static voidUpdate entity.static voidupdate(Connection conn, Entity entity, String columns, String values) Update entity.static voidUpdate the given model with entity from database, if it hasn't been updated yet.static voidupdateSecretUserKey(int userId, String newSecretUserKey) Update secret user key.
-
Field Details
-
LOG
protected static final org.slf4j.Logger LOGLogger. -
maxRefRecords
public static int maxRefRecordsMaximum of referenced records to be loaded. -
SORT_BY_ID
public static final int SORT_BY_IDSorting of foreign entities by ID.- See Also:
-
SORT_BY_VALUE
public static final int SORT_BY_VALUESorting of foreign entities by display value.- See Also:
-
-
Method Details
-
getDisplayValues
Get display values: <ID:displayValue>. Max. 200 records to be returned.- Parameters:
entityClass- class- Returns:
- entries
- Throws:
Exception- exception
-
getDisplayValues
public static Map<Integer,String> getDisplayValues(Class<?> entityClass, int amount) throws Exception Get display values: <ID:displayValue>. Max. 200 records to be returned.- Parameters:
entityClass- classamount- max. amount of records to be loaded- Returns:
- entries
- Throws:
SQLException- SQL exceptionException
-
getDisplayValues
public static Map<Integer,String> getDisplayValues(Class<?> entityClass, int amount, int sortType) throws Exception Get display values: <ID:displayValue>. -
getDisplayValue
Get display value: <ID:displayValue>.- Parameters:
entityClass- classid- id- Returns:
- entry
- Throws:
Exception- exception
-
escapeValuesForDb
Escape single value for DB.- Parameters:
value- value- Returns:
- escaped value
-
updateSecretUserKey
Update secret user key.- Parameters:
userId- DB user idnewSecretUserKey- new secret user key- Throws:
SQLException- SQL exception
-
countRows
Count rows of type clz (entity class).- Parameters:
clz- entity class- Returns:
- amount of rows or -1 if something bad happens
- Throws:
SQLException- SQL exception
-
countRows
Count rows of table.- Parameters:
table- table DB name- Returns:
- amount of rows or -1 if something bad happens
- Throws:
SQLException- SQL exception
-
selectRecord
Select a record of type clz (entity class).- Parameters:
entityClass- entity classid- DB record id- Returns:
- entity bean
- Throws:
Exception- if selection fails
-
selectRecords
Select a records of type entityClass (entity class). Max. 200 records to be returned.- Parameters:
entityClass- class- Returns:
- entity beans
- Throws:
Exception- exception
-
selectRecords
Select a records of type entityClass (entity class). Max. 200 records to be returned.- Parameters:
entityClass- classamount- max. amount of records to be loaded- Returns:
- entity beans
- Throws:
Exception- exception
-
selectRecords
public static List<Model> selectRecords(Class<?> entityClass, int amount, int sortType) throws Exception Select a records of type entityClass (entity class). -
selectRecords
public static List<Model> selectRecords(Class<?> entityClass, String condition, Object[] values) throws Exception Select a records of type entityClass (entity class).- Parameters:
entityClass- classcondition- condition for where clause, e.g. 'age >= ? AND gender = ?'values- values for the condition- Returns:
- entity beans
- Throws:
Exception- exception
-
selectRecords
public static List<Model> selectRecords(Class<?> entityClass, String condition, Object[] values, int amount) throws Exception Select a records of type entityClass (entity class).- Parameters:
entityClass- classcondition- condition for where clause, e.g. 'age >= ? AND gender = ?'values- values for the conditionamount- max. amount of records to be loaded- Returns:
- entity beans
- Throws:
Exception- exception
-
selectRecords
public static List<Model> selectRecords(Class<?> entityClass, String condition, Object[] values, int amount, int sortType) throws Exception Select a records of type entityClass (entity class). -
query
public static List<Model> query(Class<?> entityClass, String fullQuery, Object[] values) throws SQLException Select a records of type entityClass (entity class). Respect the database dialect used for the underlying database.- Parameters:
entityClass- entity class; must match the table in the queryfullQuery- the full SQL query with query place-holdersvalues- values for the query- Returns:
- entity beans
- Throws:
SQLException- SQL exception
-
delete
Delete a many-to-many-relation record.- Parameters:
model- modelforeignDbKeys- DB foreign keys as given by the keys within the return value of the model methodModel.getForeignReferences()- Throws:
SQLException- SQL exception
-
delete
Delete a record.- Parameters:
model- model- Throws:
SQLException- SQL exception
-
delete
Delete a record.- Parameters:
entity- entity- Throws:
SQLException- SQL exception
-
delete
Delete a record.- Parameters:
entityClass- entity classid- if- Throws:
SQLException- SQL exception
-
delete
Delete a record.- Parameters:
entity- entity table nameid- if- Throws:
SQLException- SQL exception
-
update
Update entity.- Parameters:
entity- entitycolumns- columns; "a,b,c".values- values; "'1','2','3'".- Throws:
SQLException- SQL exception
-
insert
Insert new entity.- Parameters:
entity- entitycolumns- columns; "a,b,c".values- values; "'1','2','3'".- Returns:
- generated id id of newly inserted entity
- Throws:
SQLException- SQL exception
-
timestamp
Get time-stamp.- Parameters:
date- date- Returns:
- time-stamp.
-
now
Now time-stamp.- Returns:
- time-stamp.
-
getValue
Access result set value, no escape of value.- Parameters:
set- result setdbColumnName- db column name- Returns:
- escaped db value
- Throws:
SQLException- SQL exception
-
isSqlTextType
public static boolean isSqlTextType(int sqlType) Check if this SQL type is a html-input text type.- Parameters:
sqlType- SQL type- Returns:
- true if it is a html-input text type.
-
isSqlNumberType
public static boolean isSqlNumberType(int sqlType) Check if this SQL type is a html-input number type.- Parameters:
sqlType- SQL type- Returns:
- true if it is a html-input number type.
-
isSqlDateTimeType
public static boolean isSqlDateTimeType(int sqlType) Check if this SQL type is a html-input date type.- Parameters:
sqlType- SQL type- Returns:
- true if it is a html-input date type.
-
isSqlBinaryType
public static boolean isSqlBinaryType(int sqlType) Check if this SQL type is a html-input binary type.- Parameters:
sqlType- SQL type- Returns:
- true if it is a html-input binary type.
-
isSqlBooelanType
public static boolean isSqlBooelanType(int sqlType) Check if this SQL type is a html-input boolean type.- Parameters:
sqlType- SQL type- Returns:
- true if it is a html-input boolean type.
-
getBooleanDatabaseMappingValue
Get correct DB value for a boolean.- Parameters:
value- boolean value- Returns:
- DB boolean value as string
-
updateModel
public static void updateModel(Entity entity, Map<String, Map<String, throws SQLExceptionDBField>> model) Update the given model with entity from database, if it hasn't been updated yet.- Parameters:
entity- entitymodel- model- Throws:
SQLException- SQL exception
-
newGlobalConnection
Get an new global DB connection. You have to roll back or commit the transaction, before you retire it withretireGlobalConnection(Connection). If you useDBUtilsroll-backs are done automatically and you'll receive anSQLException. Don't close it by yourself!- Returns:
- global DB connection
- Throws:
SQLException- SQL exception
-
retireGlobalConnection
Retire a global DB connection.- Parameters:
conn- connection- Throws:
SQLException- SQL exception- See Also:
-
insert
public static Integer insert(Connection conn, Entity entity, String columns, String values) throws SQLException Insert new entity.- Parameters:
conn- global connectionentity- entitycolumns- columns; "a,b,c".values- values; "'1','2','3'".- Returns:
- generated id
- Throws:
SQLException- SQL Exception
-
update
public static void update(Connection conn, Entity entity, String columns, String values) throws SQLException Update entity.- Parameters:
conn- global connectionentity- entitycolumns- columns; "a,b,c".values- values; "'1','2','3'".- Throws:
SQLException- SQL Exception
-
delete
Delete a record.- Parameters:
conn- global connectionentity- entity table nameid- if- Throws:
SQLException- SQL Exception
-
delete
public static void delete(Connection conn, Model model, Set<String> foreignDbKeys) throws SQLException Delete a many-to-many-relation record.- Parameters:
conn- global connectionmodel- modelforeignDbKeys- DB foreign keys as given by the keys within the return value of the model methodModel.getForeignReferences()- Throws:
SQLException- SQL Exception
-
delete
Delete a record.- Parameters:
conn- global connectionentity- entity- Throws:
SQLException- SQL Exception
-