mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 15:55:55 +00:00
#766 - RawSql should automatically map column alias with underscore to camelCase ... so item_total should automatically map to bean property itemTotal
This commit is contained in:
@@ -606,9 +606,9 @@ public final class RawSql implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
private static String derivePropertyName(String dbAlias, String dbColumn) {
|
||||
protected static String derivePropertyName(String dbAlias, String dbColumn) {
|
||||
if (dbAlias != null) {
|
||||
return dbAlias;
|
||||
return CamelCaseHelper.toCamelFromUnderscore(dbAlias);
|
||||
}
|
||||
int dotPos = dbColumn.indexOf('.');
|
||||
if (dotPos > -1) {
|
||||
|
||||
@@ -12,9 +12,12 @@ public class CamelCaseHelper {
|
||||
*/
|
||||
public static String toCamelFromUnderscore(String underscore) {
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
String[] vals = underscore.split("_");
|
||||
if (vals.length == 1) {
|
||||
return underscore;
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
String lower = vals[i].toLowerCase();
|
||||
if (i > 0) {
|
||||
|
||||
@@ -13,6 +13,16 @@ import static org.junit.Assert.assertNull;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimple() {
|
||||
|
||||
@@ -192,9 +202,9 @@ public class TestRawSqlBuilder extends BaseTestCase {
|
||||
assertEquals(0, columnMapping.getIndexPosition("id"));
|
||||
assertEquals(1, columnMapping.getIndexPosition("status"));
|
||||
assertEquals(2, columnMapping.getIndexPosition("budget"));
|
||||
assertEquals(3, columnMapping.getIndexPosition("transaction_sum"));
|
||||
assertEquals(3, columnMapping.getIndexPosition("transactionSum"));
|
||||
assertEquals(4, columnMapping.getIndexPosition("balance"));
|
||||
assertEquals(5, columnMapping.getIndexPosition("data_month"));
|
||||
assertEquals(5, columnMapping.getIndexPosition("dataMonth"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.avaje.ebean.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CamelCaseHelperTest {
|
||||
|
||||
@Test
|
||||
public void when_underscore() throws Exception {
|
||||
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there"), "helloThere");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_jim"), "helloThereJim");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_trailing_numbers() throws Exception {
|
||||
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_1"), "hello1");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello_there_2"), "helloThere2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_already_camel() throws Exception {
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThere"), "helloThere");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("helloThereJim"), "helloThereJim");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("hello"), "hello");
|
||||
assertEquals(CamelCaseHelper.toCamelFromUnderscore("HELLO"), "HELLO");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_when_explicitMapping() {
|
||||
public void test_when_aliasInUnderscore() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
@@ -62,8 +62,6 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql)
|
||||
.columnMapping("order_id", "order.id")
|
||||
.columnMapping("total_items", "totalItems")
|
||||
.columnMapping("total_amount", "totalAmount")
|
||||
.create();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class)
|
||||
@@ -74,6 +72,28 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_when_aliasInCamelCase() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql =
|
||||
"select order_id, count(*) as totalItems, sum(order_qty*unit_price) as totalAmount \n" +
|
||||
"from o_order_detail \n" +
|
||||
"group by order_id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql)
|
||||
.columnMapping("order_id", "order.id")
|
||||
.create();
|
||||
|
||||
Query<OrderAggregate> query = Ebean.find(OrderAggregate.class)
|
||||
.setRawSql(rawSql);
|
||||
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultNamedRawSql() {
|
||||
|
||||
@@ -92,7 +112,7 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
Query<OrderAggregate> query = Ebean.getDefaultServer().createNamedQuery(OrderAggregate.class, "withMax");
|
||||
List<OrderAggregate> list = query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount from o_order_detail");
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount, max(order_qty*unit_price) as maxAmount from o_order_detail");
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@@ -108,7 +128,7 @@ public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
.order().desc("totalAmount")
|
||||
.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount from o_order_detail");
|
||||
assertThat(query.getGeneratedSql()).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount, max(order_qty*unit_price) as maxAmount from o_order_detail");
|
||||
assertThat(query.getGeneratedSql()).contains("from o_order_detail where order_id > ? group by order_id having count(*) > ? order by sum(order_qty*unit_price) desc");
|
||||
assertNotNull(list);
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
<entity class="com.avaje.tests.model.basic.OrderAggregate">
|
||||
<raw-sql name="default">
|
||||
<column-mapping column="order_id" property="order.id"/>
|
||||
<column-mapping column="total_items" property="totalItems"/>
|
||||
<column-mapping column="total_amount" property="totalAmount"/>
|
||||
<query>
|
||||
select order_id, count(*) as total_items, sum(order_qty*unit_price) as total_amount
|
||||
from o_order_detail
|
||||
@@ -16,7 +14,7 @@
|
||||
<raw-sql name="withMax">
|
||||
<column-mapping column="order_id" property="order.id"/>
|
||||
<query>
|
||||
select order_id, count(*) as totalItems, sum(order_qty*unit_price) as totalAmount, max(order_qty*unit_price) as maxAmount
|
||||
select order_id, count(*) as total_items, sum(order_qty*unit_price) as total_amount, max(order_qty*unit_price) as maxAmount
|
||||
from o_order_detail
|
||||
group by order_id
|
||||
</query>
|
||||
|
||||
Reference in New Issue
Block a user