📄 genericdao.java
字号:
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
ArrayList retlist = new ArrayList();
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
for (int j = 0; j < collsize; j++) {
String fldname = (String) fldlist.get(j);
ModelField mf = modelEntityTwo.getField(fldname);
SqlJdbcUtil.getValue(sqlP.getResultSet(), j + 1, mf, gv, modelFieldTypeReader);
}
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 = new LinkedList();
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 = new LinkedList();
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 deleteByAnd(ModelEntity modelEntity, Map fields) throws GenericEntityException {
SQLProcessor sqlP = new SQLProcessor(helperName);
try {
return deleteByAnd(modelEntity, fields, sqlP);
} catch (GenericDataSourceException e) {
sqlP.rollback();
throw new GenericDataSourceException("Generic Entity Exception occurred in deleteByAnd", e);
} finally {
sqlP.close();
}
}
public int deleteByAnd(ModelEntity modelEntity, Map fields, SQLProcessor sqlP) throws GenericEntityException {
if (modelEntity == null || fields == null) return 0;
if (modelEntity instanceof ModelViewEntity) {
throw new org.ofbiz.entity.GenericNotImplementedException("Operation deleteByAnd not supported yet for view entities");
}
List whereFields = new ArrayList();
if (fields != null && fields.size() > 0) {
for (int fi = 0; fi < modelEntity.getFieldsSize(); fi++) {
ModelField curField = modelEntity.getField(fi);
if (fields.containsKey(curField.getName())) {
whereFields.add(curField);
}
}
}
GenericValue dummyValue = new GenericValue(modelEntity, fields);
String sql = "DELETE FROM " + modelEntity.getTableName(datasourceInfo);
if (fields != null && fields.size() > 0) {
sql += " WHERE " + SqlJdbcUtil.makeWhereStringFromFields(whereFields, dummyValue, "AND");
}
try {
sqlP.prepareStatement(sql);
if (fields != null && fields.size() > 0) {
SqlJdbcUtil.setValuesWhereClause(sqlP, whereFields, dummyValue, modelFieldTypeReader);
}
return sqlP.executeUpdate();
} finally {
sqlP.close();
}
}
/** Called dummyPKs because they can be invalid PKs, doing a deleteByAnd instead of a normal delete */
public int deleteAll(List dummyPKs) throws GenericEntityException {
if (dummyPKs == null || dummyPKs.size() == 0) {
return 0;
}
SQLProcessor sqlP = new SQLProcessor(helperName);
try {
Iterator iter = dummyPKs.iterator();
int numDeleted = 0;
while (iter.hasNext()) {
GenericEntity entity = (GenericEntity) iter.next();
// if it contains a complete primary key, delete the one, otherwise deleteByAnd
if (entity.containsPrimaryKey()) {
numDeleted += delete(entity, sqlP);
} else {
numDeleted += deleteByAnd(entity.getModelEntity(), entity.getAllFields(), sqlP);
}
}
return numDeleted;
} catch (GenericDataSourceException e) {
sqlP.rollback();
throw new GenericDataSourceException("Generic Entity Exception occurred in deleteAll", e);
} 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 + -