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