/** * 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); }
@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 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);
@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);
@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);
@Override public SpiffyConcurrent call() throws Exception { return SqlObjectBuilder.attach(handle, SpiffyConcurrent.class); } };
@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);
@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 testNullQueryReturn() { try { SqlObjectBuilder.attach(handle, SpiffyBoom.class); } catch (IllegalStateException e) { assertEquals("Method org.skife.jdbi.v2.sqlobject.TestVariousOddities$SpiffyBoom#returnNothing " + "is annotated as if it should return a value, but the method is void.", e.getMessage()); return; } fail(); }
@Test public void testWithHandle() throws Exception { WithGetHandle g = SqlObjectBuilder.attach(handle, WithGetHandle.class); String name = g.withHandle(new HandleCallback<String>() { @Override public String withHandle(Handle handle) throws Exception { handle.execute("insert into something (id, name) values (8, 'Mike')"); return handle.createQuery("select name from something where id = 8").mapTo(String.class).first(); } }); assertEquals("Mike", name); }
@Test public void testGetHandle() throws Exception { WithGetHandle g = SqlObjectBuilder.attach(handle, WithGetHandle.class); Handle h = g.getHandle(); assertSame(handle, h); }
@Test public void testFetchSizeOnMethodOnlyUsefulWhenSteppingThroughDebuggerSadly() throws Exception { Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(14, "Tom"); s.insert(15, "JFA"); s.insert(16, "Sunny"); List<Something> things = s.findAll(); assertEquals(3, things.size()); }
@Test public void testQueryTimeOutOnParamOnlyUsefulWhenSteppingThroughDebuggerSadly() throws Exception { Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(14, "Tom"); s.insert(15, "JFA"); s.insert(16, "Sunny"); List<Something> things = s.findAllWithQueryTimeOut(2); assertEquals(3, things.size()); }
@Test public void testQueryTimeOutOnMethodOnlyUsefulWhenSteppingThroughDebuggerSadly() throws Exception { Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(14, "Tom"); s.insert(15, "JFA"); s.insert(16, "Sunny"); List<Something> things = s.findAllWithQueryTimeOut(); assertEquals(3, things.size()); }
@Test @Category(JDBITests.class) public void testQueryTimeOutOnMethodOnlyUsefulWhenSteppingThroughDebuggerSadly() throws Exception { Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(14, "Tom"); s.insert(15, "JFA"); s.insert(16, "Sunny"); List<Something> things = s.findAllWithQueryTimeOut(); assertEquals(3, things.size()); }
@Test @Category(JDBITests.class) public void testFetchSizeAsArgOnlyUsefulWhenSteppingThroughDebuggerSadly() throws Exception { Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(14, "Tom"); s.insert(15, "JFA"); s.insert(16, "Sunny"); List<Something> things = s.findAll(1); assertEquals(3, things.size()); }
@Test @Category(JDBITests.class) public void testFetchSizeOnMethodOnlyUsefulWhenSteppingThroughDebuggerSadly() throws Exception { Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(14, "Tom"); s.insert(15, "JFA"); s.insert(16, "Sunny"); List<Something> things = s.findAll(); assertEquals(3, things.size()); }
@Test public void testAttach() throws Exception { Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(new Something(14, "Tom")); Something tom = s.byId(14); assertEquals("Tom", tom.getName()); }
@Test public void testRegistered() throws Exception { handle.registerMapper(new SomethingMapper()); Spiffy s = SqlObjectBuilder.attach(handle, Spiffy.class); s.insert(1, "Tatu"); Something t = s.byId(1); assertEquals(1, t.getId()); assertEquals("Tatu", t.getName()); }