#1150 - Refactor convert RawSql and RawSqlBuilder into interfaces and push the parsers into io.ebeaninternal

This commit is contained in:
rob bygrave
2017-10-04 23:03:12 +13:00
parent 5a5cb7ffbd
commit f02a552cdf
33 changed files with 994 additions and 906 deletions
@@ -1,66 +0,0 @@
package io.ebean;
import io.ebean.RawSql;
import org.junit.Test;
import java.util.Arrays;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class ColumnMappingTest {
RawSql.ColumnMapping.Column col(int indexPos, String dbColumn, String dbAlias) {
return new RawSql.ColumnMapping.Column(indexPos, dbColumn, dbAlias);
}
RawSql.ColumnMapping mapping(RawSql.ColumnMapping.Column... cols) {
return new RawSql.ColumnMapping(Arrays.asList(cols));
}
@Test
public void equals_same() {
RawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
RawSql.ColumnMapping mapping2 = mapping(col(1, "id", null), col(2, "name", null));
assertSame(mapping1, mapping2);
}
@Test
public void equals_diffPropertyName() {
RawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
RawSql.ColumnMapping mapping2 = mapping(col(1, "id", null), col(2, "diff", null));
assertDifferent(mapping1, mapping2);
}
@Test
public void equals_moreColumns() {
RawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
RawSql.ColumnMapping mapping2 = mapping(col(1, "id", null), col(2, "name", null), col(2, "diff", null));
assertDifferent(mapping1, mapping2);
}
@Test
public void equals_lessColumns() {
RawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
RawSql.ColumnMapping mapping2 = mapping(col(1, "id", null));
assertDifferent(mapping1, mapping2);
}
private void assertSame(Object key, Object key1) {
assertThat(key).isEqualTo(key1);
assertThat(key.hashCode()).isEqualTo(key1.hashCode());
}
private void assertDifferent(Object key, Object key1) {
assertThat(key).isNotEqualTo(key1);
assertThat(key.hashCode()).isNotEqualTo(key1.hashCode());
}
}
+3 -3
View File
@@ -1,15 +1,15 @@
package io.ebean;
import io.ebean.RawSql;
import io.ebeaninternal.server.rawsql.SpiRawSql;
import org.junit.Test;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class ColumnTest {
RawSql.ColumnMapping.Column col(int indexPos, String dbColumn, String dbAlias) {
return new RawSql.ColumnMapping.Column(indexPos, dbColumn, dbAlias);
SpiRawSql.ColumnMapping.Column col(int indexPos, String dbColumn, String dbAlias) {
return new SpiRawSql.ColumnMapping.Column(indexPos, dbColumn, dbAlias);
}
@Test
-66
View File
@@ -1,66 +0,0 @@
package io.ebean;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RawSqlKeyTest {
@Test
public void equals_when_sameParsedSql() {
RawSql.Key key = RawSqlBuilder.parse("select id from customer").create().getKey();
RawSql.Key key1 = RawSqlBuilder.parse("select id from customer").create().getKey();
assertSame(key, key1);
}
@Test
public void equals_when_diffParsedSql() {
RawSql.Key key = RawSqlBuilder.parse("select id from customer").create().getKey();
RawSql.Key key1 = RawSqlBuilder.parse("select name from customer").create().getKey();
assertDifferent(key, key1);
}
@Test
public void equals_when_sameColumnMapping() {
RawSql.Key key = RawSqlBuilder.parse("select id from customer").columnMapping("id", "b").create().getKey();
RawSql.Key key1 = RawSqlBuilder.parse("select id from customer").columnMapping("id", "b").create().getKey();
assertSame(key, key1);
}
@Test
public void equals_when_diffColumnMapping() {
RawSql.Key key = RawSqlBuilder.parse("select a from customer").columnMapping("a", "b").create().getKey();
RawSql.Key key1 = RawSqlBuilder.parse("select a from customer").columnMapping("a", "c").create().getKey();
assertDifferent(key, key1);
}
@Test
public void equals_when_parseToUnpased() {
RawSql.Key key = RawSqlBuilder.parse("select a from customer").columnMapping("a", "b").create().getKey();
RawSql.Key key1 = RawSqlBuilder.unparsed("select a from customer").columnMapping("a", "c").create().getKey();
assertDifferent(key, key1);
}
private void assertSame(RawSql.Key key, RawSql.Key key1) {
assertThat(key).isEqualTo(key1);
assertThat(key.hashCode()).isEqualTo(key1.hashCode());
}
private void assertDifferent(RawSql.Key key, RawSql.Key key1) {
assertThat(key).isNotEqualTo(key1);
assertThat(key.hashCode()).isNotEqualTo(key1.hashCode());
}
}
@@ -0,0 +1,65 @@
package io.ebeaninternal.server.rawsql;
import org.junit.Test;
import java.util.Arrays;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class ColumnMappingTest {
SpiRawSql.ColumnMapping.Column col(int indexPos, String dbColumn, String dbAlias) {
return new SpiRawSql.ColumnMapping.Column(indexPos, dbColumn, dbAlias);
}
SpiRawSql.ColumnMapping mapping(SpiRawSql.ColumnMapping.Column... cols) {
return new SpiRawSql.ColumnMapping(Arrays.asList(cols));
}
@Test
public void equals_same() {
SpiRawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
SpiRawSql.ColumnMapping mapping2 = mapping(col(1, "id", null), col(2, "name", null));
assertSame(mapping1, mapping2);
}
@Test
public void equals_diffPropertyName() {
SpiRawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
SpiRawSql.ColumnMapping mapping2 = mapping(col(1, "id", null), col(2, "diff", null));
assertDifferent(mapping1, mapping2);
}
@Test
public void equals_moreColumns() {
SpiRawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
SpiRawSql.ColumnMapping mapping2 = mapping(col(1, "id", null), col(2, "name", null), col(2, "diff", null));
assertDifferent(mapping1, mapping2);
}
@Test
public void equals_lessColumns() {
SpiRawSql.ColumnMapping mapping1 = mapping(col(1, "id", null), col(2, "name", null));
SpiRawSql.ColumnMapping mapping2 = mapping(col(1, "id", null));
assertDifferent(mapping1, mapping2);
}
private void assertSame(Object key, Object key1) {
assertThat(key).isEqualTo(key1);
assertThat(key.hashCode()).isEqualTo(key1.hashCode());
}
private void assertDifferent(Object key, Object key1) {
assertThat(key).isNotEqualTo(key1);
assertThat(key.hashCode()).isNotEqualTo(key1.hashCode());
}
}
@@ -0,0 +1,74 @@
package io.ebeaninternal.server.rawsql;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RawSqlKeyTest {
private SpiRawSql.Key key(String sqlStatement) {
return ((SpiRawSql) RawSqlBuilder.parse(sqlStatement).create()).getKey();
}
private SpiRawSql.Key key(RawSql rawSql) {
return ((SpiRawSql)rawSql).getKey();
}
@Test
public void equals_when_sameParsedSql() {
SpiRawSql.Key key = key("select id from customer");
SpiRawSql.Key key1 = key("select id from customer");
assertSame(key, key1);
}
@Test
public void equals_when_diffParsedSql() {
SpiRawSql.Key key = key("select id from customer");
SpiRawSql.Key key1 = key("select name from customer");
assertDifferent(key, key1);
}
@Test
public void equals_when_sameColumnMapping() {
SpiRawSql.Key key = key(RawSqlBuilder.parse("select id from customer").columnMapping("id", "b").create());
SpiRawSql.Key key1 = key(RawSqlBuilder.parse("select id from customer").columnMapping("id", "b").create());
assertSame(key, key1);
}
@Test
public void equals_when_diffColumnMapping() {
SpiRawSql.Key key = key(RawSqlBuilder.parse("select a from customer").columnMapping("a", "b").create());
SpiRawSql.Key key1 = key(RawSqlBuilder.parse("select a from customer").columnMapping("a", "c").create());
assertDifferent(key, key1);
}
@Test
public void equals_when_parseToUnpased() {
SpiRawSql.Key key = key(RawSqlBuilder.parse("select a from customer").columnMapping("a", "b").create());
SpiRawSql.Key key1 = key(RawSqlBuilder.unparsed("select a from customer").columnMapping("a", "c").create());
assertDifferent(key, key1);
}
private void assertSame(SpiRawSql.Key key, SpiRawSql.Key key1) {
assertThat(key).isEqualTo(key1);
assertThat(key.hashCode()).isEqualTo(key1.hashCode());
}
private void assertDifferent(SpiRawSql.Key key, SpiRawSql.Key key1) {
assertThat(key).isNotEqualTo(key1);
assertThat(key.hashCode()).isNotEqualTo(key1.hashCode());
}
}
@@ -1,6 +1,12 @@
package io.ebean;
package io.ebeaninternal.server.rawsql;
import io.ebean.RawSql.Sql;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.Query;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import io.ebeaninternal.server.rawsql.SpiRawSql.Sql;
import org.assertj.core.api.StrictAssertions;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import org.tests.model.rawsql.ERawSqlAggBean;
@@ -14,19 +20,23 @@ public class TestRawSqlBuilder extends BaseTestCase {
@Test
public void testDeriveProperty() {
assertThat(RawSql.ColumnMapping.Column.derivePropertyName("item_total", "some_other")).isEqualTo("itemTotal");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "some_other")).isEqualTo("someOther");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "alias.some_other")).isEqualTo("someOther");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "alias.someOther")).isEqualTo("someOther");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "some")).isEqualTo("some");
assertThat(RawSql.ColumnMapping.Column.derivePropertyName(null, "someOther")).isEqualTo("someOther");
StrictAssertions.assertThat(SpiRawSql.ColumnMapping.Column.derivePropertyName("item_total", "some_other")).isEqualTo("itemTotal");
assertThat(SpiRawSql.ColumnMapping.Column.derivePropertyName(null, "some_other")).isEqualTo("someOther");
assertThat(SpiRawSql.ColumnMapping.Column.derivePropertyName(null, "alias.some_other")).isEqualTo("someOther");
assertThat(SpiRawSql.ColumnMapping.Column.derivePropertyName(null, "alias.someOther")).isEqualTo("someOther");
assertThat(SpiRawSql.ColumnMapping.Column.derivePropertyName(null, "some")).isEqualTo("some");
assertThat(SpiRawSql.ColumnMapping.Column.derivePropertyName(null, "someOther")).isEqualTo("someOther");
}
private Sql getSql(String sqlStatement) {
RawSql r = RawSqlBuilder.parse(sqlStatement).create();
return ((SpiRawSql)r).getSql();
}
@Test
public void testSimple() {
RawSqlBuilder r = RawSqlBuilder.parse("select id from t_cust");
Sql sql = r.getSql();
Sql sql = getSql("select id from t_cust");
assertEquals("id", sql.getPreFrom());
assertEquals("from t_cust", sql.getPreWhere());
assertEquals("", sql.getPreHaving());
@@ -36,8 +46,7 @@ public class TestRawSqlBuilder extends BaseTestCase {
@Test
public void testWithNewLineCharacters() {
RawSqlBuilder r = RawSqlBuilder.parse("select\n id from\n o_customer");
Sql sql = r.getSql();
Sql sql = getSql("select\n id from\n o_customer");
assertEquals("id", sql.getPreFrom());
assertEquals("from o_customer", sql.getPreWhere());
@@ -46,7 +55,7 @@ public class TestRawSqlBuilder extends BaseTestCase {
ResetBasicData.reset();
RawSql rawSql = r.create();
RawSql rawSql = RawSqlBuilder.parse("select\n id from\n o_customer").create();
Ebean.find(Customer.class)
.setRawSql(rawSql)
@@ -56,8 +65,8 @@ public class TestRawSqlBuilder extends BaseTestCase {
@Test
public void testWithWhere() {
RawSqlBuilder r = RawSqlBuilder.parse("select id from t_cust where id > ?");
Sql sql = r.getSql();
Sql sql = getSql("select id from t_cust where id > ?");
assertEquals("id", sql.getPreFrom());
assertEquals("from t_cust where id > ?", sql.getPreWhere());
assertEquals("", sql.getPreHaving());
@@ -67,24 +76,21 @@ public class TestRawSqlBuilder extends BaseTestCase {
@Test
public void testWithOrder() {
RawSqlBuilder r = RawSqlBuilder.parse("select id from t_cust where id > ? order by id desc");
Sql sql = r.getSql();
Sql sql = getSql("select id from t_cust where id > ? order by id desc");
assertEquals("id", sql.getPreFrom());
assertEquals("from t_cust where id > ?", sql.getPreWhere());
assertEquals("", sql.getPreHaving());
assertEquals("order by", sql.getOrderByPrefix());
assertEquals("id desc", sql.getOrderBy());
r = RawSqlBuilder.parse("select id from t_cust order by id desc");
sql = r.getSql();
sql = getSql("select id from t_cust order by id desc");
assertEquals("id", sql.getPreFrom());
assertEquals("from t_cust", sql.getPreWhere());
assertEquals("", sql.getPreHaving());
assertEquals("id desc", sql.getOrderBy());
r = RawSqlBuilder
.parse("select id, sum(x) from t_cust where id > ? group by id order by id desc");
sql = r.getSql();
sql = getSql("select id, sum(x) from t_cust where id > ? group by id order by id desc");
assertEquals("id, sum(x)", sql.getPreFrom());
assertEquals("from t_cust where id > ?", sql.getPreWhere());
assertEquals("group by id", sql.getPreHaving());
@@ -94,9 +100,7 @@ public class TestRawSqlBuilder extends BaseTestCase {
@Test
public void testWithHaving() {
RawSqlBuilder r = RawSqlBuilder
.parse("select id, sum(x) from t_cust where id > ? group by id having sum(x) > ? order by id desc");
Sql sql = r.getSql();
Sql sql = getSql("select id, sum(x) from t_cust where id > ? group by id having sum(x) > ? order by id desc");
assertEquals("id, sum(x)", sql.getPreFrom());
assertEquals("from t_cust where id > ?", sql.getPreWhere());
assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
@@ -104,9 +108,7 @@ public class TestRawSqlBuilder extends BaseTestCase {
assertEquals("id desc", sql.getOrderBy());
// no where
r = RawSqlBuilder
.parse("select id, sum(x) from t_cust group by id having sum(x) > ? order by id desc");
sql = r.getSql();
sql = getSql("select id, sum(x) from t_cust group by id having sum(x) > ? order by id desc");
assertEquals("id, sum(x)", sql.getPreFrom());
assertEquals("from t_cust", sql.getPreWhere());
assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
@@ -114,8 +116,7 @@ public class TestRawSqlBuilder extends BaseTestCase {
assertEquals("id desc", sql.getOrderBy());
// no where, no order by
r = RawSqlBuilder.parse("select id, sum(x) from t_cust group by id having sum(x) > ?");
sql = r.getSql();
sql = getSql("select id, sum(x) from t_cust group by id having sum(x) > ?");
assertEquals("id, sum(x)", sql.getPreFrom());
assertEquals("from t_cust", sql.getPreWhere());
assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
@@ -123,9 +124,7 @@ public class TestRawSqlBuilder extends BaseTestCase {
assertEquals("order by", sql.getOrderByPrefix());
// no order by
r = RawSqlBuilder
.parse("select id, sum(x) from t_cust where id > ? group by id having sum(x) > ?");
sql = r.getSql();
sql = getSql("select id, sum(x) from t_cust where id > ? group by id having sum(x) > ?");
assertEquals("id, sum(x)", sql.getPreFrom());
assertEquals("from t_cust where id > ?", sql.getPreWhere());
assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
@@ -140,15 +139,11 @@ public class TestRawSqlBuilder extends BaseTestCase {
public void testWithOrderSiblingsByName() {
String s = "SELECT ID, DESCRIPTION, NAME, PARENT_ID FROM SOME_TABLE WHERE lower(NAME) like :name START WITH ID = :parentId CONNECT BY PRIOR ID = PARENT_ID order siblings by NAME";
RawSql rawSql = RawSqlBuilder.parse(s).create();
Sql sql = rawSql.getSql();
Sql sql = getSql(s);
assertEquals("ID, DESCRIPTION, NAME, PARENT_ID", sql.getPreFrom());
assertEquals("order siblings by", sql.getOrderByPrefix());
assertEquals("NAME", sql.getOrderBy());
assertEquals("FROM SOME_TABLE WHERE lower(NAME) like :name START WITH ID = :parentId CONNECT BY PRIOR ID = PARENT_ID", sql.getPreWhere());
}
@@ -164,13 +159,13 @@ public class TestRawSqlBuilder extends BaseTestCase {
"order by o.id, d.id asc";
RawSql rawSql = RawSqlBuilder.parse(rs)
SpiRawSql rawSql = (SpiRawSql)RawSqlBuilder.parse(rs)
.tableAliasMapping("c", "customer")
.tableAliasMapping("d", "details")
.tableAliasMapping("p", "details.product")
.create();
RawSql.ColumnMapping columnMapping = rawSql.getColumnMapping();
SpiRawSql.ColumnMapping columnMapping = rawSql.getColumnMapping();
assertEquals(0, columnMapping.getIndexPosition("id"));
assertEquals(1, columnMapping.getIndexPosition("status"));
assertEquals(2, columnMapping.getIndexPosition("customer.id"));
@@ -194,8 +189,8 @@ public class TestRawSqlBuilder extends BaseTestCase {
RawSqlBuilder builder = RawSqlBuilder.parse(rs);
RawSql rawSql = builder.create();
RawSql.ColumnMapping columnMapping = rawSql.getColumnMapping();
SpiRawSql rawSql = (SpiRawSql)builder.create();
SpiRawSql.ColumnMapping columnMapping = rawSql.getColumnMapping();
assertEquals(0, columnMapping.getIndexPosition("id"));
assertEquals(1, columnMapping.getIndexPosition("status"));
@@ -221,9 +216,9 @@ public class TestRawSqlBuilder extends BaseTestCase {
" from o_order d" +
" group by DATE_TRUNC('DAY', d.order_date)";
RawSql rawSql = RawSqlBuilder.parse(sql).create();
SpiRawSql rawSql = (SpiRawSql)RawSqlBuilder.parse(sql).create();
RawSql.ColumnMapping columnMapping = rawSql.getColumnMapping();
SpiRawSql.ColumnMapping columnMapping = rawSql.getColumnMapping();
assertEquals(0, columnMapping.getIndexPosition("day"));
assertEquals(1, columnMapping.getIndexPosition("total"));
@@ -1,7 +1,8 @@
package io.ebean;
package io.ebeaninternal.server.rawsql;
import io.ebean.RawSql.Sql;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import io.ebeaninternal.server.rawsql.SpiRawSql.Sql;
import junit.framework.TestCase;
import org.junit.Assert;
@@ -9,8 +10,8 @@ public class TestRawSqlBuilderDistinct extends TestCase {
public void testDistinct() {
RawSqlBuilder r = RawSqlBuilder.parse("select distinct id, name from t_cust");
Sql sql = r.getSql();
RawSql r = RawSqlBuilder.parse("select distinct id, name from t_cust").create();
Sql sql = ((SpiRawSql)r).getSql();
Assert.assertEquals("id, name", sql.getPreFrom());
Assert.assertEquals("from t_cust", sql.getPreWhere());
Assert.assertEquals("", sql.getPreHaving());
@@ -1,7 +1,7 @@
package io.ebean;
package io.ebeaninternal.server.rawsql;
import io.ebean.RawSql.ColumnMapping;
import io.ebean.RawSql.ColumnMapping.Column;
import io.ebeaninternal.server.rawsql.SpiRawSql.ColumnMapping;
import io.ebeaninternal.server.rawsql.SpiRawSql.ColumnMapping.Column;
import junit.framework.TestCase;
import java.util.Map;
@@ -1,7 +1,7 @@
package io.ebeaninternal.server.rawsql;
import io.ebean.RawSql;
import io.ebean.RawSql.Sql;
import io.ebeaninternal.server.rawsql.SpiRawSql.Sql;
import io.ebean.RawSqlBuilder;
import junit.framework.TestCase;
@@ -20,7 +20,7 @@ public class TestRawSqlParsing extends TestCase {
//.columnMapping("sum(order_qty*unit_price)","totalAmount")
.create();
Sql rs = rawSql.getSql();
Sql rs = ((SpiRawSql)rawSql).getSql();
String s = rs.toString();
assertTrue(s, s.contains("[order_id, sum"));
@@ -4,6 +4,7 @@ import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.FetchConfig;
import io.ebean.RawSql;
import io.ebean.RawSqlBuilder;
import io.ebean.Transaction;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
@@ -33,7 +34,7 @@ public class TestRawSqlWithResultSet extends BaseTestCase {
// ResultSet will be closed by Ebean
ResultSet resultSet = pstmt.executeQuery();
RawSql rawSql = new RawSql(resultSet, "id", "name", "billingAddress.id");
RawSql rawSql = RawSqlBuilder.resultSet(resultSet, "id", "name", "billingAddress.id");
List<Customer> list = Ebean.find(Customer.class)
.setRawSql(rawSql)