📄 genericdao.java
字号:
} // construct the source entity qualifier // get the fields from relation description kmsize = modelRelationOne.getKeyMapsSize(); Map bindMap = FastMap.newInstance(); for (int i = 0; i < kmsize; i++) { // get the equivalent column names in the relation ModelKeyMap mkm = modelRelationOne.getKeyMap(i); String sfldname = mkm.getFieldName(); String lfldname = mkm.getRelFieldName(); ModelField amf = modelEntityOne.getField(lfldname); String lcolname = amf.getColName(); Object rvalue = value.get(sfldname); bindMap.put(amf, rvalue); // construct one condition if (wheresb.length() > 0) { wheresb.append(" AND "); } wheresb.append(atable + "." + lcolname + " = ? "); } // construct a join sql query StringBuffer sqlsb = new StringBuffer(); sqlsb.append("SELECT "); sqlsb.append(selsb.toString()); sqlsb.append(" FROM "); sqlsb.append(atable + ", " + ttable); sqlsb.append(" WHERE "); sqlsb.append(wheresb.toString()); sqlsb.append(SqlJdbcUtil.makeOrderByClause(modelEntityTwo, orderBy, true, datasourceInfo)); // now execute the query List retlist = FastList.newInstance(); GenericDelegator gd = value.getDelegator(); try { sqlP.prepareStatement(sqlsb.toString()); Set entrySet = bindMap.entrySet(); for (Iterator iterator = entrySet.iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); ModelField mf = (ModelField) entry.getKey(); Object curvalue = entry.getValue(); SqlJdbcUtil.setValue(sqlP, mf, modelEntityOne.getEntityName(), curvalue, modelFieldTypeReader); } sqlP.executeQuery(); //int collsize = collist.size(); while (sqlP.next()) { GenericValue gv = gd.makeValue(modelEntityTwo.getEntityName(), Collections.EMPTY_MAP); // loop thru all columns for in one row int idx = 1; Iterator fldIter = fldlist.iterator(); while (fldIter.hasNext()) { String fldname = (String) fldIter.next(); ModelField mf = modelEntityTwo.getField(fldname); SqlJdbcUtil.getValue(sqlP.getResultSet(), idx, mf, gv, modelFieldTypeReader); idx++; } retlist.add(gv); } } finally { sqlP.close(); } return retlist; } public long selectCountByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition) throws GenericEntityException { if (modelEntity == null) { return 0; } // if no find options passed, use default EntityFindOptions findOptions = new EntityFindOptions(); boolean verboseOn = Debug.verboseOn(); if (verboseOn) { // put this inside an if statement so that we don't have to generate the string when not used... Debug.logVerbose("Doing selectListIteratorByCondition with whereEntityCondition: " + whereEntityCondition, module); } StringBuffer sqlBuffer = new StringBuffer("SELECT "); if (findOptions.getDistinct()) { sqlBuffer.append("DISTINCT "); } sqlBuffer.append("COUNT(*) "); // FROM clause and when necessary the JOIN or LEFT JOIN clause(s) as well sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, datasourceInfo)); // WHERE clause StringBuffer whereString = new StringBuffer(); String entityCondWhereString = ""; List whereEntityConditionParams = FastList.newInstance(); if (whereEntityCondition != null) { entityCondWhereString = whereEntityCondition.makeWhereString(modelEntity, whereEntityConditionParams); } String viewClause = SqlJdbcUtil.makeViewWhereClause(modelEntity, datasourceInfo.joinStyle); if (viewClause.length() > 0) { if (entityCondWhereString.length() > 0) { whereString.append("("); whereString.append(entityCondWhereString); whereString.append(") AND "); } whereString.append(viewClause); } else { whereString.append(entityCondWhereString); } if (whereString.length() > 0) { sqlBuffer.append(" WHERE "); sqlBuffer.append(whereString.toString()); } // GROUP BY clause for view-entity if (modelEntity instanceof ModelViewEntity) { ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity; String groupByString = modelViewEntity.colNameString(modelViewEntity.getGroupBysCopy(), ", ", "", false); if (UtilValidate.isNotEmpty(groupByString)) { sqlBuffer.append(" GROUP BY "); sqlBuffer.append(groupByString); } } // HAVING clause String entityCondHavingString = ""; List havingEntityConditionParams = FastList.newInstance(); if (havingEntityCondition != null) { entityCondHavingString = havingEntityCondition.makeWhereString(modelEntity, havingEntityConditionParams); } if (entityCondHavingString.length() > 0) { sqlBuffer.append(" HAVING "); sqlBuffer.append(entityCondHavingString); } String sql = sqlBuffer.toString(); SQLProcessor sqlP = new SQLProcessor(helperName); sqlP.prepareStatement(sql, findOptions.getSpecifyTypeAndConcur(), findOptions.getResultSetType(), findOptions.getResultSetConcurrency()); if (verboseOn) { // put this inside an if statement so that we don't have to generate the string when not used... Debug.logVerbose("Setting the whereEntityConditionParams: " + whereEntityConditionParams, module); } // set all of the values from the Where EntityCondition Iterator whereEntityConditionParamsIter = whereEntityConditionParams.iterator(); while (whereEntityConditionParamsIter.hasNext()) { EntityConditionParam whereEntityConditionParam = (EntityConditionParam) whereEntityConditionParamsIter.next(); SqlJdbcUtil.setValue(sqlP, whereEntityConditionParam.getModelField(), modelEntity.getEntityName(), whereEntityConditionParam.getFieldValue(), modelFieldTypeReader); } if (verboseOn) { // put this inside an if statement so that we don't have to generate the string when not used... Debug.logVerbose("Setting the havingEntityConditionParams: " + havingEntityConditionParams, module); } // set all of the values from the Having EntityCondition Iterator havingEntityConditionParamsIter = havingEntityConditionParams.iterator(); while (havingEntityConditionParamsIter.hasNext()) { EntityConditionParam havingEntityConditionParam = (EntityConditionParam) havingEntityConditionParamsIter.next(); SqlJdbcUtil.setValue(sqlP, havingEntityConditionParam.getModelField(), modelEntity.getEntityName(), havingEntityConditionParam.getFieldValue(), modelFieldTypeReader); } try { sqlP.executeQuery(); long count = 0; ResultSet resultSet = sqlP.getResultSet(); if (resultSet.next()) { count = resultSet.getLong(1); } return count; } catch (SQLException e) { throw new GenericDataSourceException("Error getting count value", e); } finally { sqlP.close(); } } /* ====================================================================== */ /* ====================================================================== */ public int delete(GenericEntity entity) throws GenericEntityException { SQLProcessor sqlP = new SQLProcessor(helperName); try { return delete(entity, sqlP); } catch (GenericDataSourceException e) { sqlP.rollback(); throw new GenericDataSourceException("Exception while deleting the following entity: " + entity.toString(), e); } finally { sqlP.close(); } } public int delete(GenericEntity entity, SQLProcessor sqlP) throws GenericEntityException { ModelEntity modelEntity = entity.getModelEntity(); if (modelEntity == null) { throw new GenericModelException("Could not find ModelEntity record for entityName: " + entity.getEntityName()); } if (modelEntity instanceof ModelViewEntity) { throw new org.ofbiz.entity.GenericNotImplementedException("Operation delete not supported yet for view entities"); } String sql = "DELETE FROM " + modelEntity.getTableName(datasourceInfo) + " WHERE " + SqlJdbcUtil.makeWhereStringFromFields(modelEntity.getPksCopy(), entity, "AND"); int retVal; try { sqlP.prepareStatement(sql); SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader); retVal = sqlP.executeUpdate(); entity.removedFromDatasource(); } finally { sqlP.close(); } return retVal; } public int deleteByCondition(ModelEntity modelEntity, EntityCondition condition) throws GenericEntityException { SQLProcessor sqlP = new SQLProcessor(helperName); try { return deleteByCondition(modelEntity, condition, sqlP); } catch (GenericDataSourceException e) { sqlP.rollback(); throw new GenericDataSourceException("Generic Entity Exception occured in deleteByCondition", e); } finally { sqlP.close(); } } public int deleteByCondition(ModelEntity modelEntity, EntityCondition condition, SQLProcessor sqlP) throws GenericEntityException { if (modelEntity == null || condition == null) return 0; if (modelEntity instanceof ModelViewEntity) { throw new org.ofbiz.entity.GenericNotImplementedException("Operation deleteByCondition not supported yet for view entities"); } String sql = "DELETE FROM " + modelEntity.getTableName(datasourceInfo); sql += " WHERE " + condition.makeWhereString(modelEntity, null); try { sqlP.prepareStatement(sql); return sqlP.executeUpdate(); } finally { sqlP.close(); } } /* ====================================================================== */ public void checkDb(Map modelEntities, List messages, boolean addMissing) { DatabaseUtil dbUtil = new DatabaseUtil(this.helperName); dbUtil.checkDb(modelEntities, messages, addMissing); } /** Creates a list of ModelEntity objects based on meta data from the database */ public List induceModelFromDb(Collection messages) { DatabaseUtil dbUtil = new DatabaseUtil(this.helperName); return dbUtil.induceModelFromDb(messages); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -