public void entityFillField(Object entity) { try { prefix = DbConfig.getProperty("Db.humpInterval"); if (prefix != null && prefix.equals("case")) { prefix = null; } // enableCase = DbConfig.getProperty("Db.case").equals("true"); catalog = DbConfig.getProperty(entity.getClass().getPackage().getName()); table = StringUtils.upperToPrefix(entity.getClass().getSimpleName(), prefix); List<String> methods = ReflectUtil.getMethodNames(entity.getClass()); String method; String field; int columnType = 0; String columnName = null; boolean isPrimaryKey = false; for (Iterator<String> it = methods.iterator(); it.hasNext(); ) { method = it.next(); if (method.indexOf("get") == 0) { Method getMethod; getMethod = entity.getClass().getMethod(method); Object columnValue = getMethod.invoke(entity); if (columnValue != null) { field = method.substring(method.indexOf("get") + 3); columnName = StringUtils.upperToPrefixNot(field, prefix); columnType = DbUtil.type(catalog, table, columnName); if (columnType != -100) { // System.out.println(columnName+" "+catalog+" "+table+" "+ columnValue+" // "+columnType+" "+isPrimaryKey); isPrimaryKey = DbUtil.isPrimaryKey(catalog, table, columnName); setField(columnName, columnValue, columnType, isPrimaryKey); } } } } } catch (Exception e) { e.printStackTrace(); } }
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 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 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; }
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 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 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; }