Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SqlQuery#select() API and SqlQuery#strteam(Class) allowed primiti…
…ve type.
- Loading branch information
1 parent
1742075
commit 0dac15e
Showing
5 changed files
with
468 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/main/java/jp/co/future/uroborosql/converter/SingleColumnResultSetConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* 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.converter; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.time.DayOfWeek; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.Month; | ||
import java.time.MonthDay; | ||
import java.time.OffsetDateTime; | ||
import java.time.OffsetTime; | ||
import java.time.Year; | ||
import java.time.YearMonth; | ||
import java.time.ZonedDateTime; | ||
import java.util.Date; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import jp.co.future.uroborosql.exception.UroborosqlRuntimeException; | ||
import jp.co.future.uroborosql.mapping.JavaType; | ||
import jp.co.future.uroborosql.mapping.mapper.PropertyMapperManager; | ||
import jp.co.future.uroborosql.utils.CaseFormat; | ||
import jp.co.future.uroborosql.utils.StringUtils; | ||
|
||
/** | ||
* 検索結果の1行をプリミティブ型(のラッパークラス)に変換する変換器 | ||
* | ||
* @param <E> プリミティブ型のラッパークラス、またはString型 | ||
* @author H.Sugimoto | ||
* | ||
*/ | ||
public class SingleColumnResultSetConverter<E> implements ResultSetConverter<E> { | ||
/** ロガー */ | ||
private static final Logger LOG = LoggerFactory.getLogger(SingleColumnResultSetConverter.class); | ||
|
||
private final String col; | ||
private final JavaType javaType; | ||
private final PropertyMapperManager mapperManager; | ||
private int columnPosition; | ||
|
||
/** | ||
* 受付可能な型かどうかを判定する. | ||
* | ||
* @param type 判定対象の型 | ||
* @return 受付可能な場合<code>true</code> を返す. | ||
*/ | ||
public static final boolean accept(Class<?> type) { | ||
if (type == null) { | ||
return false; | ||
} else if (String.class.equals(type) || | ||
boolean.class.equals(type) || | ||
Boolean.class.equals(type) || | ||
byte.class.equals(type) || | ||
Byte.class.equals(type) || | ||
short.class.equals(type) || | ||
Short.class.equals(type) || | ||
int.class.equals(type) || | ||
Integer.class.equals(type) || | ||
long.class.equals(type) || | ||
Long.class.equals(type) || | ||
float.class.equals(type) || | ||
Float.class.equals(type) || | ||
double.class.equals(type) || | ||
Double.class.equals(type) || | ||
BigInteger.class.equals(type) || | ||
BigDecimal.class.equals(type)) { | ||
// 基本的な型は受付可能とする | ||
return true; | ||
} else if (Date.class.isAssignableFrom(type)) { | ||
// Date型かDate型を継承するクラス(Timestampなど)は受付可能とする | ||
return true; | ||
} else if (LocalDateTime.class.equals(type) || | ||
OffsetDateTime.class.equals(type) || | ||
ZonedDateTime.class.equals(type) || | ||
LocalDate.class.equals(type) || | ||
LocalTime.class.equals(type) || | ||
OffsetTime.class.equals(type) || | ||
Year.class.equals(type) || | ||
YearMonth.class.equals(type) || | ||
MonthDay.class.equals(type) || | ||
Month.class.equals(type) || | ||
DayOfWeek.class.equals(type)) { | ||
// java.time配下の時間オブジェクトは受付可能とする | ||
return true; | ||
} else if (Object[].class.equals(type) || | ||
byte[].class.equals(type)) { | ||
// 配列型でコンポーネント型が受入対象の型である場合は受入可能とする | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* コンストラクタ | ||
* | ||
* @param col 取得対象のカラム名. null の場合は先頭カラムを取得 | ||
* @param columnType カラムの型 | ||
* @param mapperManager PropertyMapperManager | ||
*/ | ||
public SingleColumnResultSetConverter(String col, final Class<? extends E> columnType, | ||
final PropertyMapperManager mapperManager) { | ||
this.col = CaseFormat.UPPER_SNAKE_CASE.convert(col); | ||
this.javaType = JavaType.of(columnType); | ||
this.mapperManager = mapperManager; | ||
this.columnPosition = StringUtils.isEmpty(col) ? 1 : -1; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @see jp.co.future.uroborosql.converter.ResultSetConverter#createRecord(java.sql.ResultSet) | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public E createRecord(final ResultSet rs) throws SQLException { | ||
try { | ||
if (this.columnPosition == -1) { | ||
ResultSetMetaData rsmd = rs.getMetaData(); | ||
int columnCount = rsmd.getColumnCount(); | ||
|
||
// 指定されたカラムのpositionを取得 | ||
for (int i = 1; i <= columnCount; i++) { | ||
String columnLabel = CaseFormat.UPPER_SNAKE_CASE.convert(rsmd.getColumnLabel(i)); | ||
if (col.equalsIgnoreCase(columnLabel)) { | ||
this.columnPosition = i; | ||
break; | ||
} | ||
} | ||
if (this.columnPosition == -1) { | ||
// 指定されたカラムが見つからない場合は例外をスローする | ||
throw new UroborosqlRuntimeException(col + " not found in query result."); | ||
} | ||
} | ||
|
||
return (E) mapperManager.getValue(this.javaType, rs, this.columnPosition); | ||
} catch (SQLException | RuntimeException | Error e) { | ||
LOG.error("Error!!", e); | ||
throw e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.