Search in sources :

Example 51 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class HintProviderTest method requestHint_fieldNameHint_simple_referencedEntity_collections.

@Test
public void requestHint_fieldNameHint_simple_referencedEntity_collections() 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();
    builder.startNewEntity("League");
    builder.addStringAttribute("name");
    builder.addCollectionReferenceAttribute("teams", "Team");
    JpqlEntityModel leagueEntity = builder.produce();
    DomainModel model = new DomainModel();
    model.add(teamEntity);
    model.add(playerEntity);
    model.add(leagueEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("SELECT l.teams.~ FROM League l");
    List<String> options = response.getOptions();
    assertEquals(0, options.size());
    assertEquals("Query error", response.getErrorMessage());
    assertEquals(1, response.getCauseErrorMessages().size());
    assertEquals("Cannot get attribute of collection [l.teams.]", response.getCauseErrorMessages().get(0));
    response = hintProvider.requestHint("SELECT l.name FROM League l where l.teams.~");
    options = response.getOptions();
    assertEquals(0, options.size());
    assertEquals("Query error", response.getErrorMessage());
    assertEquals(1, response.getCauseErrorMessages().size());
    assertEquals("Cannot get attribute of collection [l.teams.]", response.getCauseErrorMessages().get(0));
    response = hintProvider.requestHint("SELECT l.name FROM League l where exists (select 1 from Team t where t.name = l.teams.n~");
    options = response.getOptions();
    assertEquals(0, options.size());
    assertEquals("Query error", response.getErrorMessage());
    assertEquals(1, response.getCauseErrorMessages().size());
    assertEquals("Cannot get attribute of collection [l.teams.n]", response.getCauseErrorMessages().get(0));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 52 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class HintProviderTest method requestHint_entityNameHint_subquery.

// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void requestHint_fieldNameHint_subqueryEntity() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// 
// 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.~ from (select t from Team t) p");
// List<String> options = response.getOptions();
// assertEquals(2, options.size());
// assertEquals("name", options.get(0));
// assertEquals("owner", options.get(1));
// 
// response = hintProvider.requestHint("select p.~ from (select t.name from Team t) p");
// options = response.getOptions();
// assertEquals(1, options.size());
// assertEquals("name", options.get(0));
// 
// response = hintProvider.requestHint("select p.t~ from (select t.name from Team t) p");
// options = response.getOptions();
// assertEquals(0, options.size());
// 
// response = hintProvider.requestHint("select p.o~ from (select t.name, t.owner from Team t) p");
// options = response.getOptions();
// assertEquals(1, options.size());
// assertEquals("owner", options.get(0));
// 
// response = hintProvider.requestHint("select g.o~ from (select t.name, t.owner from Team t) p, (select t.name from Team t) g");
// options = response.getOptions();
// assertEquals(0, options.size());
// }
// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void requestHint_fieldNameHint_severalLevelsOfSubquery() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// 
// 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.~ from " +
// "(select t from Team t " +
// "where t.name in " +
// "   (select a.name from Player a)" +
// ") p");
// List<String> options = response.getOptions();
// assertEquals(2, options.size());
// assertEquals("name", options.get(0));
// assertEquals("owner", options.get(1));
// 
// response = hintProvider.requestHint("select p.owner from (select t.o~ from Team t where t.name in (select a.name from Player a)) p");
// options = response.getOptions();
// assertEquals(1, options.size());
// assertEquals("owner", options.get(0));
// 
// response = hintProvider.requestHint("select p.owner from (select t from Team t where t.name in (select a.~ from Player a)) p");
// options = response.getOptions();
// assertEquals(3, options.size());
// assertEquals("name", options.get(0));
// assertEquals("nickname", options.get(1));
// assertEquals("team", options.get(2));
// 
// response = hintProvider.requestHint("select p.owner from (select t from Team t where t.~ in (select a.name from Player a)) p");
// options = response.getOptions();
// assertEquals(2, options.size());
// assertEquals("name", options.get(0));
// assertEquals("owner", options.get(1));
// 
// response = hintProvider.requestHint("select p.owner from (select a.ni~ from Team t where t.name in (select a.name from Player a)) p");
// options = response.getOptions();
// assertEquals(0, options.size());
// assertEquals("Query error", response.getErrorMessage());
// assertEquals(1, response.getCauseErrorMessages().size());
// assertEquals("Cannot parse [a.ni]", response.getCauseErrorMessages().get(0));
// }
// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void requestHint_fieldNameHint_subqueries_using_AS() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// 
// 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.~ from (select t.owner from Team as t where t.name) p");
// List<String> options = response.getOptions();
// assertEquals(1, options.size());
// assertEquals("owner", options.get(0));
// }
@Test
public void requestHint_entityNameHint_subquery() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
    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 from (select t from T~) p");
    List<String> options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("Team", options.get(0));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 53 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class HintProviderTest method requestHint_fieldNameHint_join_throughSeveralFields.

@Test
public void requestHint_fieldNameHint_join_throughSeveralFields() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel personEntity = builder.produceImmediately("Person", "personName");
    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();
    builder.startNewEntity("League");
    builder.addStringAttribute("name");
    builder.addCollectionReferenceAttribute("teams", "Team");
    JpqlEntityModel leagueEntity = builder.produce();
    builder.startNewEntity("Country");
    builder.addStringAttribute("flag");
    builder.addReferenceAttribute("league", "League");
    JpqlEntityModel countryEntity = builder.produce();
    DomainModel model = new DomainModel(teamEntity, playerEntity, leagueEntity, personEntity, countryEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("select m.~ from Player p " + "left join p.team.manager as m");
    List<String> options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("personName", options.get(0));
    hintProvider = createTestHintProvider(model);
    response = hintProvider.requestHint("select c.flag from Country c " + "join c.league.teams as t " + "join t.~");
    options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("manager", options.get(0));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 54 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class HintProviderTest method requestHint_fieldNameHint_simple.

@Test
public void requestHint_fieldNameHint_simple() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel playerEntity = builder.produceImmediately("Player", "name", "nickname");
    DomainModel model = new DomainModel();
    model.add(playerEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("SELECT p.~ FROM Player p");
    List<String> options = response.getOptions();
    assertEquals(2, options.size());
    assertEquals("name", options.get(0));
    assertEquals("nickname", options.get(1));
    response = hintProvider.requestHint("SELECT p.ni~ FROM Player p");
    options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("nickname", options.get(0));
    response = hintProvider.requestHint("SELECT p.p~ FROM Player p");
    options = response.getOptions();
    assertEquals(0, options.size());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 55 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class HintProviderTest method requestHint_fieldNameHint_join_and_collection.

@Test
public void requestHint_fieldNameHint_join_and_collection() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel driver = builder.produceImmediately("Driver", "name", "signal");
    builder.startNewEntity("HomeBase");
    builder.addStringAttribute("city");
    JpqlEntityModel homeBase = builder.produce();
    builder.startNewEntity("Car");
    builder.addStringAttribute("model");
    builder.addCollectionReferenceAttribute("drivers", "Driver");
    builder.addReferenceAttribute("station", "HomeBase");
    JpqlEntityModel car = builder.produce();
    DomainModel model = new DomainModel(car, driver, homeBase);
    HintProvider hp = new HintProvider(model);
    HintResponse response = hp.requestHint("select d.name from Car c join c.station s, in(c.drivers) d where d.name = ?1 and s.~");
    List<String> options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("city", options.get(0));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Aggregations

JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)57 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)46 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)45 Test (org.junit.Test)40 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)17 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)16 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)9 ArrayList (java.util.ArrayList)5 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)4 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)4 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 UnknownEntityNameException (com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException)2 NoJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel)2 Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)2 List (java.util.List)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 JPA2RecognitionException (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)1 VirtualJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.VirtualJpqlEntityModel)1