Skip to content

Commit

Permalink
Merge ce58f63 into 55d0b85
Browse files Browse the repository at this point in the history
  • Loading branch information
shout-star committed Sep 11, 2019
2 parents 55d0b85 + ce58f63 commit f21451d
Show file tree
Hide file tree
Showing 4 changed files with 475 additions and 77 deletions.
149 changes: 107 additions & 42 deletions src/main/java/jp/co/future/uroborosql/SqlQueryImpl.java
Expand Up @@ -18,6 +18,7 @@
import jp.co.future.uroborosql.converter.EntityResultSetConverter;
import jp.co.future.uroborosql.converter.MapResultSetConverter;
import jp.co.future.uroborosql.converter.ResultSetConverter;
import jp.co.future.uroborosql.exception.DataNonUniqueException;
import jp.co.future.uroborosql.exception.DataNotFoundException;
import jp.co.future.uroborosql.exception.UroborosqlSQLException;
import jp.co.future.uroborosql.fluent.SqlQuery;
Expand All @@ -43,123 +44,142 @@ final class SqlQueryImpl extends AbstractSqlFluent<SqlQuery> implements SqlQuery
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream()
* @see jp.co.future.uroborosql.fluent.SqlQuery#first()
*/
@Override
public Stream<Map<String, Object>> stream() {
return stream(agent().getDefaultMapKeyCaseFormat());
public Map<String, Object> first() {
return first(agent().getDefaultMapKeyCaseFormat());
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream(jp.co.future.uroborosql.converter.ResultSetConverter)
* @see jp.co.future.uroborosql.fluent.SqlQuery#first(jp.co.future.uroborosql.utils.CaseFormat)
*/
@Override
public <T> Stream<T> stream(final ResultSetConverter<T> converter) {
try {
return agent().query(context(), converter);
} catch (SQLException e) {
throw new UroborosqlSQLException(e);
}
public Map<String, Object> first(final CaseFormat caseFormat) {
return findFirst(caseFormat).orElseThrow(() -> new DataNotFoundException("query result is empty."));
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream(jp.co.future.uroborosql.utils.CaseFormat)
* @see jp.co.future.uroborosql.fluent.SqlQuery#first(Class)
*/
@Override
public Stream<Map<String, Object>> stream(final CaseFormat caseFormat) {
return stream(new MapResultSetConverter(agent.getSqlConfig().getDialect(), caseFormat));
public <T> T first(final Class<T> type) {
return findFirst(type).orElseThrow(() -> new DataNotFoundException("query result is empty."));
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream(java.lang.Class)
* @see jp.co.future.uroborosql.fluent.SqlQuery#findFirst()
*/
@Override
public <T> Stream<T> stream(final Class<T> type) {
return stream(new EntityResultSetConverter<>(type, new PropertyMapperManager()));
public Optional<Map<String, Object>> findFirst() {
return findFirst(agent().getDefaultMapKeyCaseFormat());
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#resultSet()
* @see jp.co.future.uroborosql.fluent.SqlQuery#findFirst(jp.co.future.uroborosql.utils.CaseFormat)
*/
@Override
public ResultSet resultSet() {
try {
return agent().query(context());
} catch (SQLException e) {
throw new UroborosqlSQLException(e);
public Optional<Map<String, Object>> findFirst(final CaseFormat caseFormat) {
try (Stream<Map<String, Object>> stream = stream(
new MapResultSetConverter(this.agent.getSqlConfig().getDialect(), caseFormat))) {
return stream.findFirst();
}
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#first()
* @see jp.co.future.uroborosql.fluent.SqlQuery#findFirst(java.lang.Class)
*/
@Override
public Map<String, Object> first() {
return first(agent().getDefaultMapKeyCaseFormat());
public <T> Optional<T> findFirst(final Class<T> type) {
try (Stream<T> stream = stream(new EntityResultSetConverter<>(type, new PropertyMapperManager()))) {
return stream.findFirst();
}
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#first(jp.co.future.uroborosql.utils.CaseFormat)
* @see jp.co.future.uroborosql.fluent.SqlQuery#one()
*/
@Override
public Map<String, Object> first(final CaseFormat caseFormat) {
return findFirst(caseFormat).orElseThrow(() -> new DataNotFoundException("query result is empty."));
public Map<String, Object> one() {
return one(agent().getDefaultMapKeyCaseFormat());
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#first(Class)
* @see jp.co.future.uroborosql.fluent.SqlQuery#one(CaseFormat)
*/
@Override
public <T> T first(final Class<T> type) {
return findFirst(type).orElseThrow(() -> new DataNotFoundException("query result is empty."));
public Map<String, Object> one(final CaseFormat caseFormat) {
return findOne(caseFormat).orElseThrow(() -> new DataNotFoundException("query result is empty."));
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#findFirst()
* @see jp.co.future.uroborosql.fluent.SqlQuery#one(Class)
*/
@Override
public Optional<Map<String, Object>> findFirst() {
return findFirst(agent().getDefaultMapKeyCaseFormat());
public <T> T one(final Class<T> type) {
return findOne(type).orElseThrow(() -> new DataNotFoundException("query result is empty."));
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#findFirst(jp.co.future.uroborosql.utils.CaseFormat)
* @see jp.co.future.uroborosql.fluent.SqlQuery#findOne()
*/
@Override
public Optional<Map<String, Object>> findFirst(final CaseFormat caseFormat) {
public Optional<Map<String, Object>> findOne() {
return findOne(agent().getDefaultMapKeyCaseFormat());
}

@Override
public Optional<Map<String, Object>> findOne(final CaseFormat caseFormat) {
try (Stream<Map<String, Object>> stream = stream(
new MapResultSetConverter(agent.getSqlConfig().getDialect(), caseFormat))) {
return stream.findFirst();
new MapResultSetConverter(this.agent.getSqlConfig().getDialect(), caseFormat))) {
List<Map<String, Object>> resultList = stream.limit(2).collect(Collectors.toList());
if (resultList.size() > 1) {
throw new DataNonUniqueException("two or more query results.");
}
return resultList.stream().findFirst();
}
}

@Override
public <T> Optional<T> findOne(final Class<T> type) {
try (Stream<T> stream = stream(new EntityResultSetConverter<>(type, new PropertyMapperManager()))) {
List<T> resultList = stream.limit(2).collect(Collectors.toList());
if (resultList.size() > 1) {
throw new DataNonUniqueException("two or more query results.");
}
return resultList.stream().findFirst();
}
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#findFirst(java.lang.Class)
* @see jp.co.future.uroborosql.fluent.SqlQuery#resultSet()
*/
@Override
public <T> Optional<T> findFirst(final Class<T> type) {
try (Stream<T> stream = stream(new EntityResultSetConverter<>(type, new PropertyMapperManager()))) {
return stream.findFirst();
public ResultSet resultSet() {
try {
return agent().query(context());
} catch (SQLException e) {
throw new UroborosqlSQLException(e);
}
}

Expand Down Expand Up @@ -198,4 +218,49 @@ public <T> List<T> collect(final Class<T> type) {
return stream.collect(Collectors.toList());
}
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream()
*/
@Override
public Stream<Map<String, Object>> stream() {
return stream(agent().getDefaultMapKeyCaseFormat());
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream(jp.co.future.uroborosql.converter.ResultSetConverter)
*/
@Override
public <T> Stream<T> stream(final ResultSetConverter<T> converter) {
try {
return agent().query(context(), converter);
} catch (SQLException e) {
throw new UroborosqlSQLException(e);
}
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream(jp.co.future.uroborosql.utils.CaseFormat)
*/
@Override
public Stream<Map<String, Object>> stream(final CaseFormat caseFormat) {
return stream(new MapResultSetConverter(this.agent.getSqlConfig().getDialect(), caseFormat));
}

/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlQuery#stream(java.lang.Class)
*/
@Override
public <T> Stream<T> stream(final Class<T> type) {
return stream(new EntityResultSetConverter<>(type, new PropertyMapperManager()));
}

}
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2017-present, Future Corporation
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package jp.co.future.uroborosql.exception;

/**
* 検索結果が2件以上で先頭行を取得しようとした場合にスローされる例外クラス
*
* @author hoshi-k
*/
public class DataNonUniqueException extends UroborosqlRuntimeException {

public DataNonUniqueException() {
super();
}

public DataNonUniqueException(final String message) {
super(message);
}

public DataNonUniqueException(final String message, final Throwable cause) {
super(message, cause);
}

public DataNonUniqueException(final Throwable cause) {
super(cause);
}

}

0 comments on commit f21451d

Please sign in to comment.