/** * <p>Calculate total rows for pagination.</p> * @param <T> - type of business object, * @param pEntityClass entity class * @return Integer row count * @throws Exception - an exception */ @Override public final <T> Integer evalRowCount( final Class<T> pEntityClass) throws Exception { String query = "select count(*) as TOTALROWS from " + pEntityClass .getSimpleName().toUpperCase() + ";"; return getSrvDatabase().evalIntegerResult(query, "TOTALROWS"); }
/** * <p>Delete entity with condition, e.g. complex ID.</p> * @param <T> entity type * @param pEntityClass entity class * @param pWhere Not Null e.g. "WAREHOUSESITE=1 and PRODUCT=1" * @throws Exception - an exception **/ @Override public final <T> void deleteEntityWhere( final Class<T> pEntityClass, final String pWhere) throws Exception { if (pWhere == null) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "param_null_not_accepted"); } getSrvDatabase().executeDelete(pEntityClass.getSimpleName() .toUpperCase(), pWhere); }
/** * <p>Delete entity with condition, e.g. complex ID.</p> * @param <T> entity type * @param pAddParam additional param * @param pEntityClass entity class * @param pWhere Not Null e.g. "WAREHOUSESITE=1 and PRODUCT=1" * @throws Exception - an exception **/ @Override public final <T> void deleteEntityWhere(final Map<String, Object> pAddParam, final Class<T> pEntityClass, final String pWhere) throws Exception { if (pWhere == null) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "param_null_not_accepted"); } getSrvDatabase().executeDelete(pEntityClass.getSimpleName() .toUpperCase(), pWhere); }
/** * <p>Init database birth and ID birth of entity.</p> * @param pEntity entity * @throws Exception an exception **/ public final void initPersistableBase(final Object pEntity) throws Exception { Field fieldIdDbBirth = getUtlReflection().retrieveField(pEntity.getClass(), APersistableBase.ID_DATABASE_BIRTH_NAME); fieldIdDbBirth.setAccessible(true); if (getSrvDatabase() != null) { //for test purpose, otherwise it must be set fieldIdDbBirth.set(pEntity, getSrvDatabase().getIdDatabase()); } Field fieldIdBirth = getUtlReflection().retrieveField(pEntity.getClass(), APersistableBase.ID_BIRTH_NAME); fieldIdBirth.setAccessible(true); fieldIdBirth.set(pEntity, null); }
/** * <p>Calculate total rows for pagination.</p> * @param <T> - type of business object, * @param pEntityClass entity class * @param pWhere not null e.g. "ITSID > 33" * @return Integer row count * @throws Exception - an exception */ @Override public final <T> Integer evalRowCountWhere( final Class<T> pEntityClass, final String pWhere) throws Exception { if (pWhere == null) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "param_null_not_accepted"); } String query = "select count(*) as TOTALROWS from " + pEntityClass .getSimpleName().toUpperCase() + " where " + pWhere + ";"; return getSrvDatabase().evalIntegerResult(query, "TOTALROWS"); }
/** * <p>Delete entity from DB by given NON-COMPLEX ID.</p> * @param <T> entity type * @param pEntityClass entity class * @param pId ID * @throws Exception - an exception **/ @Override public final <T> void deleteEntity( final Class<T> pEntityClass, final Object pId) throws Exception { TableSql tableSql = this.getTablesMap().get(pEntityClass.getSimpleName()); String idStr; if (pId instanceof String) { idStr = "'" + pId.toString() + "'"; } else { idStr = pId.toString(); } getSrvDatabase().executeDelete(pEntityClass.getSimpleName().toUpperCase(), tableSql.getIdName() + "=" + idStr); }
/** * <p>Delete entity from DB.</p> * @param <T> entity type * @param pAddParam additional param * @param <K> ID type * @param pEntity entity * @throws Exception - an exception **/ @Override public final <T> void deleteEntity( final Map<String, Object> pAddParam, final T pEntity) throws Exception { pAddParam.put("isOnlyId", Boolean.TRUE); ColumnsValues columnsValues = evalColumnsValues(pAddParam, pEntity); pAddParam.remove("isOnlyId"); String whereId = evalWhereId(pEntity, columnsValues); getSrvDatabase().executeDelete(pEntity.getClass() .getSimpleName().toUpperCase(), whereId); }
/** * <p>Calculate total rows for pagination by custom query.</p> * @param <T> - type of business object, * @param pAddParam additional param * @param pEntityClass entity class * @param pQuery not null custom query with named TOTALROWS * @return Integer row count * @throws Exception - an exception */ @Override public final <T> Integer evalRowCountByQuery( final Map<String, Object> pAddParam, final Class<T> pEntityClass, final String pQuery) throws Exception { if (pQuery == null) { throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER, "param_null_not_accepted"); } return getSrvDatabase().evalIntegerResult(pQuery, "TOTALROWS"); }
/** * <p>Update entity in DB.</p> * @param <T> entity type * @param pEntity entity * @throws Exception - an exception **/ @Override public final <T> void updateEntity( final T pEntity) throws Exception { ColumnsValues columnsValues = evalColumnsValuesAndFillNewVersion(pEntity); String whereStr = evalWhereForUpdate(pEntity, columnsValues); columnsValues.getLongsMap().remove(ISrvOrm.VERSIONOLD_NAME); TableSql tableSql = this.tablesMap.get(pEntity.getClass().getSimpleName()); columnsValues.getLongsMap().remove(tableSql.getIdName()); int result = getSrvDatabase().executeUpdate(pEntity.getClass() .getSimpleName().toUpperCase(), columnsValues, whereStr); if (result != 1) { if (result == 0 && columnsValues.ifContains(ISrvOrm.VERSION_NAME)) { throw new ExceptionWithCode(ISrvDatabase.DIRTY_READ, "dirty_read"); } else { String query = hlpInsertUpdate.evalSqlUpdate(pEntity.getClass() .getSimpleName().toUpperCase(), columnsValues, whereStr); throw new ExceptionWithCode(ISrvDatabase.ERROR_INSERT_UPDATE, "It should be 1 row updated but it was " + result + ", query:\n" + query); } } }
TableSql tableSql = this.tablesMap.get(pEntity.getClass().getSimpleName()); columnsValues.getLongsMap().remove(tableSql.getIdName()); int result = getSrvDatabase().executeUpdate(pEntity.getClass() .getSimpleName().toUpperCase(), columnsValues, pWhere); if (result != 1) {
+ pEntityClass.getSimpleName().toUpperCase() + "." + ownerFieldName.toUpperCase() + "=" + ownerIdStr + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query); if (recordSet.moveToFirst()) { do {
/** * <p>Update entity in DB.</p> * @param <T> entity type * @param pAddParam additional param * @param pEntity entity * @throws Exception - an exception **/ @Override public final <T> void updateEntity( final Map<String, Object> pAddParam, final T pEntity) throws Exception { ColumnsValues columnsValues = evalColumnsValues(pAddParam, pEntity); String whereStr = evalWhereForUpdate(pEntity, columnsValues); prepareColumnValuesForUpdate(columnsValues, pEntity); int result = getSrvDatabase().executeUpdate(pEntity.getClass() .getSimpleName().toUpperCase(), columnsValues, whereStr); if (result != 1) { if (result == 0 && columnsValues.ifContains(VERSION_NAME)) { throw new ExceptionWithCode(ISrvDatabase.DIRTY_READ, "dirty_read"); } else { String query = hlpInsertUpdate.evalSqlUpdate(pEntity.getClass() .getSimpleName().toUpperCase(), columnsValues, whereStr); throw new ExceptionWithCode(ISrvDatabase.ERROR_INSERT_UPDATE, "It should be 1 row updated but it was " + result + ", query:\n" + query); } } }
/** * <p>Retrieve a list of all entities.</p> * @param <T> - type of business object, * @param pEntityClass entity class * @return list of all business objects or empty list, not null * @throws Exception - an exception */ @Override public final <T> List<T> retrieveList( final Class<T> pEntityClass) throws Exception { List<T> result = new ArrayList<T>(); IRecordSet<RS> recordSet = null; try { String query = evalSqlSelect(pEntityClass) + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query); if (recordSet.moveToFirst()) { do { result.add(retrieveEntity(pEntityClass, recordSet)); } while (recordSet.moveToNext()); } } finally { if (recordSet != null) { recordSet.close(); } } return result; }
String query = evalSqlSelect(pEntityClass) + " limit " + pPageSize + " offset " + pFirst + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query); if (recordSet.moveToFirst()) { do {
/** * <p>Retrieve entity from DB.</p> * @param <T> entity type * @param pEntityClass entity class * @param pQuery SELECT statement * @return entity or null * @throws Exception - an exception **/ @Override public final <T> T retrieveEntity( final Class<T> pEntityClass, final String pQuery) throws Exception { T entity = null; IRecordSet<RS> recordSet = null; try { recordSet = getSrvDatabase().retrieveRecords(pQuery); if (recordSet.moveToFirst()) { entity = retrieveEntity(pEntityClass, recordSet); } } finally { if (recordSet != null) { recordSet.close(); } } return entity; }
/** * <p>Retrieve entity from DB.</p> * @param <T> entity type * @param pAddParam additional param, e.g. already retrieved TableSql * @param pEntityClass entity class * @param pQuery SELECT statement * @return entity or null * @throws Exception - an exception **/ public final <T> T retrieveEntity(final Map<String, Object> pAddParam, final Class<T> pEntityClass, final String pQuery) throws Exception { T entity = null; IRecordSet<RS> recordSet = null; try { recordSet = getSrvDatabase().retrieveRecords(pQuery); if (recordSet.moveToFirst()) { entity = retrieveEntity(pAddParam, pEntityClass, recordSet); } } finally { if (recordSet != null) { recordSet.close(); } } return entity; }
IRecordSet<RS> recordSet = null; try { recordSet = getSrvDatabase().retrieveRecords(pQuery + " limit " + pPageSize + " offset " + pFirst + ";\n"); if (recordSet.moveToFirst()) {
IRecordSet<RS> recordSet = null; try { recordSet = getSrvDatabase().retrieveRecords(pQuery); if (recordSet.moveToFirst()) { do {
String query = evalSqlSelect(pEntityClass) + " " + pQueryConditions + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query); if (recordSet.moveToFirst()) { do {
String query = evalSqlSelect(pEntityClass) + " " + pQueryConditions + " limit " + pPageSize + " offset " + pFirst + ";\n"; recordSet = getSrvDatabase().retrieveRecords(query); if (recordSet.moveToFirst()) { do {