Search in sources :

Example 1 with JPQLExpression

use of org.eclipse.persistence.jpa.jpql.parser.JPQLExpression in project eclipselink by eclipse-ee4j.

the class AbstractContentAssistVisitor method buildProposals.

/**
 * Prepares this visitor by prepopulating it with the necessary data that is required to properly
 * gather the list of proposals based on the given caret position.
 *
 * @param position The position of the cursor within the JPQL query
 * @param extension This extension can be used to provide additional support to JPQL content assist
 * that is outside the scope of providing proposals related to JPA metadata. It adds support for
 * providing suggestions related to class names, enum constants, table names, column names
 * @return The proposals that are valid choices for the given position
 */
public ContentAssistProposals buildProposals(int position, ContentAssistExtension extension) {
    try {
        JPQLExpression jpqlExpression = queryContext.getJPQLExpression();
        String jpqlQuery = queryContext.getJPQLQuery();
        // Calculate the position of the cursor within the parsed tree
        queryPosition = jpqlExpression.buildPosition(jpqlQuery, position);
        // Retrieve the word from the JPQL query (which is the text before the position of the cursor)
        wordParser = new WordParser(jpqlQuery);
        wordParser.setPosition(position);
        word = wordParser.partialWord();
        // Now visit the deepest leaf first and calculate the possible proposals
        proposals = new DefaultContentAssistProposals(queryContext.getGrammar(), extension);
        queryPosition.getExpression().accept(this);
        return proposals;
    } finally {
        dispose();
    }
}
Also used : JPQLExpression(org.eclipse.persistence.jpa.jpql.parser.JPQLExpression) WordParser(org.eclipse.persistence.jpa.jpql.WordParser)

Example 2 with JPQLExpression

use of org.eclipse.persistence.jpa.jpql.parser.JPQLExpression in project eclipselink by eclipse-ee4j.

the class ExpressionToolsTest method test_reposition_08.

@Test
public void test_reposition_08() {
    String jpqlQuery = "select o,\r\no,AVG(o.addressId) from Address o";
    JPQLExpression jpqlExpression = parse(jpqlQuery);
    // Test 1
    int position1 = "select o,\r\no,AVG(o.addressId) ".length();
    int position2 = position1;
    int[] positions = { position1, position2 };
    ExpressionTools.reposition(jpqlQuery, positions, jpqlExpression.toParsedText());
    int expectedPosition1 = "select o, o, AVG(o.addressId) ".length();
    int expectedPosition2 = expectedPosition1;
    int[] expectedPositions = { expectedPosition1, expectedPosition2 };
    assertArrayEquals(expectedPositions, positions);
    // Test 2 - The reverse
    positions = new int[] { expectedPosition1, expectedPosition2 };
    ExpressionTools.reposition(jpqlExpression.toParsedText(), positions, jpqlQuery);
    expectedPositions = new int[] { position1, position2 };
    assertArrayEquals(expectedPositions, positions);
}
Also used : JPQLExpression(org.eclipse.persistence.jpa.jpql.parser.JPQLExpression) Test(org.junit.Test)

Example 3 with JPQLExpression

use of org.eclipse.persistence.jpa.jpql.parser.JPQLExpression in project eclipselink by eclipse-ee4j.

the class ExpressionToolsTest method test_repositionCursor_01.

@Test
public void test_repositionCursor_01() {
    String jpqlQuery = "  SELECT  AVG ( mag )    FROM  Magazine   mag";
    JPQLExpression jpqlExpression = parse(jpqlQuery);
    int position = 0;
    int expectedPosition = adjustPosition(jpqlQuery, position);
    int newPosition = ExpressionTools.repositionCursor(jpqlQuery, position, jpqlExpression.toParsedText());
    assertEquals(expectedPosition, newPosition);
    position = 2;
    expectedPosition = 0;
    newPosition = ExpressionTools.repositionCursor(jpqlQuery, position, jpqlExpression.toParsedText());
    assertEquals(expectedPosition, newPosition);
    position = 4;
    expectedPosition = adjustPosition(jpqlQuery, position);
    newPosition = ExpressionTools.repositionCursor(jpqlQuery, position, jpqlExpression.toParsedText());
    assertEquals(expectedPosition, newPosition);
    position = 10;
    expectedPosition = adjustPosition(jpqlQuery, position);
    newPosition = ExpressionTools.repositionCursor(jpqlQuery, position, jpqlExpression.toParsedText());
    assertEquals(expectedPosition, newPosition);
    position = 31;
    expectedPosition = adjustPosition(jpqlQuery, position);
    newPosition = ExpressionTools.repositionCursor(jpqlQuery, position, jpqlExpression.toParsedText());
    assertEquals(expectedPosition, newPosition);
}
Also used : JPQLExpression(org.eclipse.persistence.jpa.jpql.parser.JPQLExpression) Test(org.junit.Test)

Example 4 with JPQLExpression

use of org.eclipse.persistence.jpa.jpql.parser.JPQLExpression in project eclipselink by eclipse-ee4j.

the class ExpressionToolsTest method test_reposition_09.

@Test
public void test_reposition_09() {
    String jpqlQuery = "select  \r\n o, AVG(o.addressId)  \r\n from Address o";
    JPQLExpression jpqlExpression = parse(jpqlQuery);
    // Test 1
    int position1 = "select  \r\n o, AVG(o.addressId)  \r\n ".length();
    int position2 = position1;
    int[] positions = { position1, position2 };
    ExpressionTools.reposition(jpqlQuery, positions, jpqlExpression.toParsedText());
    int expectedPosition1 = "select o, AVG(o.addressId) ".length();
    int expectedPosition2 = expectedPosition1;
    int[] expectedPositions = { expectedPosition1, expectedPosition2 };
    assertArrayEquals(expectedPositions, positions);
    // Test 2 - The reverse
    positions = new int[] { expectedPosition1, expectedPosition2 };
    ExpressionTools.reposition(jpqlExpression.toParsedText(), positions, jpqlQuery);
    expectedPositions = new int[] { position1, position2 };
    assertArrayEquals(expectedPositions, positions);
}
Also used : JPQLExpression(org.eclipse.persistence.jpa.jpql.parser.JPQLExpression) Test(org.junit.Test)

Example 5 with JPQLExpression

use of org.eclipse.persistence.jpa.jpql.parser.JPQLExpression in project eclipselink by eclipse-ee4j.

the class ExpressionToolsTest method test_reposition_02.

@Test
public void test_reposition_02() {
    String jpqlQuery = "SELECT AVG(mag) FROM Magazine mag WHERE mag.isbn+AVG()";
    JPQLExpression jpqlExpression = parse(jpqlQuery, buildQueryFormatter_1());
    // Test 1
    int position1 = "SELECT AVG(mag) FROM Magazine mag WHERE mag.isbn+AVG(".length();
    int position2 = position1;
    int[] positions = { position1, position2 };
    ExpressionTools.reposition(jpqlQuery, positions, jpqlExpression.toParsedText());
    int expectedPosition1 = "SELECT AVG(mag) FROM Magazine mag WHERE mag.isbn + AVG(".length();
    int expectedPosition2 = expectedPosition1;
    int[] expectedPositions = { expectedPosition1, expectedPosition2 };
    assertArrayEquals(expectedPositions, positions);
    // Test 2 - The reverse
    positions = new int[] { expectedPosition1, expectedPosition2 };
    ExpressionTools.reposition(jpqlExpression.toParsedText(), positions, jpqlQuery);
    expectedPositions = new int[] { position1, position2 };
    assertArrayEquals(expectedPositions, positions);
}
Also used : JPQLExpression(org.eclipse.persistence.jpa.jpql.parser.JPQLExpression) Test(org.junit.Test)

Aggregations

JPQLExpression (org.eclipse.persistence.jpa.jpql.parser.JPQLExpression)29 Test (org.junit.Test)21 CollectionExpression (org.eclipse.persistence.jpa.jpql.parser.CollectionExpression)7 Expression (org.eclipse.persistence.jpa.jpql.parser.Expression)7 LikeExpression (org.eclipse.persistence.jpa.jpql.parser.LikeExpression)7 ObjectExpression (org.eclipse.persistence.jpa.jpql.parser.ObjectExpression)7 StateFieldPathExpression (org.eclipse.persistence.jpa.jpql.parser.StateFieldPathExpression)7 UnknownExpression (org.eclipse.persistence.jpa.jpql.parser.UnknownExpression)7 SelectClause (org.eclipse.persistence.jpa.jpql.parser.SelectClause)4 LinkedList (java.util.LinkedList)2 JPQLException (org.eclipse.persistence.exceptions.JPQLException)2 EclipseLinkGrammarValidator (org.eclipse.persistence.jpa.jpql.EclipseLinkGrammarValidator)2 JPQLQueryProblem (org.eclipse.persistence.jpa.jpql.JPQLQueryProblem)2 IdentificationVariable (org.eclipse.persistence.jpa.jpql.parser.IdentificationVariable)2 SelectStatement (org.eclipse.persistence.jpa.jpql.parser.SelectStatement)2 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 WordParser (org.eclipse.persistence.jpa.jpql.WordParser)1 CountFunction (org.eclipse.persistence.jpa.jpql.parser.CountFunction)1 DefaultEclipseLinkJPQLGrammar (org.eclipse.persistence.jpa.jpql.parser.DefaultEclipseLinkJPQLGrammar)1