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
     1 /**
     2  * SQL-DK
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 package info.globalcode.jdbc.loopback;
    18 
    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;
    25 import java.util.Map;
    26 
    27 /**
    28  *
    29  * @author Ing. František Kučera (frantovo.cz)
    30  */
    31 public class PreparedStatement extends AbstractPreparedStatement {
    32 
    33 	private final Map<Integer, ObjectParameter> parameters = new HashMap<>();
    34 	private List<Object[]> table;
    35 	private final ResultSetMetaData metadata = new ResultSetMetaData();
    36 
    37 	@Override
    38 	public void setObject(int parameterNumber, Object data, int targetSqlType) throws SQLException {
    39 		parameters.put(parameterNumber, new ObjectParameter(data, targetSqlType));
    40 	}
    41 
    42 	@Override
    43 	public boolean execute() throws SQLException {
    44 
    45 		if (parameters.size() < 1) {
    46 			throw new SQLException("Missing first parameter (column count)");
    47 		} else {
    48 			int columnCount = Integer.valueOf(String.valueOf(parameters.get(1).getData()));
    49 
    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));
    53 			}
    54 
    55 			int cellIndex = 0;
    56 
    57 			table = new ArrayList<>();
    58 			Object[] currentRow = null;
    59 
    60 			for (int parameterNumber = (1 + columnCount + 1); true; parameterNumber++) {
    61 				ObjectParameter data = parameters.get(parameterNumber);
    62 				if (data == null) {
    63 					break;
    64 				} else {
    65 					int columnIndex = cellIndex % columnCount;
    66 					cellIndex++;
    67 					if (columnIndex == 0) {
    68 						currentRow = new Object[columnCount];
    69 						table.add(currentRow);
    70 					}
    71 					currentRow[columnIndex] = data.getData();
    72 				}
    73 			}
    74 
    75 			return true;
    76 		}
    77 	}
    78 
    79 	@Override
    80 	public java.sql.ResultSet getResultSet() throws SQLException {
    81 		return new ResultSet(metadata, table);
    82 	}
    83 
    84 	@Override
    85 	public int getUpdateCount() throws SQLException {
    86 		return -1;
    87 	}
    88 
    89 	@Override
    90 	public boolean getMoreResults() throws SQLException {
    91 		return false;
    92 	}
    93 
    94 	@Override
    95 	public void close() throws SQLException {
    96 	}
    97 
    98 	@Override
    99 	public SQLWarning getWarnings() throws SQLException {
   100 		return null;
   101 	}
   102 
   103 	@Override
   104 	public void clearWarnings() throws SQLException {
   105 	}
   106 }