@Test public void testUpdateStatement() { Map<String, Object> x = new CaseInsensitiveMap(); x.put("testField", "testValue"); x.put("id", "1"); a(dialect.update( Person.getMetaModel(), x)) .shouldBeEqual("UPDATE people SET testField = 'testValue' WHERE id = 1"); }
@Override public String update(MetaModel metaModel, Map<String, Object> attributes, String ... replacements) { if (attributes.isEmpty()) { throw new NoSuchElementException("No attributes set, can't create an update statement."); } StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(" SET "); String idName = metaModel.getIdName(); Map<String, Object> attributesWithoutId = new CaseInsensitiveMap<>(attributes); attributesWithoutId.remove(idName); Iterator<Entry<String, Object>> it = attributesWithoutId.entrySet().iterator(); for (;;) { Entry<String, Object> attribute = it.next(); query.append(attribute.getKey()).append(" = "); appendValue(query, attribute.getValue(), replacements); if (it.hasNext()) { query.append(", "); } else { break; } } if (metaModel.getCompositeKeys() == null){ query.append(" WHERE ").append(idName).append(" = ").append(attributes.get(idName)); } else { String[] compositeKeys = metaModel.getCompositeKeys(); for (int i = 0; i < compositeKeys.length; i++) { query.append(i == 0 ? " WHERE " : " AND ").append(compositeKeys[i]).append(" = "); appendValue(query, attributes.get(compositeKeys[i])); } } return query.toString(); }
DefaultDialect implements Dialect { @Override public String update(MetaModel metaModel, Map<String, Object> attributes, String ... replacements) { if (attributes.isEmpty()) { throw new NoSuchElementException("No attributes set, can't create an update statement."); } StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(" SET "); String idName = metaModel.getIdName(); Map<String, Object> attributesWithoutId = new CaseInsensitiveMap<>(attributes); attributesWithoutId.remove(idName); Iterator<Entry<String, Object>> it = attributesWithoutId.entrySet().iterator(); for (;;) { Entry<String, Object> attribute = it.next(); query.append(attribute.getKey()).append(" = "); appendValue(query, attribute.getValue(), replacements); if (it.hasNext()) { query.append(", "); } else { break; } } if (metaModel.getCompositeKeys() == null){ query.append(" WHERE ").append(idName).append(" = ").append(attributes.get(idName)); } else { String[] compositeKeys = metaModel.getCompositeKeys(); for (int i = 0; i < compositeKeys.length; i++) { query.append(i == 0 ? " WHERE " : " AND ").append(compositeKeys[i]).append(" = "); appendValue(query, attributes.get(compositeKeys[i])); } } return query.toString(); } }
DefaultDialect implements Dialect { @Override public String update(MetaModel metaModel, Map<String, Object> attributes, String ... replacements) { if (attributes.isEmpty()) { throw new NoSuchElementException("No attributes set, can't create an update statement."); } StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(" SET "); String idName = metaModel.getIdName(); Map<String, Object> attributesWithoutId = new CaseInsensitiveMap<>(attributes); attributesWithoutId.remove(idName); Iterator<Entry<String, Object>> it = attributesWithoutId.entrySet().iterator(); for (;;) { Entry<String, Object> attribute = it.next(); query.append(attribute.getKey()).append(" = "); appendValue(query, attribute.getValue(), replacements); if (it.hasNext()) { query.append(", "); } else { break; } } if (metaModel.getCompositeKeys() == null){ query.append(" WHERE ").append(idName).append(" = ").append(attributes.get(idName)); } else { String[] compositeKeys = metaModel.getCompositeKeys(); for (int i = 0; i < compositeKeys.length; i++) { query.append(i == 0 ? " WHERE " : " AND ").append(compositeKeys[i]).append(" = "); appendValue(query, attributes.get(compositeKeys[i])); } } return query.toString(); }  }
DefaultDialect implements Dialect { @Override public String update(MetaModel metaModel, Map<String, Object> attributes, String ... replacements) { if (attributes.isEmpty()) { throw new NoSuchElementException("No attributes set, can't create an update statement."); } StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(" SET "); String idName = metaModel.getIdName(); Map<String, Object> attributesWithoutId = new CaseInsensitiveMap<>(attributes); attributesWithoutId.remove(idName); Iterator<Entry<String, Object>> it = attributesWithoutId.entrySet().iterator(); for (;;) { Entry<String, Object> attribute = it.next(); query.append(attribute.getKey()).append(" = "); appendValue(query, attribute.getValue(), replacements); if (it.hasNext()) { query.append(", "); } else { break; } } if (metaModel.getCompositeKeys() == null){ query.append(" WHERE ").append(idName).append(" = ").append(attributes.get(idName)); } else { String[] compositeKeys = metaModel.getCompositeKeys(); for (int i = 0; i < compositeKeys.length; i++) { query.append(i == 0 ? " WHERE " : " AND ").append(compositeKeys[i]).append(" = "); appendValue(query, attributes.get(compositeKeys[i])); } } return query.toString(); }  @Override String selectStar(String table); @Override String selectStar(String table, String where); @Override String selectStarParametrized(String table, String ... parameters); @Override String formSelect(String tableName, String[] columns, String subQuery, List<String> orderBys, long limit, long offset); @Override Object overrideDriverTypeConversion(MetaModel mm, String attributeName, Object value); @Override String selectCount(String from); @Override String selectCount(String table, String where); @Override String selectExists(MetaModel metaModel); @Override String selectManyToManyAssociation(Many2ManyAssociation association, String sourceFkColumnName, int questionsCount); @Override String insertManyToManyAssociation(Many2ManyAssociation association); @Override String insertParametrized(MetaModel metaModel, List<String> columns, boolean containsId); @Override String deleteManyToManyAssociation(Many2ManyAssociation association); @Override String insert(MetaModel metaModel, Map<String, Object> attributes, String ... replacements); @Override String update(MetaModel metaModel, Map<String, Object> attributes, String ... replacements); }
DefaultDialect implements Dialect { @Override public String update(MetaModel metaModel, Map<String, Object> attributes, String ... replacements) { if (attributes.isEmpty()) { throw new NoSuchElementException("No attributes set, can't create an update statement."); } StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(" SET "); String idName = metaModel.getIdName(); Map<String, Object> attributesWithoutId = new CaseInsensitiveMap<>(attributes); attributesWithoutId.remove(idName); Iterator<Entry<String, Object>> it = attributesWithoutId.entrySet().iterator(); for (;;) { Entry<String, Object> attribute = it.next(); query.append(attribute.getKey()).append(" = "); appendValue(query, attribute.getValue(), replacements); if (it.hasNext()) { query.append(", "); } else { break; } } if (metaModel.getCompositeKeys() == null){ query.append(" WHERE ").append(idName).append(" = ").append(attributes.get(idName)); } else { String[] compositeKeys = metaModel.getCompositeKeys(); for (int i = 0; i < compositeKeys.length; i++) { query.append(i == 0 ? " WHERE " : " AND ").append(compositeKeys[i]).append(" = "); appendValue(query, attributes.get(compositeKeys[i])); } } return query.toString(); }  @Override String selectStar(String table); @Override String selectStar(String table, String where); @Override String selectStarParametrized(String table, String ... parameters); @Override String formSelect(String tableName, String[] columns, String subQuery, List<String> orderBys, long limit, long offset); @Override Object overrideDriverTypeConversion(MetaModel mm, String attributeName, Object value); @Override String selectCount(String from); @Override String selectCount(String table, String where); @Override String selectExists(MetaModel metaModel); @Override String selectManyToManyAssociation(Many2ManyAssociation association, String sourceFkColumnName, int questionsCount); @Override String insertManyToManyAssociation(Many2ManyAssociation association); @Override String insertParametrized(MetaModel metaModel, List<String> columns, boolean containsId); @Override String deleteManyToManyAssociation(Many2ManyAssociation association); @Override String insert(MetaModel metaModel, Map<String, Object> attributes, String ... replacements); @Override String update(MetaModel metaModel, Map<String, Object> attributes, String ... replacements);  }