mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-21 21:14:12 +08:00
improved ColUpdateClause
improved ColDeleteClause
This commit is contained in:
parent
dd0861715f
commit
64179ea280
@ -5,6 +5,11 @@
|
||||
*/
|
||||
package com.mysema.query.collections;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections15.BeanMap;
|
||||
|
||||
import com.mysema.query.dml.UpdateClause;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
@ -20,6 +25,8 @@ public class ColUpdateClause<T> implements UpdateClause<ColUpdateClause<T>>{
|
||||
|
||||
private final Path<T> expr;
|
||||
|
||||
private final Map<Path<?>,Object> paths = new HashMap<Path<?>,Object>();
|
||||
|
||||
public ColUpdateClause(Path<T> expr, Iterable<? extends T> col){
|
||||
this(EvaluatorFactory.DEFAULT, expr, col);
|
||||
}
|
||||
@ -31,18 +38,22 @@ public class ColUpdateClause<T> implements UpdateClause<ColUpdateClause<T>>{
|
||||
|
||||
@Override
|
||||
public long execute() {
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
// int rv = 0;
|
||||
// for (T match : query.list(expr.asExpr())){
|
||||
// // TODO : update
|
||||
// rv++;
|
||||
// }
|
||||
// return rv;
|
||||
int rv = 0;
|
||||
for (T match : query.list(expr.asExpr())){
|
||||
BeanMap beanMap = new BeanMap(match);
|
||||
for (Map.Entry<Path<?>,Object> entry : paths.entrySet()){
|
||||
// TODO : support deep updates as well
|
||||
String propertyName = entry.getKey().getMetadata().getExpression().toString();
|
||||
beanMap.put(propertyName, entry.getValue());
|
||||
}
|
||||
rv++;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <U> ColUpdateClause<T> set(Path<U> path, U value) {
|
||||
// TODO Auto-generated method stub
|
||||
paths.put(path, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
package com.mysema.query.collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.animal.Cat;
|
||||
import com.mysema.query.animal.QCat;
|
||||
|
||||
public class ColDeleteClauseTest {
|
||||
|
||||
@Test
|
||||
public void testExecute() {
|
||||
QCat cat = QCat.cat;
|
||||
List<Cat> cats = new ArrayList<Cat>(Arrays.asList(new Cat("Ann"), new Cat("Bob"), new Cat("John"), new Cat("Carl")));
|
||||
|
||||
ColDeleteClause<Cat> deleteClause = new ColDeleteClause<Cat>(cat, cats);
|
||||
deleteClause.where(cat.name.eq("Bob"));
|
||||
assertEquals(1, deleteClause.execute());
|
||||
|
||||
assertEquals(3, cats.size());
|
||||
assertEquals("Ann", cats.get(0).getName());
|
||||
assertEquals("John", cats.get(1).getName());
|
||||
assertEquals("Carl", cats.get(2).getName());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.mysema.query.collections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.animal.Cat;
|
||||
import com.mysema.query.animal.QCat;
|
||||
|
||||
public class ColUpdateClauseTest {
|
||||
|
||||
@Test
|
||||
public void testExecute() {
|
||||
QCat cat = QCat.cat;
|
||||
List<Cat> cats = Arrays.asList(new Cat("Ann"), new Cat("Bob"), new Cat("John"), new Cat("Carl"));
|
||||
|
||||
ColUpdateClause<Cat> updateClause = new ColUpdateClause<Cat>(cat, cats);
|
||||
updateClause.where(cat.name.eq("Bob"));
|
||||
updateClause.set(cat.name, "Bobby");
|
||||
assertEquals(1, updateClause.execute());
|
||||
|
||||
assertEquals("Bobby", cats.get(1).getName());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user