Search in sources :

Example 1 with SQLiteException

use of org.sqlite.SQLiteException in project sqlite-jna by gwenn.

the class SQLXMLFromRows method getSource.

@Override
public <T extends Source> T getSource(Class<T> sourceClass) throws SQLException {
    checkAndSwitchReadable();
    if (sourceClass == null || DOMSource.class.equals(sourceClass)) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            // TODO builder.setErrorHandler();
            InputSource input = new InputSource(getReader());
            return (T) new DOMSource(builder.parse(input));
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new SQLiteException(null, "Unable to decode xml data.", ErrCodes.WRAPPER_SPECIFIC, e);
        }
    } else if (SAXSource.class.equals(sourceClass)) {
        InputSource is = new InputSource(getReader());
        return (T) new SAXSource(is);
    } else if (StreamSource.class.equals(sourceClass)) {
        return (T) new StreamSource(getReader());
    } else if (StAXSource.class.equals(sourceClass)) {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        try {
            XMLStreamReader xsr = xif.createXMLStreamReader(getReader());
            return (T) new StAXSource(xsr);
        } catch (XMLStreamException e) {
            throw new SQLiteException(null, "Unable to decode xml data.", ErrCodes.WRAPPER_SPECIFIC, e);
        }
    }
    throw new SQLiteException("Unknown XML Source class: " + sourceClass, ErrCodes.WRAPPER_SPECIFIC);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StAXSource(javax.xml.transform.stax.StAXSource) SQLiteException(org.sqlite.SQLiteException) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with SQLiteException

use of org.sqlite.SQLiteException in project sqlite-jna by gwenn.

the class SQLXMLImpl method transform.

private static void transform(Source source, StreamResult target) throws SQLException {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, target);
    } catch (TransformerException e) {
        throw new SQLiteException(null, "Unable to decode xml data.", ErrCodes.WRAPPER_SPECIFIC, e);
    }
}
Also used : SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) SQLiteException(org.sqlite.SQLiteException) TransformerException(javax.xml.transform.TransformerException)

Example 3 with SQLiteException

use of org.sqlite.SQLiteException in project sqlite-jna by gwenn.

the class SqliteStatementTest method testQueryTimeout.

@Ignore
@Test
public void testQueryTimeout() throws Exception {
    try (Statement stmt = conn.createStatement()) {
        try {
            stmt.setQueryTimeout(-1);
            fail("negative timeout value allowed?");
        } catch (SQLException e) {
        }
        ((Conn) conn).getConn().createScalarFunction("delay", 0, FunctionFlags.SQLITE_UTF8, new ScalarCallback() {

            @Override
            public void func(SQLite3Context pCtx, SQLite3Values args) {
                try {
                    Thread.currentThread().join(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                pCtx.setResultInt(0);
            }
        });
        stmt.setQueryTimeout(1);
        assertEquals(1, stmt.getQueryTimeout());
        long startTime = System.currentTimeMillis();
        try (ResultSet rs = stmt.executeQuery("SELECT *, delay() from test_table")) {
            rs.next();
            fail("Expected a timeout exception");
        } catch (SQLTimeoutException e) {
            long endTime = System.currentTimeMillis();
            if (endTime - startTime < 1000) {
                fail("Timeout expired early -- " + (endTime - startTime));
            }
        }
        try {
            stmt.execute("INSERT INTO test_table VALUES (2, delay())");
        } catch (SQLiteException e) {
            long endTime = System.currentTimeMillis();
            if (endTime - startTime < 1000) {
                fail("Timeout expired early -- " + (endTime - startTime));
            }
        }
    }
}
Also used : SQLite3Context(org.sqlite.SQLite.SQLite3Context) ScalarCallback(org.sqlite.ScalarCallback) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) SQLiteException(org.sqlite.SQLiteException) SQLite3Values(org.sqlite.SQLite.SQLite3Values) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with SQLiteException

use of org.sqlite.SQLiteException in project sqlite-jna by gwenn.

the class SQLXMLImpl method setResult.

@Override
public <T extends Result> T setResult(Class<T> resultClass) throws SQLException {
    checkAndSwitchWritable();
    if (resultClass == null || DOMResult.class.equals(resultClass)) {
        final DOMResult domResult = new DOMResult();
        src = new DOMSrc(domResult);
        return (T) domResult;
    } else if (SAXResult.class.equals(resultClass)) {
        try {
            SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
            TransformerHandler transformerHandler = transformerFactory.newTransformerHandler();
            transformerHandler.setResult(new StreamResult(createWriter()));
            return resultClass.cast(new SAXResult(transformerHandler));
        } catch (TransformerException e) {
            throw new SQLiteException(null, "Unable to create SAXResult.", ErrCodes.WRAPPER_SPECIFIC, e);
        }
    } else if (StreamResult.class.equals(resultClass)) {
        return resultClass.cast(new StreamResult(createWriter()));
    } else if (StAXResult.class.equals(resultClass)) {
        try {
            XMLOutputFactory xof = XMLOutputFactory.newInstance();
            XMLStreamWriter xsw = xof.createXMLStreamWriter(createWriter());
            return resultClass.cast(new StAXResult(xsw));
        } catch (XMLStreamException e) {
            throw new SQLiteException(null, "Unable to create StAXResult.", ErrCodes.WRAPPER_SPECIFIC, e);
        }
    }
    throw new SQLiteException("Unknown XML Result class: " + resultClass, ErrCodes.WRAPPER_SPECIFIC);
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) TransformerHandler(javax.xml.transform.sax.TransformerHandler) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) SQLiteException(org.sqlite.SQLiteException) SAXResult(javax.xml.transform.sax.SAXResult) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) TransformerException(javax.xml.transform.TransformerException)

Aggregations

SQLiteException (org.sqlite.SQLiteException)4 XMLStreamException (javax.xml.stream.XMLStreamException)2 TransformerException (javax.xml.transform.TransformerException)2 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)2 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 Statement (java.sql.Statement)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMResult (javax.xml.transform.dom.DOMResult)1 DOMSource (javax.xml.transform.dom.DOMSource)1