@Override public DatabaseConnection getReadOnlyConnection(String tableName) throws SQLException { DatabaseConnection conn = getSavedConnection(); if (conn == null) { return connectionSource.getReadOnlyConnection(tableName); } else { return conn; } }
@Override public boolean idExists(ID id) throws SQLException { DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.ifExists(connection, id); } finally { connectionSource.releaseConnection(connection); } }
@Override public Cursor loadInBackground() { Cursor cursor; try { DatabaseConnection connection = dao.getConnectionSource().getReadOnlyConnection(dao.getTableName()); AndroidCompiledStatement statement = (AndroidCompiledStatement) query.compile(connection, SELECT); cursor = statement.getCursor(); } catch (SQLException e) { throw new RuntimeException(e); } // fill the cursor with results cursor.getCount(); return cursor; }
@Override public Cursor loadInBackground() { Cursor cursor; try { DatabaseConnection connection = dao.getConnectionSource().getReadOnlyConnection(dao.getTableName()); AndroidCompiledStatement statement = (AndroidCompiledStatement) query.compile(connection, SELECT); cursor = statement.getCursor(); } catch (SQLException e) { throw new RuntimeException(e); } // fill the cursor with results cursor.getCount(); return cursor; }
@Override public boolean idExists(ID id) throws SQLException { DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.ifExists(connection, id); } finally { connectionSource.releaseConnection(connection); } }
@Override public T queryForId(ID id) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForId(connection, id, objectCache); } finally { connectionSource.releaseConnection(connection); } }
@Override public T queryForId(ID id) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForId(connection, id, objectCache); } finally { connectionSource.releaseConnection(connection); } }
@Override public T queryForFirst(PreparedQuery<T> preparedQuery) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForFirst(connection, preparedQuery, objectCache); } finally { connectionSource.releaseConnection(connection); } }
@Override public long countOf() throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForCountStar(connection); } finally { connectionSource.releaseConnection(connection); } }
@Override public long countOf(PreparedQuery<T> preparedQuery) throws SQLException { checkForInitialized(); if (preparedQuery.getType() != StatementType.SELECT_LONG) { throw new IllegalArgumentException("Prepared query is not of type " + StatementType.SELECT_LONG + ", you need to call QueryBuilder.setCountOf(true)"); } DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForLong(connection, preparedQuery); } finally { connectionSource.releaseConnection(connection); } }
@Override public T queryForFirst(PreparedQuery<T> preparedQuery) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForFirst(connection, preparedQuery, objectCache); } finally { connectionSource.releaseConnection(connection); } }
@Override public long countOf(PreparedQuery<T> preparedQuery) throws SQLException { checkForInitialized(); if (preparedQuery.getType() != StatementType.SELECT_LONG) { throw new IllegalArgumentException("Prepared query is not of type " + StatementType.SELECT_LONG + ", you need to call QueryBuilder.setCountOf(true)"); } DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForLong(connection, preparedQuery); } finally { connectionSource.releaseConnection(connection); } }
@Override public long countOf() throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForCountStar(connection); } finally { connectionSource.releaseConnection(connection); } }
@Override public boolean isTableExists() throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return connection.isTableExists(tableInfo.getTableName()); } finally { connectionSource.releaseConnection(connection); } }
@Test(expected = SQLException.class) public void testDeleteNoId() throws Exception { StatementExecutor<NoId, Void> se = new StatementExecutor<NoId, Void>(databaseType, new TableInfo<NoId, Void>(databaseType, NoId.class), null); NoId noId = new NoId(); noId.stuff = "1"; ConnectionSource connectionSource = createMock(ConnectionSource.class); expect(connectionSource.getReadOnlyConnection(NOID_TABLE_NAME)).andReturn(null); replay(connectionSource); se.delete(connectionSource.getReadOnlyConnection(NOID_TABLE_NAME), noId, null); }
@Override public long queryRawValue(String query, String... arguments) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForLong(connection, query, arguments); } catch (SQLException e) { throw SqlExceptionUtil.create("Could not perform raw value query for " + query, e); } finally { connectionSource.releaseConnection(connection); } }
@Override public DatabaseConnection getReadOnlyConnection(String tableName) throws SQLException { DatabaseConnection connection = cs.getReadOnlyConnection(tableName); connectionCount.incrementAndGet(); WrappedConnection wrapped = wrapConnection(connection); wrappedConnections.put(wrapped.getDatabaseConnectionProxy(), wrapped); // System.err.println("got wrapped " + wrapped.hashCode() + ", count = " + getReleaseCount); // new RuntimeException().printStackTrace(); return wrapped.getDatabaseConnectionProxy(); }
@Override public boolean isTableExists() throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return connection.isTableExists(tableInfo.getTableName()); } finally { connectionSource.releaseConnection(connection); } }
@Override public long queryRawValue(String query, String... arguments) throws SQLException { checkForInitialized(); DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName()); try { return statementExecutor.queryForLong(connection, query, arguments); } catch (SQLException e) { throw SqlExceptionUtil.create("Could not perform raw value query for " + query, e); } finally { connectionSource.releaseConnection(connection); } }
@Test public void testCreateDbTypeDataSource() throws Exception { ConnectionSource dataSource = null; try { String dbUrl = "jdbc:h2:mem:ormlitetest"; dataSource = new JdbcConnectionSource(dbUrl, new H2DatabaseType()); dataSource.close(); } finally { if (dataSource != null) { dataSource.getReadOnlyConnection(null).close(); } } } }