public void setLike(String fieldName, Object fieldValue) { if (fieldName != null && fieldValue != null) { boolean haveField = false; for (Field field : entityField) { if (fieldName.equals(field.getFieldName()) && field.getWhereRule().equals(Field.RULE_LIKE)) { if (fieldValue.getClass().isAssignableFrom(String.class)) { String value = fieldValue.toString(); if (!StringUtils.isNull(value)) { field.setFieldValue("%" + fieldValue + "%"); haveField = true; break; } } } } if (!haveField) { extendField.add( new Field(fieldName, fieldValue, 1000, Field.WHERE_BY_AND, Field.RULE_LIKE, false)); } } }
public void addOrderBy(String fieldName) { if (fieldName != null) { boolean haveField = false; for (Field field : orderField) { if (fieldName.equals(field.getFieldName())) { haveField = true; break; } } if (!haveField) orderField.add(new Field(fieldName, false, true)); } }
public void setWhereIn(String fieldName, Object fieldValue) { if (fieldName != null && fieldValue != null) { boolean haveField = false; for (Field field : entityField) { if (fieldName.equals(field.getFieldName()) && field.getWhereRule().equals(Field.RULE_IN)) { field.setFieldValue(fieldValue); haveField = true; break; } } if (!haveField) extendField.add( new Field(fieldName, fieldValue, 1000, Field.WHERE_BY_AND, Field.RULE_IN, false)); } }
private String orderBy() { StringBuffer buffer = new StringBuffer(); boolean isHead = true; // make group by field for (Field field : orderField) { if (field.isOrderBy()) { if (isHead) { isHead = false; buffer.append(ORDER_BY); } else { buffer.append(","); } buffer.append(field.getFieldName()); } } return buffer.toString(); }
private void setWhereFor( String fieldName, Object fieldValue, int fieldType, String whereBy, String whereRule, boolean isPrimaryKey) { boolean haveField = false; for (Field field : entityField) { if (fieldName.equals(field.getFieldName()) && field.getWhereRule().equals(Field.RULE_EQUAL) && fieldValue != null) { field.setFieldValue(fieldValue); field.setFieldType(fieldType); field.setWhereBy(whereBy); field.setWhereRule(whereRule); field.setPrimaryKey(isPrimaryKey); haveField = true; break; } } if (!haveField && fieldValue != null) { if (fieldValue.getClass().isAssignableFrom(String.class)) { String value = fieldValue.toString(); if (!StringUtils.isNull(value)) { entityField.add( new Field(fieldName, fieldValue, fieldType, whereBy, whereRule, isPrimaryKey)); } } else { entityField.add( new Field(fieldName, fieldValue, fieldType, whereBy, whereRule, isPrimaryKey)); } } }
public String searchSQL(Object entity) { entityFillField(entity); String sql = SEARCH; sql += catalog + "."; sql += table; boolean isHead = true; for (Field field : entityField) { if (isHead) { isHead = false; sql += " WHERE "; } else { sql += field.getWhereBy(); } sql += field.getFieldName() + "="; Object fieldValue = field.getFieldValue(); if (field.getFieldType() == Types.TIMESTAMP) { sql += "'" + CalendarUtils.dtformat(((Date) fieldValue)) + "'"; } else if (field.getFieldType() == Types.DATE) { sql += "'" + CalendarUtils.dformat(((Date) fieldValue)) + "'"; } else { sql += StringUtils.fieldValue(fieldValue); } } // fill extend field sql += extendSQL(isHead); // make group by field sql += groupBy(); // make order by field sql += orderBy(); if (offsetRecord > -1 && maxRecords > 0) { sql += OFFSET_PAGE + offsetRecord + "," + maxRecords; } return sql; }
public String deleteSQL(Object entity) { entityFillField(entity); String sql = DELETE; sql += catalog + "."; sql += table; boolean isHead = true; for (Field field : entityField) { if (isHead) { isHead = false; sql += " WHERE "; } else { sql += field.getWhereBy(); } sql += field.getFieldName() + field.getWhereRule(); Object fieldValue = field.getFieldValue(); if (field.getFieldType() == Types.TIMESTAMP) { sql += "'" + CalendarUtils.dtformat(((Date) fieldValue)) + "'"; } else if (field.getFieldType() == Types.DATE) { sql += "'" + CalendarUtils.dformat(((Date) fieldValue)) + "'"; } else { sql += StringUtils.fieldValue(fieldValue); } } // fill extend field sql += extendSQL(isHead); return sql; }
public String addSQL(Object entity) { entityFillField(entity); String sql = ADD; sql += " " + catalog + "."; sql += table; sql += " ("; boolean isHead = true; for (Field field : entityField) { if (isHead) isHead = false; else sql += ","; sql += field.getFieldName(); } sql += ") VALUES("; isHead = true; for (Field field : entityField) { if (isHead) isHead = false; else sql += ","; Object fieldValue = field.getFieldValue(); if (field.getFieldType() == Types.TIMESTAMP) { sql += "'" + CalendarUtils.dtformat(((Date) fieldValue)) + "'"; } else if (field.getFieldType() == Types.DATE) { sql += "'" + CalendarUtils.dformat(((Date) fieldValue)) + "'"; } else { sql += StringUtils.fieldValue(fieldValue); } } sql += ")"; return sql; }
public String updateSQL(Object entity) { entityFillField(entity); String sql = UPDATE; sql += " " + catalog + "."; sql += table; Object fieldValue; boolean isHead = true; for (Field field : entityField) { if (!field.isPrimaryKey()) { if (isHead) { isHead = false; sql += " SET "; } else { sql += ","; } fieldValue = field.getFieldValue(); sql += field.getFieldName() + "="; if (field.getFieldType() == Types.TIMESTAMP) { sql += "'" + CalendarUtils.dtformat(((Date) fieldValue)) + "'"; } else if (field.getFieldType() == Types.DATE) { sql += "'" + CalendarUtils.dformat(((Date) fieldValue)) + "'"; } else { sql += StringUtils.fieldValue(fieldValue); } } } isHead = true; for (Field field : entityField) { if (field.isPrimaryKey()) { if (isHead) { isHead = false; sql += " WHERE "; } else { sql += field.getWhereBy(); } fieldValue = field.getFieldValue(); sql += field.getFieldName() + "="; if (field.getFieldType() == Types.TIMESTAMP) { sql += "'" + CalendarUtils.dtformat(((Date) fieldValue)) + "'"; } else if (field.getFieldType() == Types.DATE) { sql += "'" + CalendarUtils.dformat(((Date) fieldValue)) + "'"; } else { sql += fieldValue.toString(); } } } return sql; }
private String extendSQL(boolean isHead) { StringBuffer buffer = new StringBuffer(); // fill extend field for (Field field : extendField) { Object fieldValue = field.getFieldValue(); // Object choose if (fieldValue != null) { if (fieldValue.getClass().isAssignableFrom(Date.class)) { Date value = (Date) fieldValue; if (isHead) { isHead = false; buffer.append(" WHERE "); } else { buffer.append(field.getWhereBy()); } buffer.append(field.getFieldName() + field.getWhereRule()); if (field.getWhereRule().equals(Field.RULE_LIKE)) { if (field.getFieldType() == Types.TIMESTAMP) { buffer.append("'%" + CalendarUtils.dtformat(value) + "%'"); } else if (field.getFieldType() == Types.DATE) { buffer.append("'%" + CalendarUtils.dformat(value) + "%'"); } } else { if (field.getFieldType() == Types.TIMESTAMP) { buffer.append("'" + CalendarUtils.dtformat(value) + "'"); } else if (field.getFieldType() == Types.DATE) { buffer.append("'" + CalendarUtils.dformat(value) + "'"); } } } else if (fieldValue.getClass().isAssignableFrom(Integer.class) || fieldValue.getClass().isAssignableFrom(Long.class) || fieldValue.getClass().isAssignableFrom(Float.class) || fieldValue.getClass().isAssignableFrom(Double.class)) { if (isHead) { isHead = false; buffer.append(" WHERE "); } else { buffer.append(field.getWhereBy()); } buffer.append(field.getFieldName() + field.getWhereRule() + fieldValue); } else if (fieldValue.getClass().isAssignableFrom(String.class)) { String value = (String) fieldValue; if (!value.equals("")) { if (isHead) { isHead = false; buffer.append(" WHERE "); } else { buffer.append(field.getWhereBy()); } buffer.append(field.getFieldName() + field.getWhereRule()); if (field.getWhereRule().equals(Field.RULE_LIKE)) { buffer.append("'%" + value + "%'"); } else if (field.getWhereRule().equals(Field.RULE_IN)) { buffer.append(" (" + value + ") "); } else { buffer.append(" '" + value + "' "); } } } } } return buffer.toString(); }