franta-hg@171: /** franta-hg@171: * SQL-DK franta-hg@171: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@171: * franta-hg@171: * This program is free software: you can redistribute it and/or modify franta-hg@171: * it under the terms of the GNU General Public License as published by franta-hg@171: * the Free Software Foundation, either version 3 of the License, or franta-hg@171: * (at your option) any later version. franta-hg@171: * franta-hg@171: * This program is distributed in the hope that it will be useful, franta-hg@171: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@171: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@171: * GNU General Public License for more details. franta-hg@171: * franta-hg@171: * You should have received a copy of the GNU General Public License franta-hg@171: * along with this program. If not, see . franta-hg@171: */ franta-hg@171: package info.globalcode.jdbc.loopback; franta-hg@171: franta-hg@171: import java.sql.SQLException; franta-hg@171: import java.sql.SQLWarning; franta-hg@171: import java.sql.Types; franta-hg@171: import java.util.ArrayList; franta-hg@171: import java.util.HashMap; franta-hg@171: import java.util.List; franta-hg@171: import java.util.Map; franta-hg@171: franta-hg@171: /** franta-hg@171: * franta-hg@171: * @author Ing. František Kučera (frantovo.cz) franta-hg@171: */ franta-hg@171: public class PreparedStatement extends AbstractPreparedStatement { franta-hg@171: franta-hg@171: private Map parameters = new HashMap<>(); franta-hg@171: private List table; franta-hg@171: private ResultSetMetaData metadata = new ResultSetMetaData(); franta-hg@171: franta-hg@171: @Override franta-hg@171: public void setObject(int parameterIndex, Object data, int targetSqlType) throws SQLException { franta-hg@171: parameters.put(parameterIndex, new ObjectParameter(data, targetSqlType)); franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public boolean execute() throws SQLException { franta-hg@171: franta-hg@171: if (parameters.size() < 1) { franta-hg@171: throw new SQLException("Missing first parameter (column count)"); franta-hg@171: } else { franta-hg@171: int columnCount = Integer.valueOf((String) parameters.get(1).getData()); franta-hg@171: franta-hg@171: for (int i = 0; i < columnCount; i++) { franta-hg@171: String label = parameters.get(1 + i + 1).getData().toString(); franta-hg@171: metadata.addColumn(new ResultSetMetaData.ColumnDescriptor(Types.VARCHAR, "VARCHAR", label, label)); franta-hg@171: } franta-hg@171: franta-hg@171: int cellIndex = 0; franta-hg@171: franta-hg@171: table = new ArrayList<>(); franta-hg@171: Object[] currentRow = null; franta-hg@171: franta-hg@171: for (int parameterNumber = (1 + columnCount + 1); true; parameterNumber++) { franta-hg@171: ObjectParameter data = parameters.get(parameterNumber); franta-hg@171: if (data == null) { franta-hg@171: break; franta-hg@171: } else { franta-hg@171: int columnIndex = cellIndex % columnCount; franta-hg@171: cellIndex++; franta-hg@171: if (columnIndex == 0) { franta-hg@171: currentRow = new Object[columnCount]; franta-hg@171: table.add(currentRow); franta-hg@171: } franta-hg@171: currentRow[columnIndex] = data.getData(); franta-hg@171: } franta-hg@171: } franta-hg@171: franta-hg@171: return true; franta-hg@171: } franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public java.sql.ResultSet getResultSet() throws SQLException { franta-hg@171: return new ResultSet(metadata, table); franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public int getUpdateCount() throws SQLException { franta-hg@171: return -1; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public boolean getMoreResults() throws SQLException { franta-hg@171: return false; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public void close() throws SQLException { franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public SQLWarning getWarnings() throws SQLException { franta-hg@171: return null; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public void clearWarnings() throws SQLException { franta-hg@171: } franta-hg@171: }