private QueryUpdate.Builder createCommitOrRollbackQuery(boolean commit) { String action; if (commit) action = "commit"; else action = "rollback"; QueryUpdate.Builder u = update(action); return u; }
@Override public void configure(final Env env, final Config config, final Binder binder) { Key<DataSource> dskey = Key.get(DataSource.class, Names.named(name)); DataSource ds = env.get(dskey) .orElseThrow(() -> new NoSuchElementException("DataSource missing: " + dskey)); Database db = Database.fromDataSource(ds); env.serviceKey().generate(Database.class, name, k -> binder.bind(k).toInstance(db)); // close on shutdown env.onStop(db::close); } }
/** * Returns a {@link Database} based on a jdbc connection string. * * @param url * jdbc connection url * @return */ public static Database from(String url) { return new Database(url, null, null); }
Observable<Post> findAll() { return this.db.select("select * from posts") .get( rs -> new Post(rs.getLong("id"), rs.getString("title"), rs.getString("content") ) ) .asObservable(); }
private class dbHelper { Database mDB; // set this up however you are doing it already private void addStudent(StudentRecord newRecord){ mDB.beginTransaction(); try { ContentValues StudentRecordToAdd = new ContentValues(); StudentRecordToAdd.put(Students.STUDENT_ID, newRecord.getStudentName()); StudentRecordToAdd.put(Student.STUDENT_TYPE, newRecord.getStudentType()); StudentRecordToAdd.put(Student.STUDENT_LOCATION, newRecord.getStudentLocation()); StudentRecordToAdd.put(Student.STUDENT_MOTHER, newRecord.getStudentMother()); mDB.insert(Student.STUDENT_TABLE_NAME,Student.STUDENT_ANIMALID, StudentRecordToAdd); mDB.setTransactionSuccessful(); Toast.makeText(this,"Recorded Added ",0).show(); } finally { mDB.endTransaction(); } } }
Database database = new Database(new SQL("host", "port","database", "username","password")); List<tStd_M_lt> bases = database.select(tStd_M_lt.class, new Where(tStd_M_lt.FIELDS.ITEM_ID.getDatabaseFieldName(), OPERATOR.LIKE, "?"), "%Base%"); for (tStd_M_lt base : bases) { List<tShower_Base_Kit> kits = database.selectByForiegnKey(tShower_Base_Kit.class, base); }
/** * Starts a transaction. Until commit() or rollback() is called on the * source this will set the query context for all created queries to be a * single threaded executor with one (new) connection. * * @return */ public Observable<Boolean> beginTransaction() { return beginTransaction(Observable.empty()); }
/** * Returns a Database based on the current Database except all * non-transactional queries run on the given scheduler. * * @return new Database instance */ public Database asynchronous(final Scheduler nonTransactionalScheduler) { return asynchronous(new Func0<Scheduler>() { @Override public Scheduler call() { return nonTransactionalScheduler; } }); }
void setupBatching() { db.batching(batchSize); }
void beginTransactionSubscribe() { db.beginTransactionSubscribe(); }
void beginTransactionObserve() { db.beginTransactionObserve(); }
Single<Post> findById(Long id) { return this.db.select("select * from posts where id=?") .parameter(id) .get( rs -> new Post(rs.getLong("id"), rs.getString("title"), rs.getString("content") ) ) .first() .toSingle(); }
@Override public Observable<T> call(T t) { return db.beginTransaction().map(Functions.constant(t)); } });
/** * Returns a Database based on the current Database except all * non-transactional queries run {@link Schedulers#io}. * * @return new Database instance */ public Database asynchronous() { return asynchronous(Schedulers.io()); }
@Override public Observable<Integer> call(Observable<Integer> dep, String command) { return update(command).dependsOn(dep).count(); } }).flatMap(Functions.<Observable<Integer>> identity());
@Override public ResultBuilder query(String sql) { return new Results(getDatabase().select(sql).get(resultSet -> new RowImpl(resultSet))); }
database.beginTransaction();
@Bean public Database db(DataSource ds){ return Database.fromDataSource(ds); } }
/** * Factory method. Uses the single connection provided and current thread * scheduler (trampoline) to run all queries. The connection will not be * closed in reality though the log may indicate it as having received a * close call. * * @param con * the connection */ public static Database from(Connection con) { return new Database(con); }
/** * Starts a transaction. Until commit() or rollback() is called on the * source this will set the query context for all created queries to be a * single threaded executor with one (new) connection. * * @param dependency * @return */ public Observable<Boolean> beginTransaction(Observable<?> dependency) { return update("begin").dependsOn(dependency).count().map(Functions.constant(true)); }