Search in sources :

Example 1 with AbstractJDOQLQuery

use of org.datanucleus.store.query.AbstractJDOQLQuery in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testImport.

/**
 * Test for the detection of "import" blocks.
 */
public void testImport() {
    // Test of the basic import parsing
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass import java.util.*; import java.net.*";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    parser.parse();
    String queryString = query.toString();
    // Parser will strip out whitespace from the imports
    String expectationString = "SELECT FROM org.jpox.samples.MyClass import java.util.*;import java.net.*";
    assertEquals("Parse of single-string basic import failed", expectationString, queryString);
}
Also used : AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.Query) JDOQLSingleStringParser(org.datanucleus.query.JDOQLSingleStringParser) PersistenceManager(javax.jdo.PersistenceManager)

Example 2 with AbstractJDOQLQuery

use of org.datanucleus.store.query.AbstractJDOQLQuery in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testLiteralWithKeyword.

/**
 * Test for the parse of a literal which includes a keyword.
 */
public void testLiteralWithKeyword() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass WHERE field1 == 'The book from which I took inspiration'";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
    } catch (NucleusUserException e) {
        LOG.error("Exception in test", e);
        fail("Exception in parse : " + e.getMessage());
    }
}
Also used : AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.Query) JDOQLSingleStringParser(org.datanucleus.query.JDOQLSingleStringParser) PersistenceManager(javax.jdo.PersistenceManager) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 3 with AbstractJDOQLQuery

use of org.datanucleus.store.query.AbstractJDOQLQuery in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testWhereWithMultipleSpaces.

/**
 * Test for the parse of a WHERE clause which has multiple spaces present.
 */
public void testWhereWithMultipleSpaces() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass WHERE field1 == 'The     value to compare against'";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
        // Should preserve spaces in the WHERE
        assertEquals("The WHERE clause was not correctly parsed", "field1 == 'The     value to compare against'", query.getFilter());
    } catch (NucleusUserException e) {
        fail("Exception in parse : " + e.getMessage());
    }
}
Also used : AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.Query) JDOQLSingleStringParser(org.datanucleus.query.JDOQLSingleStringParser) PersistenceManager(javax.jdo.PersistenceManager) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 4 with AbstractJDOQLQuery

use of org.datanucleus.store.query.AbstractJDOQLQuery in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testSubquery.

/**
 * Test for parse and compile of single-string JDOQL subquery case.
 */
public void testSubquery() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass WHERE field1 > " + "(SELECT avg(f.field1) FROM org.jpox.samples.MyClass f)";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
        SubqueryDefinition subq1 = query.getSubqueryForVariable("DN_SUBQUERY_1");
        assertNotNull("Subquery #1 should be generated by single-string parser but isnt", subq1);
        assertEquals(subq1.getQuery().toString(), "SELECT avg(f.field1) FROM org.jpox.samples.MyClass f");
        SubqueryDefinition subq2 = query.getSubqueryForVariable("DN_SUBQUERY_2");
        assertNull("Subquery #2 shouldnt be generated by single-string parser but is", subq2);
    } catch (NucleusUserException e) {
        fail("Exception in parser : " + e.getMessage());
    }
}
Also used : AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.Query) JDOQLSingleStringParser(org.datanucleus.query.JDOQLSingleStringParser) PersistenceManager(javax.jdo.PersistenceManager) SubqueryDefinition(org.datanucleus.store.query.Query.SubqueryDefinition) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 5 with AbstractJDOQLQuery

use of org.datanucleus.store.query.AbstractJDOQLQuery in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testVariableBeforeParameter.

/**
 * Test for the order of variable and parameter definitions.
 */
public void testVariableBeforeParameter() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass PARAMETERS String p1 VARIABLES int v1";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
        fail("Query using VARIABLES after PARAMETERS should have thrown an exception but passed");
    } catch (NucleusUserException e) {
    // Expected since VARIABLES should be before PARAMETERS
    }
}
Also used : AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.Query) JDOQLSingleStringParser(org.datanucleus.query.JDOQLSingleStringParser) PersistenceManager(javax.jdo.PersistenceManager) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)7 Query (javax.jdo.Query)7 JDOQuery (org.datanucleus.api.jdo.JDOQuery)7 JDOQLSingleStringParser (org.datanucleus.query.JDOQLSingleStringParser)7 AbstractJDOQLQuery (org.datanucleus.store.query.AbstractJDOQLQuery)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)6 SubqueryDefinition (org.datanucleus.store.query.Query.SubqueryDefinition)2