mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Fix for #206 - Reuse SqlUpdate when binding a list that can vary in size
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.avaje.ebeaninternal.api;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class BindParamsTest {
|
||||
|
||||
@Test
|
||||
public void test_hash() {
|
||||
|
||||
BindParams bindParams = new BindParams();
|
||||
|
||||
List<String> ids = Arrays.asList("1", "2", "3");
|
||||
bindParams.setParameter("ids", ids);
|
||||
BindParams.Param param = bindParams.getParameter("ids");
|
||||
assertEquals(3, param.queryBindCount());
|
||||
assertFalse(bindParams.isSameBindHash());
|
||||
|
||||
List<String> ids2 = Arrays.asList("1", "2", "3", "4");
|
||||
bindParams.setParameter("ids", ids2);
|
||||
assertEquals(4, param.queryBindCount());
|
||||
assertFalse(bindParams.isSameBindHash());
|
||||
|
||||
List<String> ids3 = Arrays.asList("2", "99", "44");
|
||||
bindParams.setParameter("ids", ids3);
|
||||
assertEquals(3, param.queryBindCount());
|
||||
assertFalse(bindParams.isSameBindHash());
|
||||
|
||||
|
||||
List<String> ids4 = Arrays.asList("4545", "3499", "3444");
|
||||
bindParams.setParameter("ids", ids4);
|
||||
assertEquals(3, param.queryBindCount());
|
||||
assertTrue(bindParams.isSameBindHash());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import com.avaje.ebeaninternal.api.BindParams;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BindParamsParserTest {
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
|
||||
String dml = "delete from foo where id in (:ids)";
|
||||
BindParams bindParams = new BindParams();
|
||||
|
||||
bindParams.setParameter("ids", Arrays.asList("1", "2", "3"));
|
||||
String sql1 = BindParamsParser.parse(bindParams, dml);
|
||||
assertEquals("delete from foo where id in (?,?,?)", sql1);
|
||||
|
||||
bindParams.setParameter("ids", Arrays.asList("451", "52"));
|
||||
sql1 = BindParamsParser.parse(bindParams, dml);
|
||||
assertEquals("delete from foo where id in (?,?)", sql1);
|
||||
|
||||
bindParams.setParameter("ids", Arrays.asList("545", "656"));
|
||||
sql1 = BindParamsParser.parse(bindParams, dml);
|
||||
assertEquals("delete from foo where id in (?,?)", sql1);
|
||||
|
||||
bindParams.setParameter("ids", Arrays.asList("545df", "df656", "SDF", "sdf"));
|
||||
sql1 = BindParamsParser.parse(bindParams, dml);
|
||||
assertEquals("delete from foo where id in (?,?,?,?)", sql1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.avaje.tests.update;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.SqlUpdate;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestSqlUpdateBindMultipleLists extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate("delete from o_customer where id in (:ids)");
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992, 9993));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
// 3 parameters in the IN clause
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test_multipleLists() {
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate("delete from o_customer where id in (:ids) and name in (:names)");
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992, 9993));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?,?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim", "sd"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?) and name in (?,?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9992));
|
||||
sqlUpdate.setParameter("names", asList("ro3b", "j3im"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9992,4545));
|
||||
sqlUpdate.setParameter("names", asList("ro3b"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?) and name in (?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user