Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Polevoy committed Mar 26, 2015
2 parents fb0f902 + 4a3a221 commit b8fe3be
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 73 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package org.javalite.activejdbc;

import java.util.Map;
import java.util.TreeMap;

/**
Expand All @@ -29,4 +30,9 @@ public class CaseInsensitiveMap<V> extends TreeMap<String, V> {
public CaseInsensitiveMap() {
super(String.CASE_INSENSITIVE_ORDER);
}

public CaseInsensitiveMap(Map<? extends String, V> m) {
this();
putAll(m);
}
}
27 changes: 13 additions & 14 deletions activejdbc/src/main/java/org/javalite/activejdbc/Model.java
Expand Up @@ -2682,25 +2682,24 @@ public void manageTime(boolean manage) {
* value literals.
* Example:
* <pre>
* String insert = u.toInsert();
* String sql = user.toInsert();
* //yields this output:
* //INSERT INTO users (id, first_name, email, last_name) VALUES (1, 'Marilyn', 'mmonroe@yahoo.com', 'Monroe');
* //INSERT INTO users (id, email, first_name, last_name) VALUES (1, 'mmonroe@yahoo.com', 'Marilyn', 'Monroe')
* </pre>
*
* @return INSERT SQL based on this model.
*/
public String toInsert() {
return toInsert(getMetaModelLocal().getDialect());
}


/**
* Generates INSERT SQL based on this model with the provided dialect.
* Example:
* <pre>
* String insert = u.toInsert(new MySQLDialect());
* String sql = user.toInsert(new MySQLDialect());
* //yields this output:
* //INSERT INTO users (id, first_name, email, last_name) VALUES (1, 'Marilyn', 'mmonroe@yahoo.com', 'Monroe');
* //INSERT INTO users (id, email, first_name, last_name) VALUES (1, 'mmonroe@yahoo.com', 'Marilyn', 'Monroe')
* </pre>
*
* @param dialect dialect to be used to generate the SQL
Expand All @@ -2711,31 +2710,31 @@ public String toInsert(Dialect dialect) {
}

/**
* Generates UPDATE SQL based on this model. Uses single quotes for all string values.
* Generates UPDATE SQL based on this model. Uses the dialect associated with this model database to format the
* value literals.
* Example:
* <pre>
*
* String update = u.toUpdate();
* String sql = user.toUpdate();
* //yields this output:
* //UPDATE students SET DOB = '1965-12-01' , FIRST_NAME = 'Jim' , LAST_NAME = 'Cary' WHERE id = 1
* //UPDATE users SET email = 'mmonroe@yahoo.com', first_name = 'Marilyn', last_name = 'Monroe' WHERE id = 1
* </pre>
*
* @return UPDATE SQL based on this model.
*/
public String toUpdate() {
return toUpdate(getMetaModelLocal().getDialect());
}

/**
* Generates UPDATE SQL based on this model. Uses single quotes for all string values.
* Generates UPDATE SQL based on this model with the provided dialect.
* Example:
* <pre>
*
* String update = u.toUpdate();
* String sql = user.toUpdate(new MySQLDialect());
* //yields this output:
* //UPDATE students SET DOB = '1965-12-01' , FIRST_NAME = 'Jim' , LAST_NAME = 'Cary' WHERE id = 1
* //UPDATE users SET email = 'mmonroe@yahoo.com', first_name = 'Marilyn', last_name = 'Monroe' WHERE id = 1
* </pre>
*
* @param dialect dialect to be used to generate the SQL
* @return UPDATE SQL based on this model.
*/
public String toUpdate(Dialect dialect) {
Expand Down
Expand Up @@ -224,41 +224,31 @@ public String insert(MetaModel metaModel, Map<String, Object> attributes) {
}
return query.toString();
}

@Override
public String update(MetaModel metaModel, Map<String, Object> attributes) {
StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(' ');
if (attributes.isEmpty()) {
throw new NoSuchElementException("No attributes set, can't create an update statement.");
} else {

query.append("SET ");
String idName = metaModel.getIdName();

// don't include the id name in the set portion
Map<String, Object> attributesWithoutId = new CaseInsensitiveMap<Object>();
attributesWithoutId.putAll(attributes);
attributesWithoutId.remove(idName);

Iterator<Entry<String, Object>> attributesIt = attributesWithoutId.entrySet().iterator();
for (;;) {
Entry<String, Object> attribute = attributesIt.next();
String key = attribute.getKey();
Object val = attribute.getValue();

query.append(key + " = ");
appendValue(query, val); // Accommodates the different types

if (attributesIt.hasNext()) {
query.append(", ");
} else {
break;
}
}
query.append(" WHERE ").append(idName).append(" = " + attributes.get(idName));
}
StringBuilder query = new StringBuilder().append("UPDATE ").append(metaModel.getTableName()).append(" SET ");
String idName = metaModel.getIdName();

// don't include the id name in the SET portion
Map<String, Object> attributesWithoutId = new CaseInsensitiveMap<Object>(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()); // Accommodates the different types
if (it.hasNext()) {
query.append(", ");
} else {
break;
}
}
query.append(" WHERE ").append(idName).append(" = ").append(attributes.get(idName));
return query.toString();

}

}
48 changes: 19 additions & 29 deletions activejdbc/src/test/java/org/javalite/activejdbc/ModelTest.java
Expand Up @@ -483,44 +483,35 @@ public void shouldGenerateCorrectInsertSQL() {
insertSQL = s.toInsert(new SimpleFormatter(java.sql.Date.class, "to_date('", "')"));
the(insertSQL).shouldBeEqual("INSERT INTO students (dob, first_name, id, last_name) VALUES (to_date('1965-12-01'), 'Jim', 1, 'Cary')");
}

@Test
public void shouldGenerateCorrectUpdateSQL() {

// Create the student
public void shouldGenerateValidUpdateSQL() {
deleteAndPopulateTable("students");
Student s = new Student();
s.set("first_name", "Jim");
s.set("last_name", "Cary");
s.set("dob", new java.sql.Date(getDate(1965, 12, 1).getTime()));
s.set("id", 1);
s.saveIt();

// find them, and change a column
s = Student.findById(1);
s.set("first_name", "Drew");
s.saveIt();
String updateSQL = s.toUpdate();
Base.exec(updateSQL);

// Find them again
s = Student.findById(1);
Student s = Student.findById(1);
s.set("first_name", "James", "last_name", "Meredith");
java.sql.Date dob = getDate(1933, 6, 25);
java.sql.Timestamp enrollmentDate = getTimestamp(1962, 10, 1, 12, 0, 0, 0);
s.setDate("dob", dob);
s.setTimestamp("enrollment_date", enrollmentDate);
// don't save it!

String updateSql = s.toUpdate();
the(Base.exec(updateSql)).shouldBeEqual(1);

// Verify that the first name column changed
the(s.get("first_name")).shouldBeEqual("Drew");
System.out.println(updateSQL);

s = Student.findById(1);
the(s.get("first_name")).shouldBeEqual("James");
the(s.get("last_name")).shouldBeEqual("Meredith");
the(s.get("dob")).shouldBeEqual(dob);
the(s.get("enrollment_date")).shouldBeEqual(enrollmentDate);
}
@Test(expected=NoSuchElementException.class)

@Test(expected = NoSuchElementException.class)
public void shouldGenerateNoSuchElementFromBlankUpdate() {
// Verify that a model with no attributes throws an error
Student s = new Student();
s.saveIt();
s.toUpdate();
}


@Test
public void shouldGenerateValidInsertSQL() {
Student s = new Student();
Expand All @@ -531,7 +522,6 @@ public void shouldGenerateValidInsertSQL() {
s.setTimestamp("enrollment_date", enrollmentDate);

String insertSql = s.toInsert();
System.out.println(insertSql);
Object id = Base.execInsert(insertSql, s.getIdName());

s = Student.findById(id);
Expand Down

0 comments on commit b8fe3be

Please sign in to comment.