mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 12:53:59 +00:00
EQL - parsing fetch clause
This commit is contained in:
@@ -88,6 +88,28 @@ class EqlAdapter<T> extends EQLBaseListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterFetch_path(EQLParser.Fetch_pathContext ctx) {
|
||||
int childCount = ctx.getChildCount();
|
||||
checkChildren(ctx, 2);
|
||||
String path = child(ctx, 1);
|
||||
if (childCount == 2) {
|
||||
query.fetch(path);
|
||||
|
||||
} else {
|
||||
String fetchProperties = trimParenthesis(ctx.getChild(2).getText());
|
||||
query.fetch(path, fetchProperties);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim leading '(' and trailing ')'
|
||||
*/
|
||||
private String trimParenthesis(String text) {
|
||||
text = text.substring(1);
|
||||
text = text.substring(0, text.length()-1);
|
||||
return text;
|
||||
}
|
||||
|
||||
private String getLeftHandSidePath(ParserRuleContext ctx) {
|
||||
TerminalNode pathToken = ctx.getToken(EQLLexer.PATH_VARIABLE, 0);
|
||||
@@ -128,7 +150,7 @@ class EqlAdapter<T> extends EQLBaseListener {
|
||||
int childCount = ctx.getChildCount();
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
String text = child(ctx, i);
|
||||
if (isInValue(text)) {
|
||||
if (isValue(text)) {
|
||||
inValues.add(helper.bind(text));
|
||||
}
|
||||
}
|
||||
@@ -139,7 +161,7 @@ class EqlAdapter<T> extends EQLBaseListener {
|
||||
return child.getText();
|
||||
}
|
||||
|
||||
private boolean isInValue(String text) {
|
||||
private boolean isValue(String text) {
|
||||
if (text.length() == 1 && (text.equals("(") || text.equals(")") || text.equals(","))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -71,6 +71,18 @@ public class EQLBaseListener implements EQLListener {
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitFetch_path(EQLParser.Fetch_pathContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterFetch_property_set(EQLParser.Fetch_property_setContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitFetch_property_set(EQLParser.Fetch_property_setContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
||||
@@ -57,6 +57,16 @@ public interface EQLListener extends ParseTreeListener {
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitFetch_path(EQLParser.Fetch_pathContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link EQLParser#fetch_property_set}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void enterFetch_property_set(EQLParser.Fetch_property_setContext ctx);
|
||||
/**
|
||||
* Exit a parse tree produced by {@link EQLParser#fetch_property_set}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitFetch_property_set(EQLParser.Fetch_property_setContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link EQLParser#fetch_property_group}.
|
||||
* @param ctx the parse tree
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -185,6 +185,43 @@ public class EqlParserTest {
|
||||
assertThat(query.getGeneratedSql()).contains("where ? between t0.name and t0.smallnote");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_basic() throws Exception {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress");
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains(", t1.id c9");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_withProperty() throws Exception {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress (city)");
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains(", t1.id c9, t1.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_basic_multiple() throws Exception {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress fetch shippingAddress");
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains(", t1.city ");
|
||||
assertThat(query.getGeneratedSql()).contains(", t2.city ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_basic_multiple_withProperties() throws Exception {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress (city) fetch shippingAddress (city)");
|
||||
query.findList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains(", t1.id c8, t1.city c9, t2.id c10, t2.city c11");
|
||||
}
|
||||
|
||||
private Query<Customer> parse(String raw) {
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
grammar EQL;
|
||||
|
||||
select_statement
|
||||
: (select_clause)? (fetch_clause)? (where_clause)?
|
||||
: (select_clause)? (fetch_clause)* (where_clause)?
|
||||
;
|
||||
|
||||
select_clause
|
||||
@@ -17,7 +17,11 @@ where_clause
|
||||
;
|
||||
|
||||
fetch_path
|
||||
: 'fetch' PATH_VARIABLE '(' fetch_property_group ')'
|
||||
: 'fetch' PATH_VARIABLE fetch_property_set?
|
||||
;
|
||||
|
||||
fetch_property_set
|
||||
: '(' fetch_property_group ')'
|
||||
;
|
||||
|
||||
fetch_property_group
|
||||
|
||||
Reference in New Issue
Block a user