svnno****@sourc*****
svnno****@sourc*****
2009年 4月 3日 (金) 01:15:24 JST
Revision: 3063 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3063 Author: daisuke_m Date: 2009-04-03 01:15:24 +0900 (Fri, 03 Apr 2009) Log Message: ----------- DatabaseImporterの内部ロジックでエラってたので、ResultSetIteratorまわりを修正。 Modified Paths: -------------- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/ForEachUtil.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/ColumnMeta.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/KeyMeta.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TableMeta.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TypeSafeDatabaseMetaData.java -------------- next part -------------- Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/ForEachUtil.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/ForEachUtil.java 2009-04-02 12:09:20 UTC (rev 3062) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/ForEachUtil.java 2009-04-02 16:15:24 UTC (rev 3063) @@ -21,6 +21,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; +import java.util.Iterator; import java.util.Map; import org.apache.commons.lang.Validate; @@ -41,15 +42,16 @@ * @param target 処理対象コレクション * @param visitor ビジター * @return accept結果 - * @throws X * @throws IllegalArgumentException 引数に{@code null}を与えた場合 */ public static <T, R, X extends Exception>R accept(Iterable<T> target, CollectionVisitor<T, R, X> visitor) throws X { Validate.notNull(target); Validate.notNull(visitor); - for (T connection : target) { - R result = visitor.visit(connection); + Iterator<T> iterator = target.iterator(); + while (iterator.hasNext()) { + T element = iterator.next(); + R result = visitor.visit(element); if (result != null) { return result; } @@ -67,7 +69,6 @@ * @param targetMap 処理対象コレクション * @param visitor ビジター * @return accept結果 - * @throws X * @throws IllegalArgumentException 引数に{@code null}を与えた場合 */ public static <K, V, R, X extends Exception>R accept(Map<K, V> targetMap, MapVisitor<K, V, R, X> visitor) throws X { @@ -92,7 +93,7 @@ * @param visitor ビジター * @return accept結果 * @throws SQLException SQLの実行に失敗した場合 - * @throws IllegalArgumentException 引数に{@code null}を与えた場合 + * @throws IllegalArgumentException 引数に{@code null}を与えた場合 */ public static <R, X extends Exception>R accept(ResultSet target, ResultSetVisitor<ResultSet, R, X> visitor) throws SQLException, X { Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/ColumnMeta.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/ColumnMeta.java 2009-04-02 12:09:20 UTC (rev 3062) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/ColumnMeta.java 2009-04-02 16:15:24 UTC (rev 3063) @@ -144,11 +144,13 @@ // "NO" means column definitely does not allow NULL values; // "YES" means the column might allow NULL values. // An empty string means nobody knows. - scopeCatalog = column.getString("SCOPE_CATLOG"); - scopeSchema = column.getString("SCOPE_SCHEMA"); - scopeTable = column.getString("SCOPE_TABLE"); - sourceDataType = column.getShort("SOURCE_DATA_TYPE"); + // FIXME + scopeCatalog = null; //column.getString("SCOPE_CATLOG"); + scopeSchema = null; //column.getString("SCOPE_SCHEMA"); + scopeTable = null; //column.getString("SCOPE_TABLE"); + sourceDataType = 0; //column.getShort("SOURCE_DATA_TYPE"); + assert tableName != null; assert columnName != null; assert typeName != null; Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/KeyMeta.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/KeyMeta.java 2009-04-02 12:09:20 UTC (rev 3062) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/KeyMeta.java 2009-04-02 16:15:24 UTC (rev 3063) @@ -86,8 +86,8 @@ */ public KeyMeta(ResultSet importedKey) throws SQLException { Validate.notNull(importedKey); - pkTableCat = importedKey.getString("PKTABLE_CAT"); - pkTableSchem = importedKey.getString("PKTABLE_SCHEM"); + pkTableCat = TypeSafeDatabaseMetaData.careException(importedKey, "PKTABLE_CAT"); + pkTableSchem = TypeSafeDatabaseMetaData.careException(importedKey, "PKTABLE_SCHEM"); pkTableName = importedKey.getString("PKTABLE_NAME"); pkColumnName = importedKey.getString("PKCOLUMN_NAME"); fkTableCat = importedKey.getString("FKTABLE_CAT"); Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TableMeta.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TableMeta.java 2009-04-02 12:09:20 UTC (rev 3062) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TableMeta.java 2009-04-02 16:15:24 UTC (rev 3063) @@ -83,15 +83,21 @@ tableName = table.getString("TABLE_NAME"); tableType = table.getString("TABLE_TYPE"); remarks = table.getString("REMARKS"); - typeCat = table.getString("TYPE_CAT"); - typeSchem = table.getString("TYPE_SCHEM"); - typeName = table.getString("TYPE_NAME"); - selfReferencingColName = table.getString("SELF_REFERENCING_COL_NAME"); - refGeneration = RefGeneration.valueOf(table.getString("REF_GENERATION")); + typeCat = TypeSafeDatabaseMetaData.careException(table, "TYPE_CAT"); + typeSchem = TypeSafeDatabaseMetaData.careException(table, "TYPE_SCHEM"); + typeName = TypeSafeDatabaseMetaData.careException(table, "TYPE_NAME"); + selfReferencingColName = TypeSafeDatabaseMetaData.careException(table, "SELF_REFERENCING_COL_NAME"); + String refGenerationString = TypeSafeDatabaseMetaData.careException(table, "REF_GENERATION"); + if (refGenerationString == null) { + refGeneration = null; + } else { + refGeneration = RefGeneration.valueOf(refGenerationString); + } + assert tableName != null; - assert tableType != null; - assert remarks != null; // THINK ? +// assert tableType != null; +// assert remarks != null; // THINK ? } @Override Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TypeSafeDatabaseMetaData.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TypeSafeDatabaseMetaData.java 2009-04-02 12:09:20 UTC (rev 3062) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/metadata/TypeSafeDatabaseMetaData.java 2009-04-02 16:15:24 UTC (rev 3063) @@ -25,8 +25,11 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; +import java.util.NoSuchElementException; import org.apache.commons.lang.Validate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.jiemamy.exception.UnexpectedConditionError; @@ -37,12 +40,22 @@ */ public class TypeSafeDatabaseMetaData { + static String careException(ResultSet rs, String columnName) { + try { + return rs.getString(columnName); + } catch (SQLException e) { + return null; + } + } + + private DatabaseMetaData meta; /** * インスタンスを生成する。 - * @param connection + * + * @param connection 取り扱うDBへのコネクション * @throws SQLException SQLの実行に失敗した場合 * @throws IllegalArgumentException 引数に{@code null}を与えた場合 */ @@ -53,7 +66,8 @@ /** * インスタンスを生成する。 - * @param meta + * + * @param meta 取り扱う{@link DatabaseMetaData} * @throws IllegalArgumentException 引数に{@code null}を与えた場合 */ public TypeSafeDatabaseMetaData(DatabaseMetaData meta) { @@ -382,8 +396,8 @@ /** * {@link ResultSet}の{@link Iterator}を取得できる{@link Iterable}の実装クラス。 * + * @param <T> イテレートする型 * @author daisuke - * @param <T> */ public static class IterableResult<T> implements Iterable<T> { @@ -417,16 +431,27 @@ /** * ResultSetをラップするイテレータ。 * - * @param <T> + * @param <T> イテレート要素の型 * @author daisuke */ static class ResultSetIterator<T> implements Iterator<T> { + private static Logger logger = LoggerFactory.getLogger(ResultSetIterator.class); + private ResultSet resultSet; private Class<T> resultClass; + /** + * インスタンスを生成する。 + * + * <p>resultClassは、resultSetを引数に持つコンストラクタを持っていなければならない。</p> + * + * @param resultSet イテレート対象の{@link ResultSet} + * @param resultClass イテレート要素の型 + * @throws IllegalArgumentException 引数に{@code null}を与えた場合 + */ ResultSetIterator(ResultSet resultSet, Class<T> resultClass) { Validate.notNull(resultSet); Validate.notNull(resultClass); @@ -435,36 +460,40 @@ } public boolean hasNext() { + boolean hasNext = false; try { - return resultSet.isLast() == false; + // HACK nextしてpreviousするとか、イヤです。 + hasNext = resultSet.next(); + resultSet.previous(); } catch (SQLException e) { + logger.warn("SQLException", e); // ignore } - return false; + return hasNext; } public T next() { try { - try { - resultSet.next(); - Constructor<T> constructor = resultClass.getConstructor(ResultSet.class); - return constructor.newInstance(resultSet); - } catch (SecurityException e) { - throw new UnexpectedConditionError("unknown", e); - } catch (NoSuchMethodException e) { - throw new UnexpectedConditionError("resultClass must have ResultSet constructor.", e); - } catch (IllegalArgumentException e) { - throw new UnexpectedConditionError("unknown", e); - } catch (InstantiationException e) { - throw new UnexpectedConditionError("unknown", e); - } catch (IllegalAccessException e) { - throw new UnexpectedConditionError("resultClass must have public constructor.", e); - } catch (InvocationTargetException e) { - throw new UnexpectedConditionError("unknown", e); - } finally { - resultSet.close(); + boolean next = resultSet.next(); + if (next == false) { + throw new NoSuchElementException(); } + Constructor<T> constructor = resultClass.getConstructor(ResultSet.class); + return constructor.newInstance(resultSet); + } catch (SecurityException e) { + throw new UnexpectedConditionError("unknown", e); + } catch (NoSuchMethodException e) { + throw new UnexpectedConditionError("resultClass must have ResultSet constructor.", e); + } catch (IllegalArgumentException e) { + throw new UnexpectedConditionError("unknown", e); + } catch (InstantiationException e) { + throw new UnexpectedConditionError("unknown", e); + } catch (IllegalAccessException e) { + throw new UnexpectedConditionError("resultClass must have public constructor.", e); + } catch (InvocationTargetException e) { + throw new UnexpectedConditionError("resultClass construction failed.", e); } catch (SQLException e) { + logger.warn("SQLException", e); // ignore } return null; @@ -474,6 +503,7 @@ try { resultSet.deleteRow(); } catch (SQLException e) { + logger.warn("SQLException", e); // ignore } }