/** * Get an instance of a specified EntitySqlDao class, sharing the same database session as the * initial sql dao class with which this wrapper factory was created. * * @param newSqlDaoClass the class to instantiate * @param <NewSqlDao> EntitySqlDao type to create * @return instance of NewSqlDao */ public <NewSqlDao extends EntitySqlDao<NewEntityModelDao, NewEntity>, NewEntityModelDao extends EntityModelDao<NewEntity>, NewEntity extends Entity> NewSqlDao become(final Class<NewSqlDao> newSqlDaoClass) { final NewSqlDao newSqlDao = SqlObjectBuilder.attach(handle, newSqlDaoClass); return create(newSqlDaoClass, newSqlDao); }
/** * Create a new sql object which will obtain and release connections from this dbi instance, as it needs to, * and can, respectively. You should not explicitely close this sql object * * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ public <SqlObjectType> SqlObjectType onDemand(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.onDemand(this, sqlObjectType); }
/** * Open a handle and attach a new sql object of the specified type to that handle. Be sure to close the * sql object (via a close() method, or calling {@link IDBI#close(Object)} * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ @Override public <SqlObjectType> SqlObjectType open(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.open(this, sqlObjectType); }
@Test public void testTransactionIsolationActuallyHappens() throws Exception { TransactionStuff txl = SqlObjectBuilder.attach(handle, TransactionStuff.class); TransactionStuff tx2 = SqlObjectBuilder.open(dbi, TransactionStuff.class); txl.insert(8, "Mike"); txl.begin(); txl.updateName(8, "Miker"); assertEquals("Miker", txl.byId(8).getName()); assertEquals("Mike", tx2.byId(8).getName()); txl.commit(); assertEquals("Miker", tx2.byId(8).getName()); tx2.close(); }
/** * Used to close a sql object which lacks a close() method. * @param sqlObject the sql object to close */ public void close(Object sqlObject) { if (sqlObject instanceof Handle) { // just because someone is *sure* to do it Handle h = (Handle) sqlObject; h.close(); } else { SqlObjectBuilder.close(sqlObject); } }
/** * Create a new sql object which will obtain and release connections from this dbi instance, as it needs to, * and can, respectively. You should not explicitely close this sql object * * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ @Override public <SqlObjectType> SqlObjectType onDemand(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.onDemand(this, sqlObjectType); }
/** * Open a handle and attach a new sql object of the specified type to that handle. Be sure to close the * sql object (via a close() method, or calling {@link IDBI#close(Object)} * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ public <SqlObjectType> SqlObjectType open(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.open(this, sqlObjectType); }
@Test public void testTransactionIsolationActuallyHappens() throws Exception { TransactionStuff txl = SqlObjectBuilder.attach(handle, TransactionStuff.class); TransactionStuff tx2 = SqlObjectBuilder.open(dbi, TransactionStuff.class); txl.insert(8, "Mike"); txl.begin(); txl.updateName(8, "Miker"); assertEquals("Miker", txl.byId(8).getName()); assertEquals("Mike", tx2.byId(8).getName()); txl.commit(); assertEquals("Miker", tx2.byId(8).getName()); tx2.close(); }
/** * Used to close a sql object which lacks a close() method. * @param sqlObject the sql object to close */ @Override public void close(Object sqlObject) { if (sqlObject instanceof Handle) { // just because someone is *sure* to do it Handle h = (Handle) sqlObject; h.close(); } else { SqlObjectBuilder.close(sqlObject); } }
@Override public Long doRetrieve(final ObjectType objectType) { final NonEntitySqlDao inTransactionNonEntitySqlDao = handle == null ? dbRouter.onDemand(true) : SqlObjectBuilder.attach(handle, NonEntitySqlDao.class); return inTransactionNonEntitySqlDao.getRecordIdFromObject(objectId.toString(), tableName.getTableName()); } }, objectId.toString(), objectType, tableName, cache);
@Override public <SqlObjectType> SqlObjectType onDemand(final Class<SqlObjectType> sqlObjectType) { if (delegate == null) { throw new IllegalStateException("No delegate has been set!"); } return wrapping ? SqlObjectBuilder.onDemand(this, sqlObjectType) : delegate.onDemand(sqlObjectType); }
@Override public <SqlObjectType> SqlObjectType open(final Class<SqlObjectType> sqlObjectType) { if (delegate == null) { throw new IllegalStateException("No delegate has been set!"); } return wrapping ? SqlObjectBuilder.open(this, sqlObjectType) : delegate.open(sqlObjectType); }
@Override public UUID doRetrieve(final ObjectType objectType) { final NonEntitySqlDao inTransactionNonEntitySqlDao = handle == null ? dbRouter.onDemand(true) : SqlObjectBuilder.attach(handle, NonEntitySqlDao.class); return inTransactionNonEntitySqlDao.getIdFromObject(recordId, tableName.getTableName()); } }, String.valueOf(recordId), objectType, tableName, cache);
@Test public void testSqlFromExternalFileWorks() throws Exception { Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); ExternalSql external = SqlObjectBuilder.onDemand(dbi, ExternalSql.class); spiffy.insert(1, "Tom"); spiffy.insert(2, "Sam"); Iterator<Something> all = external.findAll(); all.next(); all.next(); assertFalse(all.hasNext()); }
@Test public void testIterator() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); handle.execute("insert into something (id, name) values (3, 'Diego')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Iterator<Something> itty = spiffy.findByIdRange(2, 10); Set<Something> all = new HashSet<Something>(); while (itty.hasNext()) { all.add(itty.next()); } assertEquals(2, all.size()); assertTrue(all.contains(new Something(7, "Tim"))); assertTrue(all.contains(new Something(3, "Diego"))); }
@Override public Long doRetrieve(final ObjectType objectType) { final NonEntitySqlDao inTransactionNonEntitySqlDao = handle == null ? dbRouter.onDemand(true) : SqlObjectBuilder.attach(handle, NonEntitySqlDao.class); switch (tableName) { case TENANT: // Explicit cast to Long to avoid NPE (unboxing to long) return objectId == null ? (Long) 0L : inTransactionNonEntitySqlDao.getTenantRecordIdFromTenant(objectIdOrNull); default: return inTransactionNonEntitySqlDao.getTenantRecordIdFromObjectOtherThanTenant(objectIdOrNull, tableName.getTableName()); } } }, objectIdOrNull, objectType, tableName, cache);
@Test public void testSqlFromExternalFileWorks() throws Exception { Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); ExternalSql external = SqlObjectBuilder.onDemand(dbi, ExternalSql.class); spiffy.insert(1, "Tom"); spiffy.insert(2, "Sam"); Iterator<Something> all = external.findAll(); all.next(); all.next(); assertFalse(all.hasNext()); }
@Test public void testIterator() throws Exception { handle.execute("insert into something (id, name) values (7, 'Tim')"); handle.execute("insert into something (id, name) values (3, 'Diego')"); Spiffy spiffy = SqlObjectBuilder.open(dbi, Spiffy.class); Iterator<Something> itty = spiffy.findByIdRange(2, 10); Set<Something> all = new HashSet<Something>(); while (itty.hasNext()) { all.add(itty.next()); } assertEquals(2, all.size()); assertTrue(all.contains(new Something(7, "Tim"))); assertTrue(all.contains(new Something(3, "Diego"))); }
@Override public Long doRetrieve(final ObjectType objectType) { final NonEntitySqlDao inTransactionNonEntitySqlDao = handle == null ? dbRouter.onDemand(true) : SqlObjectBuilder.attach(handle, NonEntitySqlDao.class); switch (tableName) { case TENANT: case TAG_DEFINITIONS: case TAG_DEFINITION_HISTORY: return null; case ACCOUNT: return inTransactionNonEntitySqlDao.getAccountRecordIdFromAccount(objectIdOrNull); default: return inTransactionNonEntitySqlDao.getAccountRecordIdFromObjectOtherThanAccount(objectIdOrNull, tableName.getTableName()); } } }, objectIdOrNull, objectType, tableName, cache);
@Test(expected=TransactionException.class) public void testExceptionOnClose() throws Exception { DBI dbi = new DBI(ds) { @Override public Handle open() { Handle h = EasyMock.createMock(Handle.class); h.createStatement(EasyMock.anyString()); EasyMock.expectLastCall() .andThrow(new TransactionException("connection reset")); h.close(); EasyMock.expectLastCall() .andThrow(new UnableToCloseResourceException("already closed", null)); EasyMock.replay(h); return h; } }; Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); s.insert(1, "Tom"); }