📄 genericdao.java
字号:
entity.synchronizedWithDatasource(); } else { // Debug.logWarning("[GenericDAO.select]: select failed, result set was empty for entity: " + entity.toString(), module); throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString()); } } finally { sqlP.close(); } } public void partialSelect(GenericEntity entity, Set keys) 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 partialSelect not supported yet for view entities"); } /* if(entity == null || entity.<%=modelEntity.pkNameString(" == null || entity."," == null")%>) { Debug.logWarning("[GenericDAO.select]: Cannot select GenericEntity: required primary key field(s) missing.", module); return false; } */ // we don't want to select ALL fields, just the nonpk fields that are in the passed GenericEntity List partialFields = FastList.newInstance(); Set tempKeys = new TreeSet(keys); Iterator nopkIter = modelEntity.getNopksIterator(); while (nopkIter.hasNext()) { ModelField curField = (ModelField) nopkIter.next(); if (tempKeys.contains(curField.getName())) { partialFields.add(curField); tempKeys.remove(curField.getName()); } } if (tempKeys.size() > 0) { throw new GenericModelException("In partialSelect invalid field names specified: " + tempKeys.toString()); } StringBuffer sqlBuffer = new StringBuffer("SELECT "); if (partialFields.size() > 0) { sqlBuffer.append(modelEntity.colNameString(partialFields, ", ", "", datasourceInfo.aliasViews)); } else { sqlBuffer.append("*"); } sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, datasourceInfo)); sqlBuffer.append(SqlJdbcUtil.makeWhereClause(modelEntity, modelEntity.getPksCopy(), entity, "AND", datasourceInfo.joinStyle)); SQLProcessor sqlP = new SQLProcessor(helperName); try { sqlP.prepareStatement(sqlBuffer.toString(), true, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader); sqlP.executeQuery(); if (sqlP.next()) { for (int j = 0; j < partialFields.size(); j++) { ModelField curField = (ModelField) partialFields.get(j); SqlJdbcUtil.getValue(sqlP.getResultSet(), j + 1, curField, entity, modelFieldTypeReader); } entity.synchronizedWithDatasource(); } else { // Debug.logWarning("[GenericDAO.select]: select failed, result set was empty.", module); throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString()); } } finally { sqlP.close(); } } /* ====================================================================== */ /* ====================================================================== */ /** Finds GenericValues by the conditions specified in the EntityCondition object, the the EntityCondition javadoc for more details. *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file *@param whereEntityCondition The EntityCondition object that specifies how to constrain this query before any groupings are done (if this is a view entity with group-by aliases) *@param havingEntityCondition The EntityCondition object that specifies how to constrain this query after any groupings are done (if this is a view entity with group-by aliases) *@param fieldsToSelect The fields of the named entity to get from the database; if empty or null all fields will be retreived *@param orderBy The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending *@param findOptions An instance of EntityFindOptions that specifies advanced query options. See the EntityFindOptions JavaDoc for more details. *@return EntityListIterator representing the result of the query: NOTE THAT THIS MUST BE CLOSED WHEN YOU ARE * DONE WITH IT, AND DON'T LEAVE IT OPEN TOO LONG BEACUSE IT WILL MAINTAIN A DATABASE CONNECTION. */ public EntityListIterator selectListIteratorByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Collection fieldsToSelect, List orderBy, EntityFindOptions findOptions) throws GenericEntityException { if (modelEntity == null) { return null; } // if no find options passed, use default if (findOptions == null) 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); } // make two ArrayLists of fields, one for fields to select and the other for where clause fields (to find by) List selectFields = FastList.newInstance(); if (fieldsToSelect != null && fieldsToSelect.size() > 0) { Set tempKeys = FastSet.newInstance(); tempKeys.addAll(fieldsToSelect); Iterator fieldIter = modelEntity.getFieldsIterator(); while (fieldIter.hasNext()) { ModelField curField = (ModelField) fieldIter.next(); if (tempKeys.contains(curField.getName())) { selectFields.add(curField); tempKeys.remove(curField.getName()); } } if (tempKeys.size() > 0) { throw new GenericModelException("In selectListIteratorByCondition invalid field names specified: " + tempKeys.toString()); } } else { selectFields = modelEntity.getFieldsCopy(); } StringBuffer sqlBuffer = new StringBuffer("SELECT "); if (findOptions.getDistinct()) { sqlBuffer.append("DISTINCT "); } if (selectFields.size() > 0) { sqlBuffer.append(modelEntity.colNameString(selectFields, ", ", "", datasourceInfo.aliasViews)); } else { sqlBuffer.append("*"); } // 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); } // ORDER BY clause sqlBuffer.append(SqlJdbcUtil.makeOrderByClause(modelEntity, orderBy, datasourceInfo)); String sql = sqlBuffer.toString(); SQLProcessor sqlP = new SQLProcessor(helperName); sqlP.prepareStatement(sql, findOptions.getSpecifyTypeAndConcur(), findOptions.getResultSetType(), findOptions.getResultSetConcurrency(), findOptions.getFetchSize(), findOptions.getMaxRows()); 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); } long queryStartTime = 0; if (Debug.timingOn()) { queryStartTime = System.currentTimeMillis(); } sqlP.executeQuery(); if (Debug.timingOn()) { long queryEndTime = System.currentTimeMillis(); long queryTotalTime = queryEndTime - queryStartTime; if (queryTotalTime > 150) { Debug.logTiming("Ran query in " + queryTotalTime + " milli-seconds: " + sql, module); } } return new EntityListIterator(sqlP, modelEntity, selectFields, modelFieldTypeReader); } public List selectByMultiRelation(GenericValue value, ModelRelation modelRelationOne, ModelEntity modelEntityOne, ModelRelation modelRelationTwo, ModelEntity modelEntityTwo, List orderBy) throws GenericEntityException { SQLProcessor sqlP = new SQLProcessor(helperName); // get the tables names String atable = modelEntityOne.getTableName(datasourceInfo); String ttable = modelEntityTwo.getTableName(datasourceInfo); // get the column name string to select StringBuffer selsb = new StringBuffer(); List collist = FastList.newInstance(); List fldlist = FastList.newInstance(); for (Iterator iterator = modelEntityTwo.getFieldsIterator(); iterator.hasNext();) { ModelField mf = (ModelField) iterator.next(); collist.add(mf.getColName()); fldlist.add(mf.getName()); selsb.append(ttable + "." + mf.getColName()); if (iterator.hasNext()) { selsb.append(", "); } else { selsb.append(" "); } } // construct assoc->target relation string int kmsize = modelRelationTwo.getKeyMapsSize(); StringBuffer wheresb = new StringBuffer(); for (int i = 0; i < kmsize; i++) { ModelKeyMap mkm = modelRelationTwo.getKeyMap(i); String lfname = mkm.getFieldName(); String rfname = mkm.getRelFieldName(); if (wheresb.length() > 0) { wheresb.append(" AND "); } wheresb.append(atable + "." + modelEntityOne.getField(lfname).getColName() + " = " + ttable + "." + modelEntityTwo.getField(rfname).getColName());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -