Search in sources :

Example 1 with SQLTemplateParser

use of org.apache.cayenne.template.parser.SQLTemplateParser in project cayenne by apache.

the class CayenneSQLTemplateProcessor method process.

protected SQLStatement process(String template, Context context) {
    Node node = templateCache.get(template);
    if (node == null) {
        SQLTemplateParser parser = parserPool.get();
        try {
            parser.ReInit(new ByteArrayInputStream(template.getBytes()));
            node = parser.template();
        } catch (ParseException | TokenMgrError ex) {
            throw new CayenneRuntimeException("Error parsing template '%s' : %s", template, ex.getMessage());
        } finally {
            parserPool.put(parser);
        }
        // can ignore case when someone resolved this template concurrently, it has no side effects
        templateCache.put(template, node);
    }
    node.evaluate(context);
    return new SQLStatement(context.buildTemplate(), context.getColumnDescriptors(), context.getParameterBindings());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.apache.cayenne.template.parser.Node) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) SQLTemplateParser(org.apache.cayenne.template.parser.SQLTemplateParser) TokenMgrError(org.apache.cayenne.template.parser.TokenMgrError) ParseException(org.apache.cayenne.template.parser.ParseException) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement)

Example 2 with SQLTemplateParser

use of org.apache.cayenne.template.parser.SQLTemplateParser in project cayenne by apache.

the class TemplateParserPoolTest method createNewParser.

@Test
public void createNewParser() throws Exception {
    SQLTemplateParser parser = parserPool.createNewParser();
    assertNotNull(parser);
}
Also used : SQLTemplateParser(org.apache.cayenne.template.parser.SQLTemplateParser) Test(org.junit.Test)

Example 3 with SQLTemplateParser

use of org.apache.cayenne.template.parser.SQLTemplateParser in project cayenne by apache.

the class TemplateParserPoolTest method put.

@Test
public void put() throws Exception {
    SQLTemplateParser parser = new SQLTemplateParser(new ByteArrayInputStream("".getBytes()));
    parserPool.put(parser);
    for (int i = 0; i < TemplateParserPool.INITIAL_POOL_SIZE; i++) {
        SQLTemplateParser parser1 = parserPool.get();
        assertNotNull(parser1);
        assertNotSame(parser, parser1);
    }
    SQLTemplateParser parser1 = parserPool.get();
    assertSame(parser, parser1);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SQLTemplateParser(org.apache.cayenne.template.parser.SQLTemplateParser) Test(org.junit.Test)

Example 4 with SQLTemplateParser

use of org.apache.cayenne.template.parser.SQLTemplateParser in project cayenne by apache.

the class TemplateParserPoolTest method get.

@Test
public void get() throws Exception {
    for (int i = 0; i < TemplateParserPool.MAX_POOL_SIZE + 10; i++) {
        SQLTemplateParser parser = parserPool.get();
        assertNotNull(parser);
    }
}
Also used : SQLTemplateParser(org.apache.cayenne.template.parser.SQLTemplateParser) Test(org.junit.Test)

Aggregations

SQLTemplateParser (org.apache.cayenne.template.parser.SQLTemplateParser)4 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)1 Node (org.apache.cayenne.template.parser.Node)1 ParseException (org.apache.cayenne.template.parser.ParseException)1 TokenMgrError (org.apache.cayenne.template.parser.TokenMgrError)1