@Test public void buildUpdateStatement() { SystemCodes _new = new SystemCodes("category2","key1","value2"); _new.setId(1l); List<String> updatedFields = new ArrayList<String>(); updatedFields.add("category"); updatedFields.add("value"); Assert.assertEquals( "update system_codes set CATEGORY_='CATEGORY2',VALUE_='value2' where id=1", SyncUtil.buildUpdateStatement(_new, updatedFields)); }
public static String[] buildUpdateStatement(BaseEntity obj, List<String> fields) { String[] updateQuery = new String[2]; String tableName = SyncUtil.getTableName(obj); Map<String, String> columns = CacheUtil.getColumnNames(obj); StringBuilder params = new StringBuilder("["); StringBuilder sql = new StringBuilder("update "); sql.append(tableName).append(" set "); int count = 0; for (String field : fields) { String[] pField = StringUtil.splitSafe(field, "\\."); String column = columns.get(pField[0]); Object ret = CrudUtil.retrieveNullableObjectValue(obj, field); if (column != null) { String[] cols = StringUtil.splitSafe(column, ","); if (cols != null && cols.length > 0) { for (int i = 0; i < cols.length; i++) { String n = normalizeValue(ret, cols[i]); if (n.trim().length() > 0) { if (count++ > 0) { sql.append(","); params.append(","); } sql.append(cols[i]).append("=?"); params.append(n); } } } } } sql.append(" where id=?"); if (count > 0) params.append(","); params.append(obj.getId()); params.append("]"); updateQuery[0] = sql.toString(); updateQuery[1] = params.toString(); return updateQuery; }
SyncUtil { public static String[] buildUpdateStatement(BaseEntity obj, List<String> fields) { String[] updateQuery = new String[2]; String tableName = SyncUtil.getTableName(obj); Map<String, String> columns = CacheUtil.getColumnNames(obj); StringBuilder params = new StringBuilder("["); StringBuilder sql = new StringBuilder("update "); sql.append(tableName).append(" set "); int count = 0; for (String field : fields) { String[] pField = StringUtil.splitSafe(field, "\\."); String column = columns.get(pField[0]); Object ret = CrudUtil.retrieveNullableObjectValue(obj, field); if (column != null) { String[] cols = StringUtil.splitSafe(column, ","); if (cols != null && cols.length > 0) { for (int i = 0; i < cols.length; i++) { String n = normalizeValue(ret, cols[i]); if (n.trim().length() > 0) { if (count++ > 0) { sql.append(","); params.append(","); } sql.append(cols[i]).append("=?"); params.append(n); } } } } } sql.append(" where id=?"); if (count > 0) params.append(","); params.append(obj.getId()); params.append("]"); updateQuery[0] = sql.toString(); updateQuery[1] = params.toString(); return updateQuery; } }
SyncUtil { public static String[] buildUpdateStatement(BaseEntity obj, List<String> fields) { String[] updateQuery = new String[2]; String tableName = SyncUtil.getTableName(obj); Map<String, String> columns = CacheUtil.getColumnNames(obj); StringBuilder params = new StringBuilder("["); StringBuilder sql = new StringBuilder("update "); sql.append(tableName).append(" set "); int count = 0; for (String field : fields) { String[] pField = StringUtil.splitSafe(field, "\\."); String column = columns.get(pField[0]); Object ret = CrudUtil.retrieveNullableObjectValue(obj, field); if (column != null) { String[] cols = StringUtil.splitSafe(column, ","); if (cols != null && cols.length > 0) { for (int i = 0; i < cols.length; i++) { String n = normalizeValue(ret, cols[i]); if (n.trim().length() > 0) { if (count++ > 0) { sql.append(","); params.append(","); } sql.append(cols[i]).append("=?"); params.append(n); } } } } } sql.append(" where id=?"); if (count > 0) params.append(","); params.append(obj.getId()); params.append("]"); updateQuery[0] = sql.toString(); updateQuery[1] = params.toString(); return updateQuery; }  }
SyncUtil { public static String[] buildUpdateStatement(BaseEntity obj, List<String> fields) { String[] updateQuery = new String[2]; String tableName = SyncUtil.getTableName(obj); Map<String, String> columns = CacheUtil.getColumnNames(obj); StringBuilder params = new StringBuilder("["); StringBuilder sql = new StringBuilder("update "); sql.append(tableName).append(" set "); int count = 0; for (String field : fields) { String[] pField = StringUtil.splitSafe(field, "\\."); String column = columns.get(pField[0]); Object ret = CrudUtil.retrieveNullableObjectValue(obj, field); if (column != null) { String[] cols = StringUtil.splitSafe(column, ","); if (cols != null && cols.length > 0) { for (int i = 0; i < cols.length; i++) { String n = normalizeValue(ret, cols[i]); if (n.trim().length() > 0) { if (count++ > 0) { sql.append(","); params.append(","); } sql.append(cols[i]).append("=?"); params.append(n); } } } } } sql.append(" where id=?"); if (count > 0) params.append(","); params.append(obj.getId()); params.append("]"); updateQuery[0] = sql.toString(); updateQuery[1] = params.toString(); return updateQuery; }  static String[] buildInsertStatement(BaseEntity obj); static String[] buildInsertStatement(BaseEntity obj, Class<?> clazz); static String[] buildUpdateStatement(BaseEntity obj, List<String> fields); static String buildDeleteStatement(BaseEntity obj); static String getTableName(BaseEntity obj); void setSyncNormalizerList(List<Normalizer> normalizer); }
SyncUtil { public static String[] buildUpdateStatement(BaseEntity obj, List<String> fields) { String[] updateQuery = new String[2]; String tableName = SyncUtil.getTableName(obj); Map<String, String> columns = CacheUtil.getColumnNames(obj); StringBuilder params = new StringBuilder("["); StringBuilder sql = new StringBuilder("update "); sql.append(tableName).append(" set "); int count = 0; for (String field : fields) { String[] pField = StringUtil.splitSafe(field, "\\."); String column = columns.get(pField[0]); Object ret = CrudUtil.retrieveNullableObjectValue(obj, field); if (column != null) { String[] cols = StringUtil.splitSafe(column, ","); if (cols != null && cols.length > 0) { for (int i = 0; i < cols.length; i++) { String n = normalizeValue(ret, cols[i]); if (n.trim().length() > 0) { if (count++ > 0) { sql.append(","); params.append(","); } sql.append(cols[i]).append("=?"); params.append(n); } } } } } sql.append(" where id=?"); if (count > 0) params.append(","); params.append(obj.getId()); params.append("]"); updateQuery[0] = sql.toString(); updateQuery[1] = params.toString(); return updateQuery; }  static String[] buildInsertStatement(BaseEntity obj); static String[] buildInsertStatement(BaseEntity obj, Class<?> clazz); static String[] buildUpdateStatement(BaseEntity obj, List<String> fields); static String buildDeleteStatement(BaseEntity obj); static String getTableName(BaseEntity obj); void setSyncNormalizerList(List<Normalizer> normalizer);  }