java/jdbc-loopback-driver/src/main/java/info/globalcode/jdbc/loopback/PreparedStatement.java
3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.jdbc.loopback;
19 import java.sql.SQLException;
20 import java.sql.SQLWarning;
21 import java.sql.Types;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
29 * @author Ing. František Kučera (frantovo.cz)
31 public class PreparedStatement extends AbstractPreparedStatement {
33 private final Map<Integer, ObjectParameter> parameters = new HashMap<>();
34 private List<Object[]> table;
35 private final ResultSetMetaData metadata = new ResultSetMetaData();
38 public void setObject(int parameterNumber, Object data, int targetSqlType) throws SQLException {
39 parameters.put(parameterNumber, new ObjectParameter(data, targetSqlType));
43 public boolean execute() throws SQLException {
45 if (parameters.size() < 1) {
46 throw new SQLException("Missing first parameter (column count)");
48 int columnCount = Integer.valueOf(String.valueOf(parameters.get(1).getData()));
50 for (int i = 0; i < columnCount; i++) {
51 String label = parameters.get(1 + i + 1).getData().toString();
52 metadata.addColumn(new ResultSetMetaData.ColumnDescriptor(Types.VARCHAR, "VARCHAR", label, label));
57 table = new ArrayList<>();
58 Object[] currentRow = null;
60 for (int parameterNumber = (1 + columnCount + 1); true; parameterNumber++) {
61 ObjectParameter data = parameters.get(parameterNumber);
65 int columnIndex = cellIndex % columnCount;
67 if (columnIndex == 0) {
68 currentRow = new Object[columnCount];
69 table.add(currentRow);
71 currentRow[columnIndex] = data.getData();
80 public java.sql.ResultSet getResultSet() throws SQLException {
81 return new ResultSet(metadata, table);
85 public int getUpdateCount() throws SQLException {
90 public boolean getMoreResults() throws SQLException {
95 public void close() throws SQLException {
99 public SQLWarning getWarnings() throws SQLException {
104 public void clearWarnings() throws SQLException {