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();
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations