java/jdbc-loopback-driver/src/main/java/info/globalcode/jdbc/loopback/PreparedStatement.java
branchv_0
changeset 237 7e08730da258
parent 176 9aa00e214020
child 250 aae5009bd0af
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/jdbc-loopback-driver/src/main/java/info/globalcode/jdbc/loopback/PreparedStatement.java	Mon Mar 04 17:06:42 2019 +0100
@@ -0,0 +1,107 @@
+/**
+ * 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, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 {
+	}
+}