use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class HintProvider method hintFieldName.
private HintResponse hintFieldName(String lastWord, String input, int caretPosition, Set<InferredType> expectedTypes) throws RecognitionException {
QueryTreeAnalyzer queryAnalyzer = new QueryTreeAnalyzer();
try {
queryAnalyzer.prepare(model, input, false);
} catch (RecognitionException | JPA2RecognitionException e) {
List<String> errorMessages = new ArrayList<>();
errorMessages.add(e.getMessage());
return new HintResponse("Query error", errorMessages);
}
List<ErrorRec> errorRecs = queryAnalyzer.getInvalidIdVarNodes();
QueryVariableContext root = queryAnalyzer.getRootQueryVariableContext();
if (root == null) {
List<String> errorMessages = prepareErrorMessages(errorRecs);
errorMessages.add(0, "Query variable context is null");
return new HintResponse("Query error", errorMessages);
}
QueryVariableContext queryVC = root.getContextByCaretPosition(caretPosition);
EntityPath path = EntityPath.parseEntityPath(lastWord);
Pointer pointer = path.resolvePointer(model, queryVC);
if (pointer instanceof NoPointer) {
List<String> errorMessages = prepareErrorMessages(errorRecs);
errorMessages.add(0, "Cannot parse [" + lastWord + "]");
return new HintResponse("Query error", errorMessages);
}
if (pointer instanceof CollectionPointer) {
List<String> errorMessages = prepareErrorMessages(errorRecs);
errorMessages.add(0, "Cannot get attribute of collection [" + lastWord + "]");
return new HintResponse("Query error", errorMessages);
}
if (!(pointer instanceof EntityPointer)) {
List<String> errorMessages = prepareErrorMessages(errorRecs);
return new HintResponse("Query error", errorMessages);
}
List<Option> options = new ArrayList<>();
JpqlEntityModel targetEntity = ((EntityPointer) pointer).getEntity();
if (targetEntity instanceof NoJpqlEntityModel)
return new HintResponse(options, path.lastEntityFieldPattern);
List<Attribute> attributes = targetEntity.findAttributesStartingWith(path.lastEntityFieldPattern, expectedTypes);
for (Attribute attribute : attributes) {
options.add(new Option(attribute.getName(), attribute.getUserFriendlyName()));
}
return new HintResponse(options, path.lastEntityFieldPattern);
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class HintProvider method hintEntityName.
private HintResponse hintEntityName(String lastWord) {
List<JpqlEntityModel> matchingEntities = model.findEntitiesStartingWith(lastWord);
List<Option> options = new ArrayList<>();
for (JpqlEntityModel entity : matchingEntities) {
options.add(new Option(entity.getName(), entity.getUserFriendlyName()));
}
return new HintResponse(options, lastWord);
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class HintProviderTest method requestHint_fieldNameHint_join.
// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void requestHint_fieldNameHint_wherein_subquerywhere() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// JpqlEntityModel playerEntity = builder.produceImmediately("Player", "nickname", "name");
// DomainModel model = new DomainModel();
// model.add(playerEntity);
// model.add(teamEntity);
//
// HintProvider hintProvider = createTestHintProvider(model);
// HintResponse response = hintProvider.requestHint("select p.name from Player p where p.team in (select a from Team a where a.~)");
// List<String> options = response.getOptions();
// assertEquals(2, options.size());
// assertEquals("name", options.get(0));
// assertEquals("owner", options.get(1));
//
// hintProvider = createTestHintProvider(model);
// response = hintProvider.requestHint("select p.name from Player p where p.team in (select a from Team a where a.name = p.n~)");
// options = response.getOptions();
// assertEquals(2, options.size());
// assertEquals("name", options.get(0));
// assertEquals("nickname", options.get(1));
// }
@Test
public void requestHint_fieldNameHint_join() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
JpqlEntityModel personEntity = builder.produceImmediately("Person", "name");
builder.startNewEntity("Team");
builder.addStringAttribute("name");
builder.addStringAttribute("owner");
builder.addReferenceAttribute("manager", "Person");
JpqlEntityModel teamEntity = builder.produce();
builder.startNewEntity("Player");
builder.addStringAttribute("name");
builder.addStringAttribute("nickname");
builder.addReferenceAttribute("team", "Team");
builder.addReferenceAttribute("agent", "Person");
JpqlEntityModel playerEntity = builder.produce();
DomainModel model = new DomainModel();
model.add(teamEntity);
model.add(playerEntity);
model.add(personEntity);
HintProvider hintProvider = createTestHintProvider(model);
HintResponse response = hintProvider.requestHint("select p.name, t.o~ from Player p join p.team as t");
List<String> options = response.getOptions();
assertEquals(1, options.size());
assertEquals("owner", options.get(0));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select p from Player p join p.team as t where t.~");
options = response.getOptions();
assertEquals(3, options.size());
assertEquals("manager", options.get(0));
assertEquals("name", options.get(1));
assertEquals("owner", options.get(2));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select p from Player p join p.~ ");
options = response.getOptions();
assertEquals(2, options.size());
assertEquals("agent", options.get(0));
assertEquals("team", options.get(1));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select p from Player p join p.team as t where t.~");
options = response.getOptions();
assertEquals(3, options.size());
assertEquals("manager", options.get(0));
assertEquals("name", options.get(1));
assertEquals("owner", options.get(2));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select m.~ from Player p " + "join p.team as t " + "join t.manager as m");
options = response.getOptions();
assertEquals(1, options.size());
assertEquals("name", options.get(0));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select t from Player p " + "join p.team as t " + "join t.~");
options = response.getOptions();
assertEquals(1, options.size());
assertEquals("manager", options.get(0));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select t from Player p join Team t on t.~ = p.agent.id");
options = response.getOptions();
assertEquals(3, options.size());
assertEquals("manager", options.get(0));
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class HintProviderTest method requestHint_fieldNameHint_simple_referencedEntity.
@Test
public void requestHint_fieldNameHint_simple_referencedEntity() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
builder.startNewEntity("Player");
builder.addStringAttribute("name");
builder.addStringAttribute("nickname");
builder.addReferenceAttribute("team", "Team");
JpqlEntityModel playerEntity = builder.produce();
DomainModel model = new DomainModel();
model.add(teamEntity);
model.add(playerEntity);
HintProvider hintProvider = createTestHintProvider(model);
HintResponse response = hintProvider.requestHint("SELECT p.te~ FROM Player p");
List<String> options = response.getOptions();
assertEquals(1, options.size());
assertEquals("team", options.get(0));
response = hintProvider.requestHint("SELECT p.team.~ FROM Player p");
options = response.getOptions();
assertEquals(1, options.size());
assertEquals("name", options.get(0));
response = hintProvider.requestHint("SELECT p.team.ni~ FROM Player p");
options = response.getOptions();
assertEquals(0, options.size());
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class HintProviderTest method requestHint_fieldNameHint_simple_wherePart.
@Test
public void requestHint_fieldNameHint_simple_wherePart() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
DomainModel model = new DomainModel();
model.add(teamEntity);
HintProvider hintProvider = createTestHintProvider(model);
HintResponse response = hintProvider.requestHint("select t from Team t where t.~");
List<String> options = response.getOptions();
assertEquals(2, options.size());
assertEquals("name", options.get(0));
assertEquals("owner", options.get(1));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select t.~ from Team t where t.name = 'KS'");
options = response.getOptions();
assertEquals(2, options.size());
assertEquals("name", options.get(0));
assertEquals("owner", options.get(1));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select t.name from Team t where t.~");
options = response.getOptions();
assertEquals(2, options.size());
assertEquals("name", options.get(0));
assertEquals("owner", options.get(1));
hintProvider = createTestHintProvider(model);
response = hintProvider.requestHint("select t.~ from Team t where t.name in ('KS', 'Zenit')");
options = response.getOptions();
assertEquals(2, options.size());
assertEquals("name", options.get(0));
assertEquals("owner", options.get(1));
}
Aggregations