Search in sources :

Example 46 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class ExecuteFunctionTest method testEncodingInErrorMessage.

@Test
public void testEncodingInErrorMessage() throws SQLException, XPathException {
    // mocks a failing SQL query returning a single string and
    // checks the resulting error report
    XQueryContext context = new XQueryContextStub();
    ExecuteFunction execute = new ExecuteFunction(context, signatureByArity(ExecuteFunction.FS_EXECUTE, functionName, 3));
    final String query = "SELECT '<NAME>' FROM BLA";
    final String testMessage = "Some <&> error occurred!";
    // create mock objects
    Connection connection = mock(Connection.class);
    Statement stmt = mock(Statement.class);
    Object[] mocks = new Object[] { connection, stmt };
    // mock behavior
    expect(connection.createStatement()).andReturn(stmt);
    expect(stmt.execute(query)).andStubThrow(new SQLException(testMessage));
    stmt.close();
    replay(mocks);
    // register mocked connection
    final long connId = SQLModule.storeConnection(context, connection);
    // execute function
    Sequence res = execute.eval(new Sequence[] { new IntegerValue(connId), new StringValue(query), new BooleanValue(false) }, Sequence.EMPTY_SEQUENCE);
    // assert expectations
    verify(mocks);
    // <sql:exception><sql:state/><sql:message/><sql:stack-trace/><sql:sql/></sql:exception>
    assertEquals(1, res.getItemCount());
    assertEquals(Type.ELEMENT, res.getItemType());
    Node root = ((NodeValue) res.itemAt(0)).getNode();
    assertEquals("sql:exception", root.getNodeName());
    Node state = root.getFirstChild();
    assertEquals("sql:state", state.getNodeName());
    Node msg = state.getNextSibling();
    assertEquals("sql:message", msg.getNodeName());
    assertEquals(testMessage, msg.getTextContent());
    Node stacktrace = msg.getNextSibling();
    assertEquals("sql:stack-trace", stacktrace.getNodeName());
    Node sql = stacktrace.getNextSibling();
    assertEquals("sql:sql", sql.getNodeName());
    assertEquals(query, sql.getTextContent());
    Node xquery = sql.getNextSibling();
    assertEquals("sql:xquery", xquery.getNodeName());
}
Also used : Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) Test(org.junit.Test)

Example 47 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class ExecuteFunctionTest method testSQLException.

@Test
public void testSQLException() throws SQLException, XPathException {
    // mocks a simple SQL prepared statement with one parameter that fails on execution
    // and verifies the error message
    // the parameter is filled with an empty sql:param element
    XQueryContext context = new XQueryContextStub();
    ExecuteFunction execute = new ExecuteFunction(context, signatureByArity(ExecuteFunction.FS_EXECUTE, functionName, 3));
    final String sql = "SELECT ?";
    final String test_message = "SQL ERROR";
    final String test_sqlState = "SQL STATE";
    // create mock objects
    Connection connection = mock(Connection.class);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    Object[] mocks = new Object[] { connection, preparedStatement };
    // register mocked connection and prepared statement
    final long connId = SQLModule.storeConnection(context, connection);
    final long stmtId = SQLModule.storePreparedStatement(context, new PreparedStatementWithSQL(sql, preparedStatement));
    // mock behavior
    expect(preparedStatement.getConnection()).andReturn(connection);
    preparedStatement.setObject(1, "", Types.VARCHAR);
    expect(preparedStatement.execute()).andThrow(new SQLException(test_message, test_sqlState));
    replay(mocks);
    // execute function
    MemTreeBuilder paramBuilder = new MemTreeBuilder(context);
    paramBuilder.startDocument();
    paramBuilder.startElement(new QName("parameters", SQLModule.NAMESPACE_URI), null);
    paramBuilder.startElement(new QName("param", SQLModule.NAMESPACE_URI), null);
    paramBuilder.addAttribute(new QName("type", SQLModule.NAMESPACE_URI), "varchar");
    paramBuilder.endElement();
    paramBuilder.endElement();
    paramBuilder.endDocument();
    final ElementImpl sqlParams = (ElementImpl) paramBuilder.getDocument().getFirstChild();
    Sequence res = execute.eval(new Sequence[] { new IntegerValue(connId), new IntegerValue(stmtId), sqlParams, new BooleanValue(false) }, Sequence.EMPTY_SEQUENCE);
    // assert expectations
    verify(mocks);
    assertEquals(1, res.getItemCount());
    assertEquals(Type.ELEMENT, res.getItemType());
    Node root = ((NodeValue) res.itemAt(0)).getNode();
    assertEquals("sql:exception", root.getNodeName());
    assertEquals(6, root.getChildNodes().getLength());
    Node node = root.getFirstChild();
    Node state = node;
    assertEquals("sql:state", state.getNodeName());
    assertEquals(test_sqlState, state.getTextContent());
    node = node.getNextSibling();
    Node message = node;
    assertEquals("sql:message", message.getNodeName());
    assertEquals(test_message, message.getTextContent());
    node = node.getNextSibling();
    Node stackTrace = node;
    assertEquals("sql:stack-trace", stackTrace.getNodeName());
    node = node.getNextSibling();
    Node sqlErr = node;
    assertEquals("sql:sql", sqlErr.getNodeName());
    assertEquals(sql, sqlErr.getTextContent());
    node = node.getNextSibling();
    Node parameters = node;
    assertEquals("sql:parameters", parameters.getNodeName());
    Node param1 = parameters.getFirstChild();
    assertEquals("sql:param", param1.getNodeName());
    assertEquals("varchar", param1.getAttributes().getNamedItemNS(SQLModule.NAMESPACE_URI, "type").getTextContent());
    assertEquals("", param1.getTextContent());
    node = node.getNextSibling();
    Node xquery = node;
    assertEquals("sql:xquery", xquery.getNodeName());
}
Also used : QName(org.exist.dom.QName) Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) ElementImpl(org.exist.dom.memtree.ElementImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Test(org.junit.Test)

Example 48 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class Util method compileQuery.

static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
    CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    if (compiled == null) {
        compiled = xqueryService.compile(context, query);
    } else {
        compiled.getContext().updateContext(context);
        context.getWatchDog().reset();
    }
    return compiled;
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext)

Example 49 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class ConnectionPoolIT method getConnectionFromPoolIsAutomaticallyClosed.

@Test
public void getConnectionFromPoolIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException {
    // NOTE: pool-1 is configured in src/test/resources-filtered/conf.xml
    final String mainQuery = "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "sql:get-connection-from-pool(\"pool-1\")";
    final Source mainQuerySource = new StringSource(mainQuery);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.getBroker();
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final XQueryContext escapedMainQueryContext = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
            final XQueryContext mainQueryContext = mainCompiledQuery.getContext();
            // execute the query
            final Sequence result = executeQuery(broker, mainCompiledQuery);
            // check that the handle for the sql connection that was created was valid
            assertEquals(1, result.getItemCount());
            assertTrue(result.itemAt(0) instanceof IntegerValue);
            assertEquals(Type.LONG, result.itemAt(0).getType());
            final long connectionHandle = result.itemAt(0).toJavaObject(long.class);
            assertFalse(connectionHandle == 0);
            // intentionally escape the context from the lambda
            return mainQueryContext;
        });
        // check the connections map is empty
        final int connectionsCount = ModuleUtils.readContextMap(escapedMainQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
        assertEquals(0, connectionsCount);
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) IntegerValue(org.exist.xquery.value.IntegerValue) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) Map(java.util.Map) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 50 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class DebuggeeJointImpl method sessionClosed.

public synchronized void sessionClosed(boolean disconnect) {
    // disconnected already
    if (compiledXQuery == null)
        return;
    // disconnect debuggee & compiled source
    XQueryContext context = compiledXQuery.getContext();
    context.setDebuggeeJoint(null);
    compiledXQuery = null;
    if (command != null && disconnect)
        command.disconnect();
    reset();
    notifyAll();
}
Also used : XQueryContext(org.exist.xquery.XQueryContext)

Aggregations

XQueryContext (org.exist.xquery.XQueryContext)70 CompiledXQuery (org.exist.xquery.CompiledXQuery)37 Sequence (org.exist.xquery.value.Sequence)34 XQuery (org.exist.xquery.XQuery)33 DBBroker (org.exist.storage.DBBroker)24 Test (org.junit.Test)23 XPathException (org.exist.xquery.XPathException)18 BrokerPool (org.exist.storage.BrokerPool)17 IOException (java.io.IOException)11 Source (org.exist.source.Source)10 XQueryPool (org.exist.storage.XQueryPool)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 AnyURIValue (org.exist.xquery.value.AnyURIValue)8 URI (java.net.URI)7 StringValue (org.exist.xquery.value.StringValue)7 InputSource (org.xml.sax.InputSource)7 EXistException (org.exist.EXistException)6 QName (org.exist.dom.QName)6