java/jdbc-loopback-driver/src/info/globalcode/jdbc/loopback/PreparedStatement.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 13 Sep 2015 19:25:36 +0200
branchv_0
changeset 227 0094319a274a
parent 176 9aa00e214020
permissions -rw-r--r--
headerTypes: new option to hide column types in tabular headers
     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, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.jdbc.loopback;
    19 
    20 import java.sql.SQLException;
    21 import java.sql.SQLWarning;
    22 import java.sql.Types;
    23 import java.util.ArrayList;
    24 import java.util.HashMap;
    25 import java.util.List;
    26 import java.util.Map;
    27 
    28 /**
    29  *
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 public class PreparedStatement extends AbstractPreparedStatement {
    33 
    34 	private final Map<Integer, ObjectParameter> parameters = new HashMap<>();
    35 	private List<Object[]> table;
    36 	private final ResultSetMetaData metadata = new ResultSetMetaData();
    37 
    38 	@Override
    39 	public void setObject(int parameterNumber, Object data, int targetSqlType) throws SQLException {
    40 		parameters.put(parameterNumber, new ObjectParameter(data, targetSqlType));
    41 	}
    42 
    43 	@Override
    44 	public boolean execute() throws SQLException {
    45 
    46 		if (parameters.size() < 1) {
    47 			throw new SQLException("Missing first parameter (column count)");
    48 		} else {
    49 			int columnCount = Integer.valueOf(String.valueOf(parameters.get(1).getData()));
    50 
    51 			for (int i = 0; i < columnCount; i++) {
    52 				String label = parameters.get(1 + i + 1).getData().toString();
    53 				metadata.addColumn(new ResultSetMetaData.ColumnDescriptor(Types.VARCHAR, "VARCHAR", label, label));
    54 			}
    55 
    56 			int cellIndex = 0;
    57 
    58 			table = new ArrayList<>();
    59 			Object[] currentRow = null;
    60 
    61 			for (int parameterNumber = (1 + columnCount + 1); true; parameterNumber++) {
    62 				ObjectParameter data = parameters.get(parameterNumber);
    63 				if (data == null) {
    64 					break;
    65 				} else {
    66 					int columnIndex = cellIndex % columnCount;
    67 					cellIndex++;
    68 					if (columnIndex == 0) {
    69 						currentRow = new Object[columnCount];
    70 						table.add(currentRow);
    71 					}
    72 					currentRow[columnIndex] = data.getData();
    73 				}
    74 			}
    75 
    76 			return true;
    77 		}
    78 	}
    79 
    80 	@Override
    81 	public java.sql.ResultSet getResultSet() throws SQLException {
    82 		return new ResultSet(metadata, table);
    83 	}
    84 
    85 	@Override
    86 	public int getUpdateCount() throws SQLException {
    87 		return -1;
    88 	}
    89 
    90 	@Override
    91 	public boolean getMoreResults() throws SQLException {
    92 		return false;
    93 	}
    94 
    95 	@Override
    96 	public void close() throws SQLException {
    97 	}
    98 
    99 	@Override
   100 	public SQLWarning getWarnings() throws SQLException {
   101 		return null;
   102 	}
   103 
   104 	@Override
   105 	public void clearWarnings() throws SQLException {
   106 	}
   107 }