DatabaseImpl implements Database { @Override public <T> List<T> select(String sql, Class<T> viewClass, Object... params) { validateAsterisk(sql); validateStringValue(sql); var watch = new StopWatch(); int returnedRows = 0; try { List<T> results = operation.select(sql, rowMapper(viewClass), params); returnedRows = results.size(); checkTooManyRowsReturned(returnedRows); return results; } finally { long elapsed = watch.elapsed(); int operations = ActionLogContext.track("db", elapsed, returnedRows, 0); logger.debug("select, sql={}, params={}, returnedRows={}, elapsed={}", sql, new SQLParams(operation.enumMapper, params), returnedRows, elapsed); checkOperation(elapsed, operations); } } DatabaseImpl(String name); void close(); void timeout(Duration timeout); void url(String url); void view(Class<T> viewClass); Repository<T> repository(Class<T> entityClass); @Override Transaction beginTransaction(); @Override List<T> select(String sql, Class<T> viewClass, Object... params); @Override Optional<T> selectOne(String sql, Class<T> viewClass, Object... params); @Override int execute(String sql, Object... params); @Override int[] batchExecute(String sql, List<Object[]> params); final Pool<Connection> pool; final DatabaseOperation operation; public String user; public String password; public int maxOperations; public int tooManyRowsReturnedThreshold; public long slowOperationThresholdInNanos; public IsolationLevel isolationLevel; }
@Test void selectWithView() { insertRow(1, "string1", TestEnum.V1); insertRow(2, "string2", TestEnum.V2); List<EntityView> views = database.select("SELECT string_field as string_label, enum_field as enum_label FROM database_test", EntityView.class); assertThat(views).hasSize(2); assertThat(views.get(0).enumField).isEqualTo(TestEnum.V1); assertThat(views.get(1).enumField).isEqualTo(TestEnum.V2); }
@Test void selectEmptyWithView() { List<EntityView> views = database.select("SELECT string_field as string_label, enum_field as enum_label FROM database_test where id = -1", EntityView.class); assertThat(views).isEmpty(); }