package com.chncwang.easy2db.sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import com.chncwang.easy2db.parser.TableParser;
import com.chncwang.easy2db.parser.TableParserUtil;
import com.chncwang.easy2db.table.ColumnDef;
import com.chncwang.easy2db.table.ForeignKeyDef;
import com.chncwang.easy2db.table.TableDef;
import com.chncwang.easy2db.table.util.TableUtil;

public class SqlUtil {
    private SqlUtil() {}

    public static Object toObject(final ResultSet resultSet,
            final Class<?> clazz) throws SQLException {
        final Object object;
        try {
            object = clazz.newInstance();
        } catch (final InstantiationException e) {
            throw new IllegalArgumentException(e);
        } catch (final IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }

        final TableParser tableParser = TableParser.getTableParser(clazz);
        final Map<String, Class<?>> classMap = TableParserUtil
                .createColumnClassMap(tableParser);

        final TableDef tableDef = tableParser.getTableDef();
        try {
            final Object primaryKeyValue = resultSet.getObject(
                    TableUtil.getPrimaryKeyName(tableDef), classMap);
            tableParser.getPrimaryKeyField().set(object, primaryKeyValue);

            final Object uniqueKeyValue = resultSet.getObject(tableDef
                    .getUniqueKeyDef().getName(), classMap);
            tableParser.getUniqueKeyField().set(object, uniqueKeyValue);

            for (int i=0; i<tableDef.getForeignKeyDefs().size(); ++i) {
                final ForeignKeyDef foreignKeyDef = tableDef
                        .getForeignKeyDefs().get(i);
                final Object foreignKeyValue = resultSet.getObject(
                        foreignKeyDef.getColumnName(), classMap);
                tableParser.getForeignFields().get(i)
                        .set(object, foreignKeyValue);
            }

            for (int i=0; i<tableDef.getColumnDefs().size(); ++i) {
                final ColumnDef columnDef = tableDef.getColumnDefs().get(i);
                final Object columnValue = resultSet.getObject(
                        columnDef.getName(), classMap);
                tableParser.getColumnFields().get(i).set(object, columnValue);
            }
        } catch (final IllegalAccessException e) {
            throw new IllegalStateException(e);
        }

        return object;
    }
}

@@ -257,57 +257,8 @@
 ef;%0A
-import com.chncwang.easy2db.table.ForeignKeyDef;%0A
 impo
@@ -1500,452 +1500,8 @@
 );%0A%0A
-            for (int i=0; i%3CtableDef.getForeignKeyDefs().size(); ++i) %7B%0A                final ForeignKeyDef foreignKeyDef = tableDef%0A                        .getForeignKeyDefs().get(i);%0A                final Object foreignKeyValue = resultSet.getObject(%0A                        foreignKeyDef.getColumnName(), classMap);%0A                tableParser.getForeignFields().get(i)%0A                        .set(object, foreignKeyValue);%0A            %7D%0A%0A
     

Fix a bug in select method.