Refactor SQL generation - remove excess whitespace (double spaces before where, join and order by) (#2065)

This commit is contained in:
Rob Bygrave
2020-10-07 21:28:33 +13:00
committed by GitHub
parent 694518db0a
commit de22b7bf27
42 changed files with 96 additions and 108 deletions
@@ -51,7 +51,7 @@ public class TestAddOrderByWithFirstRowsMaxRows extends BaseTestCase {
assertThat(loggedSql).hasSize(1);
if (isH2()) {
assertThat(loggedSql.get(0)).contains("from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id limit 10");
assertThat(loggedSql.get(0)).contains("from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id limit 10");
}
}
@@ -112,7 +112,7 @@ public class TestAddOrderByWithFirstRowsMaxRows extends BaseTestCase {
assertThat(loggedSql).hasSize(1);
if (isH2()) {
assertThat(loggedSql.get(0)).contains("join o_customer t1 on t1.id = t0.kcustomer_id limit 10");
assertThat(loggedSql.get(0)).contains("join o_customer t1 on t1.id = t0.kcustomer_id limit 10");
}
}
@@ -24,11 +24,11 @@ public class TestImplicitJoinOnParentRelationship extends BaseTestCase {
query.findList();
if (isPostgres()) {
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id join o_order_detail u2 on u2.order_id = u1.id join o_product u3 on u3.id = u2.product_id where u3.name = ?";
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id join o_order_detail u2 on u2.order_id = u1.id join o_product u3 on u3.id = u2.product_id where u3.name = ?";
assertThat(sqlOf(query, 1)).contains(expectedSql);
} else {
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id join o_order_detail u2 on u2.order_id = u1.id join o_product u3 on u3.id = u2.product_id where u3.name = ?";
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id join o_order_detail u2 on u2.order_id = u1.id join o_product u3 on u3.id = u2.product_id where u3.name = ?";
assertThat(sqlOf(query, 1)).contains(expectedSql);
}
@@ -55,10 +55,10 @@ public class TestImplicitJoinOnParentRelationship extends BaseTestCase {
query.findList();
if (isPostgres()) {
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
assertThat(sqlOf(query, 1)).contains(expectedSql);
} else {
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
assertThat(sqlOf(query, 1)).contains(expectedSql);
}
}
@@ -76,11 +76,11 @@ public class TestImplicitJoinOnParentRelationship extends BaseTestCase {
query.findList();
if (isPostgres()) {
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
String expectedSql = "select distinct on (t0.id) t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
assertThat(sqlOf(query, 1)).contains(expectedSql);
} else {
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
String expectedSql = "select distinct t0.id, t0.name from o_customer t0 left join o_order u1 on u1.kcustomer_id = t0.id left join o_order_detail u2 on u2.order_id = u1.id left join o_product u3 on u3.id = u2.product_id where (u3.name = ? or t0.id = ?)";
assertThat(sqlOf(query, 1)).contains(expectedSql);
}
}
@@ -41,7 +41,7 @@ public class TestManyWhereJoin extends BaseTestCase {
}
assertThat(sql).contains("join o_order ");
assertThat(sql).contains(".status = ?");
assertThat(sql).contains("t0.id, t0.status from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id where u1.status = ?");
assertThat(sql).contains("t0.id, t0.status from o_customer t0 join o_order u1 on u1.kcustomer_id = t0.id where u1.status = ?");
}
@Test
@@ -66,7 +66,7 @@ public class TestWhereIn extends BaseTestCase {
.where().inOrEmpty("customer.billingAddress.id", Arrays.asList(1)).query();
query.findList();
assertThat(sqlOf(query)).contains("select t0.id from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id where t1.billing_address_id ");
assertThat(sqlOf(query)).contains("select t0.id from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id where t1.billing_address_id ");
}
@@ -199,7 +199,7 @@ public class TestAggregationCount extends BaseTestCase {
query0.findList();
String sql = sqlOf(query0, 5);
assertThat(sql).contains("select t0.id, t0.name, count(u1.id), t1.id, t1.name from tevent_one t0 left join tevent t1 on t1.id = t0.event_id join tevent_many u1 on u1.event_id = t0.id ");
assertThat(sql).contains("select t0.id, t0.name, count(u1.id), t1.id, t1.name from tevent_one t0 left join tevent t1 on t1.id = t0.event_id join tevent_many u1 on u1.event_id = t0.id ");
assertThat(sql).contains("group by t0.id, t0.name, t1.id, t1.name");
}
@@ -58,11 +58,11 @@ public class TestDisjunctWhereOuterOnMany extends BaseTestCase {
Assert.assertEquals(2, rowCount);
if (isPostgres()) {
String expectedSql = "select distinct on (t0.id) t0.id, t0.name, t0.description, t0.version from uuone t0 left join uutwo u1 on u1.master_id = t0.id where (t0.name = ? or u1.name = ?)";
String expectedSql = "select distinct on (t0.id) t0.id, t0.name, t0.description, t0.version from uuone t0 left join uutwo u1 on u1.master_id = t0.id where (t0.name = ? or u1.name = ?)";
assertSql(query).contains(expectedSql);
} else {
String expectedSql = "select distinct t0.id, t0.name, t0.description, t0.version from uuone t0 left join uutwo u1 on u1.master_id = t0.id where (t0.name = ? or u1.name = ?)";
String expectedSql = "select distinct t0.id, t0.name, t0.description, t0.version from uuone t0 left join uutwo u1 on u1.master_id = t0.id where (t0.name = ? or u1.name = ?)";
assertSql(query).contains(expectedSql);
}
@@ -26,7 +26,7 @@ public class TestQueryManyToOneWhereClauseJoin extends BaseTestCase {
query.findList();
//select t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t1.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7
String expectedSql = "from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id where lower(t1.name) like ";
String expectedSql = "from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id where lower(t1.name) like ";
Assert.assertTrue(query.getGeneratedSql().contains(expectedSql));
// select t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t1.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7
@@ -50,7 +50,7 @@ public class TestQueryManyToOneWhereClauseJoin extends BaseTestCase {
query.findList();
//select t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t1.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7
String expectedSql = "from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id where (lower(t1.name) like ";
String expectedSql = "from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id where (lower(t1.name) like ";
Assert.assertTrue(query.getGeneratedSql().contains(expectedSql));
// select t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t1.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7
@@ -38,7 +38,7 @@ public class TestOrderByOnComplex extends BaseTestCase {
List<Order> list = query.findList();
assertThat(sqlOf(query)).contains("select t0.id, t0.status, t0.order_date, t0.ship_date, t1.name, t0.cretime, t0.updtime, t0.kcustomer_id from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id order by case when t0.status=3 then 10 when t0.status=2 then 11 else 99 end");
assertThat(sqlOf(query)).contains("select t0.id, t0.status, t0.order_date, t0.ship_date, t1.name, t0.cretime, t0.updtime, t0.kcustomer_id from o_order t0 join o_customer t1 on t1.id = t0.kcustomer_id order by case when t0.status=3 then 10 when t0.status=2 then 11 else 99 end");
assertThat(list).isNotEmpty();
}
@@ -38,7 +38,7 @@ public class TestFetchPreference extends BaseTestCase {
String sql = sqlOf(query, 1);
// join to group (the ToOne part only) and participants (our preferred ToMany path)
assertThat(sql).contains(" from c_conversation t0 left join c_group t1 on t1.id = t0.group_id left join c_participation t2");
assertThat(sql).contains(" from c_conversation t0 left join c_group t1 on t1.id = t0.group_id left join c_participation t2");
}
@Test
@@ -66,7 +66,7 @@ public class TestQueryConversationRowCount extends BaseTestCase {
Assert.assertEquals(1, loggedSql.size());
String countSql = trimSql(loggedSql.get(0), 0);
assertThat(countSql).contains("select count(*) from ( select distinct t0.id from c_conversation t0 left join c_participation u1 on u1.conversation_id = t0.id where t0.group_id = ? and ((t0.isopen = ? and u1.user_id = ?) or t0.isopen = ?))");
assertThat(countSql).contains("select count(*) from ( select distinct t0.id from c_conversation t0 left join c_participation u1 on u1.conversation_id = t0.id where t0.group_id = ? and ((t0.isopen = ? and u1.user_id = ?) or t0.isopen = ?))");
}
}
@@ -30,6 +30,6 @@ public class TestQueryRawExpressionMany extends BaseTestCase {
query.findCount();
List<String> sql = LoggedSqlCollector.stop();
assertThat(trimSql(sql.get(0), 1)).contains("select count(*) from ( select distinct t0.id from o_order t0 left join o_order_detail t1 on t1.order_id = t0.id where t1.order_qty = ?)");
assertThat(trimSql(sql.get(0), 1)).contains("select count(*) from ( select distinct t0.id from o_order t0 left join o_order_detail t1 on t1.order_id = t0.id where t1.order_qty = ?)");
}
}
@@ -69,7 +69,7 @@ public class TestQueryRowCountWithMany extends BaseTestCase {
assertEquals(list.size(), rowCount);
assertEquals(2, sqlLogged.size());
assertThat(trimSql(sqlLogged.get(1), 1)).contains(
"select count(*) from ( select distinct t0.id from o_order t0 join o_order_detail u1 on u1.order_id = t0.id where u1.product_id = ?)");
"select count(*) from ( select distinct t0.id from o_order t0 join o_order_detail u1 on u1.order_id = t0.id where u1.product_id = ?)");
}
@@ -95,7 +95,7 @@ public class TestQueryRowCountWithMany extends BaseTestCase {
List<String> sqlLogged = LoggedSqlCollector.stop();
assertEquals(1, sqlLogged.size());
assertThat(trimSql(sqlLogged.get(0), 1)).contains("select count(*) from ( select distinct t0.id from o_order t0 join o_order_detail u1 on u1.order_id = t0.id where u1.product_id = ?)");
assertThat(trimSql(sqlLogged.get(0), 1)).contains("select count(*) from ( select distinct t0.id from o_order t0 join o_order_detail u1 on u1.order_id = t0.id where u1.product_id = ?)");
query.findList();
}
@@ -161,7 +161,7 @@ public class TestQuerySingleAttribute extends BaseTestCase {
List<String> names = query.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select distinct t0.name from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id where t0.status = ? and lower(t1.city) like ");
assertThat(sqlOf(query)).contains("select distinct t0.name from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id where t0.status = ? and lower(t1.city) like ");
assertThat(names).isNotNull();
}
@@ -224,7 +224,7 @@ public class TestQuerySingleAttribute extends BaseTestCase {
List<String> cities = query.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select distinct t2.city from contact t0 join o_customer t1 on t1.id = t0.customer_id left join o_address t2 on t2.id = t1.billing_address_id");
assertThat(sqlOf(query)).contains("select distinct t2.city from contact t0 join o_customer t1 on t1.id = t0.customer_id left join o_address t2 on t2.id = t1.billing_address_id");
assertThat(cities).contains("Auckland").containsNull();
}
@@ -404,7 +404,7 @@ public class TestQuerySingleAttribute extends BaseTestCase {
query.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 join o_customer t1 on t1.id = t0.customer_id order by t1.billing_address_id desc");
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 join o_customer t1 on t1.id = t0.customer_id order by t1.billing_address_id desc");
}
@Test
@@ -422,8 +422,8 @@ public class TestQuerySingleAttribute extends BaseTestCase {
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id " // two spaces!
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address_id desc");
}
@@ -443,8 +443,8 @@ public class TestQuerySingleAttribute extends BaseTestCase {
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address_id desc");
}
@@ -464,8 +464,8 @@ public class TestQuerySingleAttribute extends BaseTestCase {
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address");
}
@@ -485,8 +485,8 @@ public class TestQuerySingleAttribute extends BaseTestCase {
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.shipping_address_id "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.shipping_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address_id desc");
@@ -536,8 +536,8 @@ public class TestQuerySingleAttribute extends BaseTestCase {
.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) from ("
+ "select t2.line_1 as attribute_ "
+ "from contact t0 join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.shipping_address_id "
+ "from contact t0 join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.shipping_address_id"
+ ") r1 group by r1.attribute_ order by r1.attribute_");
assertThat(list2.get(0)).isInstanceOf(CountedValue.class);
//assertThat(list2.toString()).isEqualTo("[1: null, 3: 1 Banana St, 5: 12 Apple St, 3: 15 Kumera Way]");
@@ -550,8 +550,8 @@ public class TestQuerySingleAttribute extends BaseTestCase {
.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) from ("
+ "select t0.first_name as attribute_ from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.shipping_address_id where t2.line_1 = ?"
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.shipping_address_id where t2.line_1 = ?"
+ ") r1 group by r1.attribute_ order by r1.attribute_");
assertThat(list3.get(0)).isInstanceOf(CountedValue.class);
//assertThat(list3.toString()).isEqualTo("[1: Bugs1, 1: Fiona, 1: Fred1, 1: Jim1, 1: Tracy]");
@@ -567,9 +567,9 @@ public class TestQuerySingleAttribute extends BaseTestCase {
.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select r1.attribute_, count(*) from ("
+ "select t2.line_1 as attribute_ "
+ "from contact t0 join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "left join o_address t3 on t3.id = t1.shipping_address_id "
+ "from contact t0 join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "left join o_address t3 on t3.id = t1.shipping_address_id "
+ "where (t3.line_1 <> ? or t3.line_1 is null)"
+ ") r1 group by r1.attribute_ order by r1.attribute_");
assertThat(list4.get(0)).isInstanceOf(CountedValue.class);
@@ -584,12 +584,12 @@ public class TestQuerySingleAttribute extends BaseTestCase {
.findSingleAttributeList();
if (isOracle()) {
assertSql(query).contains("select r1.attribute_, count(*) from (select t2.line_1 as attribute_ from contact t0 join o_customer t1 on t1.id = t0.customer_id left join o_address t2 on t2.id = t1.billing_address_id where t2.line_1 is not null) r1 group by r1.attribute_ order by r1.attribute_ desc offset 1 rows fetch next 2 rows only");
assertSql(query).contains("select r1.attribute_, count(*) from (select t2.line_1 as attribute_ from contact t0 join o_customer t1 on t1.id = t0.customer_id left join o_address t2 on t2.id = t1.billing_address_id where t2.line_1 is not null) r1 group by r1.attribute_ order by r1.attribute_ desc offset 1 rows fetch next 2 rows only");
} else {
assertSql(query).contains("select r1.attribute_, count(*) from ("
+ "select t2.line_1 as attribute_ from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.line_1 is not null"
+ ") r1 group by r1.attribute_ order by r1.attribute_ desc ");
}