package com.topdesk.sqltransfer;_x000D_
_x000D_
import java.sql.ResultSet;_x000D_
import java.sql.SQLException;_x000D_
import java.sql.Statement;_x000D_
import java.text.NumberFormat;_x000D_
import java.util.Collections;_x000D_
import java.util.HashMap;_x000D_
import java.util.List;_x000D_
import java.util.Map;_x000D_
import java.util.NoSuchElementException;_x000D_
_x000D_
import org.slf4j.LoggerFactory;_x000D_
_x000D_
public class SQLtransferSQLResultSet implements SQLtransferResultSet {_x000D_
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SQLtransferResultSet.class);_x000D_
	_x000D_
	@SuppressWarnings("unused")_x000D_
	private final Object finalizerGuardian = new Object() {_x000D_
		@Override_x000D_
		protected void finalize() throws Throwable {_x000D_
			try {_x000D_
				internalClose(false);_x000D_
			}_x000D_
			catch (Exception e) {_x000D_
				// Ignore_x000D_
			}_x000D_
		}_x000D_
	};_x000D_
	_x000D_
	private final Statement statement;_x000D_
	private final ResultSet rs;_x000D_
	private final List<ColumnMetaData> metaData;_x000D_
_x000D_
	boolean needsNext = true;_x000D_
	boolean hasNext;_x000D_
_x000D_
	public SQLtransferSQLResultSet(SQLtransferSQLConnection connection, ImportTable table) throws SQLtransferException {_x000D_
		statement = connection.createStatement();_x000D_
		try {_x000D_
			long begin = System.currentTimeMillis();_x000D_
			rs = statement.executeQuery(table.getQuery());_x000D_
	  		NumberFormat nf = NumberFormat.getInstance();_x000D_
			logger.info(String.format("    Duration : %s ms", nf.format(System.currentTimeMillis() - begin)));_x000D_
			metaData = connection.createMetaData(rs.getMetaData());_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			throw new SQLtransferException(e);_x000D_
		}_x000D_
	}_x000D_
_x000D_
	public List<ColumnMetaData> getMetaData() throws SQLtransferException {_x000D_
		return metaData;_x000D_
	}_x000D_
_x000D_
	private Object getValue(ColumnMetaData column) throws SQLtransferException {_x000D_
		try {_x000D_
			int index = column.getIndex();_x000D_
			// Vieze fix voor Oracle, volgens MetaData is het opeens 2005 en omzetten lukt niet_x000D_
			if (column.getType() == 2005) {_x000D_
				return rs.getString(index);_x000D_
			}_x000D_
			return column.getValue(rs.getObject(index));_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			throw new SQLtransferException(e);_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	_x000D_
	public boolean hasNext()  {_x000D_
		if (needsNext) {_x000D_
			needsNext = false;_x000D_
			try {_x000D_
				hasNext = rs.next();_x000D_
			} _x000D_
			catch (SQLException e) {_x000D_
				throw new RuntimeException(e);_x000D_
			}		_x000D_
		}_x000D_
		return hasNext;_x000D_
	}_x000D_
	_x000D_
	public Map<String, Object> next() {_x000D_
		if (!hasNext()) {_x000D_
			throw new NoSuchElementException();_x000D_
		}_x000D_
		needsNext = true;_x000D_
		_x000D_
		Map<String, Object> data = new HashMap<String, Object>(); _x000D_
		for (ColumnMetaData column : metaData) {_x000D_
			try {_x000D_
				data.put(column.getName().toLowerCase(), getValue(column));_x000D_
			} _x000D_
			catch (SQLtransferException e) {_x000D_
				throw new RuntimeException(e);_x000D_
			}		_x000D_
		}_x000D_
		_x000D_
		if (!hasNext()) {_x000D_
			internalClose(true);_x000D_
		}_x000D_
		return Collections.unmodifiableMap(data);_x000D_
	}_x000D_
	_x000D_
	public void remove() {_x000D_
		throw new UnsupportedOperationException();_x000D_
	}_x000D_
_x000D_
	private void internalClose(boolean log) {_x000D_
		try {_x000D_
			rs.close();_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			if (log) {_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
		try {_x000D_
			statement.close();_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			if (log) {_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
	}_x000D_
}_x000D_

// Vieze fix voor Oracle, volgens MetaData is het opeens 2005 en omzetten lukt niet_x000D_
package com.topdesk.sqltransfer;_x000D_
_x000D_
import java.sql.ResultSet;_x000D_
import java.sql.SQLException;_x000D_
import java.sql.Statement;_x000D_
import java.text.NumberFormat;_x000D_
import java.util.Collections;_x000D_
import java.util.HashMap;_x000D_
import java.util.List;_x000D_
import java.util.Map;_x000D_
import java.util.NoSuchElementException;_x000D_
_x000D_
import org.slf4j.LoggerFactory;_x000D_
_x000D_
public class SQLtransferSQLResultSet implements SQLtransferResultSet {_x000D_
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SQLtransferResultSet.class);_x000D_
	_x000D_
	@SuppressWarnings("unused")_x000D_
	private final Object finalizerGuardian = new Object() {_x000D_
		@Override_x000D_
		protected void finalize() throws Throwable {_x000D_
			try {_x000D_
				internalClose(false);_x000D_
			}_x000D_
			catch (Exception e) {_x000D_
				// Ignore_x000D_
			}_x000D_
		}_x000D_
	};_x000D_
	_x000D_
	private final Statement statement;_x000D_
	private final ResultSet rs;_x000D_
	private final List<ColumnMetaData> metaData;_x000D_
_x000D_
	boolean needsNext = true;_x000D_
	boolean hasNext;_x000D_
_x000D_
	public SQLtransferSQLResultSet(SQLtransferSQLConnection connection, ImportTable table) throws SQLtransferException {_x000D_
		statement = connection.createStatement();_x000D_
		try {_x000D_
			long begin = System.currentTimeMillis();_x000D_
			rs = statement.executeQuery(table.getQuery());_x000D_
	  		NumberFormat nf = NumberFormat.getInstance();_x000D_
			logger.info(String.format("    Duration : %s ms", nf.format(System.currentTimeMillis() - begin)));_x000D_
			metaData = connection.createMetaData(rs.getMetaData());_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			throw new SQLtransferException(e);_x000D_
		}_x000D_
	}_x000D_
_x000D_
	public List<ColumnMetaData> getMetaData() throws SQLtransferException {_x000D_
		return metaData;_x000D_
	}_x000D_
_x000D_
	private Object getValue(ColumnMetaData column) throws SQLtransferException {_x000D_
		try {_x000D_
			int index = column.getIndex();_x000D_
			// Vieze fix<SUF>
			if (column.getType() == 2005) {_x000D_
				return rs.getString(index);_x000D_
			}_x000D_
			return column.getValue(rs.getObject(index));_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			throw new SQLtransferException(e);_x000D_
		}_x000D_
	}_x000D_
	_x000D_
	_x000D_
	public boolean hasNext()  {_x000D_
		if (needsNext) {_x000D_
			needsNext = false;_x000D_
			try {_x000D_
				hasNext = rs.next();_x000D_
			} _x000D_
			catch (SQLException e) {_x000D_
				throw new RuntimeException(e);_x000D_
			}		_x000D_
		}_x000D_
		return hasNext;_x000D_
	}_x000D_
	_x000D_
	public Map<String, Object> next() {_x000D_
		if (!hasNext()) {_x000D_
			throw new NoSuchElementException();_x000D_
		}_x000D_
		needsNext = true;_x000D_
		_x000D_
		Map<String, Object> data = new HashMap<String, Object>(); _x000D_
		for (ColumnMetaData column : metaData) {_x000D_
			try {_x000D_
				data.put(column.getName().toLowerCase(), getValue(column));_x000D_
			} _x000D_
			catch (SQLtransferException e) {_x000D_
				throw new RuntimeException(e);_x000D_
			}		_x000D_
		}_x000D_
		_x000D_
		if (!hasNext()) {_x000D_
			internalClose(true);_x000D_
		}_x000D_
		return Collections.unmodifiableMap(data);_x000D_
	}_x000D_
	_x000D_
	public void remove() {_x000D_
		throw new UnsupportedOperationException();_x000D_
	}_x000D_
_x000D_
	private void internalClose(boolean log) {_x000D_
		try {_x000D_
			rs.close();_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			if (log) {_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
		try {_x000D_
			statement.close();_x000D_
		} _x000D_
		catch (SQLException e) {_x000D_
			if (log) {_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
	}_x000D_
}_x000D_
