/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param <V> * @return */ public <V> V withConnection(StatementRunnableWithResult<V> runnable) { return withConnection(runnable, null); }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable */ public void withConnection(StatementRunnable runnable) { withConnection(runnable, null); }
/** * Invokes the run method on the {@link StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * * @param runnable */ public void withConnection(StatementRunnable runnable) { withConnection(runnable, null); }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable */ public void withConnection(StatementRunnable runnable) { withConnection(runnable, null); }
/** * Invokes the run method on the {@link StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * * @param runnable * @param <V> * @return */ public <V> V withConnection(StatementRunnableWithResult<V> runnable) { return withConnection(runnable, null); }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param <V> * @return */ public <V> V withConnection(StatementRunnableWithResult<V> runnable) { return withConnection(runnable, null); }
@Override @SuppressWarnings({"ConstantConditions", "unchecked"}) public Collection<Todo> findAll() { return sql2oHandler.withSql2o(sql2o -> (Collection<Todo>) sql2o.withConnection((connection, argument) -> { return connection.createQuery("SELECT * FROM todos").executeAndFetch(Todo.class); })); }
@Override public void clear() { sql2oHandler.withSql2o(sql2o -> { sql2o.withConnection((connection, argument) -> { connection.createQuery("DELETE FROM todos").executeUpdate(); }); return null; }); } }
@Override public Todo findById(Long id) { return sql2oHandler.withSql2o(sql2o -> sql2o.withConnection((connection, argument) -> { return connection.createQuery("SELECT * FROM todos WHERE id = :id") .addParameter("id", id) .executeAndFetchFirst(Todo.class); })); }
private void update(final Todo todo) { sql2oHandler.withSql2o(sql2o -> { sql2o.withConnection((connection, argument) -> { String sql = "UPDATE todos SET " + "done = :done " + "WHERE id = :id"; connection.createQuery(sql) .bind(todo) .executeUpdate(); }); return todo; }); }
private void insert(final Todo todo) { sql2oHandler.withSql2o(sql2o -> { sql2o.withConnection((connection, argument) -> { String sql = "INSERT INTO todos(description, done) VALUES (:description, false)"; Object key = connection.createQuery(sql) .bind(todo) .executeUpdate() .getKey(); todo.setId(((Number) key).longValue()); }); return todo; }); }
@Override public void delete(final Todo todo) { sql2oHandler.withSql2o(sql2o -> { sql2o.withConnection((connection, argument) -> { connection.createQuery("DELETE FROM todos WHERE id = :id") .addParameter("id", todo.getId()) .executeUpdate(); }); return todo; }); }