@Override public <T> Object queryForOne(String statement, Object[] args, FieldType[] argfieldTypes, GenericRowMapper<T> rowMapper, ObjectCache objectCache) throws SQLException { if (proxy == null) { return null; } else { return proxy.queryForOne(statement, args, argfieldTypes, rowMapper, objectCache); } }
@Override public <T> Object queryForOne(String statement, Object[] args, FieldType[] argfieldTypes, GenericRowMapper<T> rowMapper, ObjectCache objectCache) throws SQLException { if (proxy == null) { return null; } else { return proxy.queryForOne(statement, args, argfieldTypes, rowMapper, objectCache); } }
@Test public void testQueryForOne() throws Exception { DatabaseConnection conn = createMock(DatabaseConnection.class); String statement = "insert bar"; Object result = new Object(); expect(conn.queryForOne(statement, null, null, null, null)).andReturn(result); conn.close(); DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn); replay(conn); assertEquals(result, proxy.queryForOne(statement, null, null, null, null)); proxy.close(); verify(conn); }
/** * Query for an object in the database which matches the id argument. */ public T execute(DatabaseConnection databaseConnection, ID id, ObjectCache objectCache) throws SQLException { if (objectCache != null) { T result = objectCache.get(clazz, id); if (result != null) { return result; } } Object[] args = new Object[] { convertIdToFieldObject(id) }; // @SuppressWarnings("unchecked") Object result = databaseConnection.queryForOne(statement, args, argFieldTypes, this, objectCache); if (result == null) { logger.debug("{} using '{}' and {} args, got no results", label, statement, args.length); } else if (result == DatabaseConnection.MORE_THAN_ONE) { logger.error("{} using '{}' and {} args, got >1 results", label, statement, args.length); logArgs(args); throw new SQLException(label + " got more than 1 result: " + statement); } else { logger.debug("{} using '{}' and {} args, got 1 result", label, statement, args.length); } logArgs(args); @SuppressWarnings("unchecked") T castResult = (T) result; return castResult; }
/** * Query for an object in the database which matches the id argument. */ public T execute(DatabaseConnection databaseConnection, ID id, ObjectCache objectCache) throws SQLException { if (objectCache != null) { T result = objectCache.get(clazz, id); if (result != null) { return result; } } Object[] args = new Object[] { convertIdToFieldObject(id) }; // @SuppressWarnings("unchecked") Object result = databaseConnection.queryForOne(statement, args, argFieldTypes, this, objectCache); if (result == null) { logger.debug("{} using '{}' and {} args, got no results", label, statement, args.length); } else if (result == DatabaseConnection.MORE_THAN_ONE) { logger.error("{} using '{}' and {} args, got >1 results", label, statement, args.length); logArgs(args); throw new SQLException(label + " got more than 1 result: " + statement); } else { logger.debug("{} using '{}' and {} args, got 1 result", label, statement, args.length); } logArgs(args); @SuppressWarnings("unchecked") T castResult = (T) result; return castResult; }
@SuppressWarnings("unchecked") @Test public void testForeignAutoRefresh() throws Exception { Field field = ForeignAutoRefresh.class.getDeclaredField("foreign"); ConnectionSource connectionSource = createMock(ConnectionSource.class); DatabaseConnection connection = createMock(DatabaseConnection.class); expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes(); expect(connectionSource.getReadOnlyConnection("ForeignAutoRefresh")).andReturn(connection); ForeignForeign foreignForeign = new ForeignForeign(); String stuff = "21312j3213"; int id = 4123123; foreignForeign.id = id; foreignForeign.stuff = stuff; expect(connection.queryForOne(isA(String.class), isA(Object[].class), isA(FieldType[].class), isA(GenericRowMapper.class), (ObjectCache) isNull())).andReturn(foreignForeign); connectionSource.releaseConnection(connection); DatabaseResults results = createMock(DatabaseResults.class); ForeignAutoRefresh foreign = new ForeignAutoRefresh(); replay(results, connectionSource, connection); FieldType fieldType = FieldType.createFieldType(databaseType, ForeignAutoRefresh.class.getSimpleName(), field, ForeignAutoRefresh.class); fieldType.configDaoInformation(connectionSource, ForeignAutoRefresh.class); assertNull(foreign.foreign); fieldType.assignField(connectionSource, foreign, id, false, null); assertNotNull(foreign.foreign); assertEquals(id, foreign.foreign.id); assertEquals(stuff, foreign.foreign.stuff); verify(results, connectionSource, connection); }