java/jdbc-loopback-driver/src/main/java/info/globalcode/jdbc/loopback/PreparedStatement.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 237 7e08730da258
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * SQL-DK
 * Copyright © 2014 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.jdbc.loopback;

import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class PreparedStatement extends AbstractPreparedStatement {

	private final Map<Integer, ObjectParameter> parameters = new HashMap<>();
	private List<Object[]> table;
	private final ResultSetMetaData metadata = new ResultSetMetaData();

	@Override
	public void setObject(int parameterNumber, Object data, int targetSqlType) throws SQLException {
		parameters.put(parameterNumber, new ObjectParameter(data, targetSqlType));
	}

	@Override
	public boolean execute() throws SQLException {

		if (parameters.size() < 1) {
			throw new SQLException("Missing first parameter (column count)");
		} else {
			int columnCount = Integer.valueOf(String.valueOf(parameters.get(1).getData()));

			for (int i = 0; i < columnCount; i++) {
				String label = parameters.get(1 + i + 1).getData().toString();
				metadata.addColumn(new ResultSetMetaData.ColumnDescriptor(Types.VARCHAR, "VARCHAR", label, label));
			}

			int cellIndex = 0;

			table = new ArrayList<>();
			Object[] currentRow = null;

			for (int parameterNumber = (1 + columnCount + 1); true; parameterNumber++) {
				ObjectParameter data = parameters.get(parameterNumber);
				if (data == null) {
					break;
				} else {
					int columnIndex = cellIndex % columnCount;
					cellIndex++;
					if (columnIndex == 0) {
						currentRow = new Object[columnCount];
						table.add(currentRow);
					}
					currentRow[columnIndex] = data.getData();
				}
			}

			return true;
		}
	}

	@Override
	public java.sql.ResultSet getResultSet() throws SQLException {
		return new ResultSet(metadata, table);
	}

	@Override
	public int getUpdateCount() throws SQLException {
		return -1;
	}

	@Override
	public boolean getMoreResults() throws SQLException {
		return false;
	}

	@Override
	public void close() throws SQLException {
	}

	@Override
	public SQLWarning getWarnings() throws SQLException {
		return null;
	}

	@Override
	public void clearWarnings() throws SQLException {
	}
}