You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatementHelper.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.util.sqlcontainer.query.generator;
  17. import java.io.Serializable;
  18. import java.math.BigDecimal;
  19. import java.sql.Date;
  20. import java.sql.PreparedStatement;
  21. import java.sql.SQLException;
  22. import java.sql.Time;
  23. import java.sql.Timestamp;
  24. import java.sql.Types;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * StatementHelper is a simple helper class that assists TableQuery and the
  31. * query generators in filling a PreparedStatement. The actual statement is
  32. * generated by the query generator methods, but the resulting statement and all
  33. * the parameter values are stored in an instance of StatementHelper.
  34. *
  35. * This class will also fill the values with correct setters into the
  36. * PreparedStatement on request.
  37. *
  38. * @deprecated As of 8.0, no replacement available.
  39. */
  40. @Deprecated
  41. public class StatementHelper implements Serializable {
  42. private String queryString;
  43. private List<Object> parameters = new ArrayList<Object>();
  44. private Map<Integer, Class<?>> dataTypes = new HashMap<Integer, Class<?>>();
  45. public StatementHelper() {
  46. }
  47. public void setQueryString(String queryString) {
  48. this.queryString = queryString;
  49. }
  50. public String getQueryString() {
  51. return queryString;
  52. }
  53. public void addParameterValue(Object parameter) {
  54. if (parameter != null) {
  55. parameters.add(parameter);
  56. dataTypes.put(parameters.size() - 1, parameter.getClass());
  57. } else {
  58. throw new IllegalArgumentException(
  59. "You cannot add null parameters using addParameters(Object). "
  60. + "Use addParameters(Object,Class) instead");
  61. }
  62. }
  63. public void addParameterValue(Object parameter, Class<?> type) {
  64. parameters.add(parameter);
  65. dataTypes.put(parameters.size() - 1, type);
  66. }
  67. public void setParameterValuesToStatement(PreparedStatement pstmt)
  68. throws SQLException {
  69. for (int i = 0; i < parameters.size(); i++) {
  70. if (parameters.get(i) == null) {
  71. handleNullValue(i, pstmt);
  72. } else {
  73. pstmt.setObject(i + 1, parameters.get(i));
  74. }
  75. }
  76. /*
  77. * The following list contains the data types supported by
  78. * PreparedStatement but not supported by SQLContainer:
  79. *
  80. * [The list is provided as PreparedStatement method signatures]
  81. *
  82. * setNCharacterStream(int parameterIndex, Reader value)
  83. *
  84. * setNClob(int parameterIndex, NClob value)
  85. *
  86. * setNString(int parameterIndex, String value)
  87. *
  88. * setRef(int parameterIndex, Ref x)
  89. *
  90. * setRowId(int parameterIndex, RowId x)
  91. *
  92. * setSQLXML(int parameterIndex, SQLXML xmlObject)
  93. *
  94. * setBytes(int parameterIndex, byte[] x)
  95. *
  96. * setCharacterStream(int parameterIndex, Reader reader)
  97. *
  98. * setClob(int parameterIndex, Clob x)
  99. *
  100. * setURL(int parameterIndex, URL x)
  101. *
  102. * setArray(int parameterIndex, Array x)
  103. *
  104. * setAsciiStream(int parameterIndex, InputStream x)
  105. *
  106. * setBinaryStream(int parameterIndex, InputStream x)
  107. *
  108. * setBlob(int parameterIndex, Blob x)
  109. */
  110. }
  111. private void handleNullValue(int i, PreparedStatement pstmt)
  112. throws SQLException {
  113. Class<?> dataType = dataTypes.get(i);
  114. int index = i + 1;
  115. if (BigDecimal.class.equals(dataType)) {
  116. pstmt.setBigDecimal(index, null);
  117. } else if (Boolean.class.equals(dataType)) {
  118. pstmt.setNull(index, Types.BOOLEAN);
  119. } else if (Byte.class.equals(dataType)) {
  120. pstmt.setNull(index, Types.SMALLINT);
  121. } else if (Date.class.equals(dataType)) {
  122. pstmt.setDate(index, null);
  123. } else if (Double.class.equals(dataType)) {
  124. pstmt.setNull(index, Types.DOUBLE);
  125. } else if (Float.class.equals(dataType)) {
  126. pstmt.setNull(index, Types.FLOAT);
  127. } else if (Integer.class.equals(dataType)) {
  128. pstmt.setNull(index, Types.INTEGER);
  129. } else if (Long.class.equals(dataType)) {
  130. pstmt.setNull(index, Types.BIGINT);
  131. } else if (Short.class.equals(dataType)) {
  132. pstmt.setNull(index, Types.SMALLINT);
  133. } else if (String.class.equals(dataType)) {
  134. pstmt.setString(index, null);
  135. } else if (Time.class.equals(dataType)) {
  136. pstmt.setTime(index, null);
  137. } else if (Timestamp.class.equals(dataType)) {
  138. pstmt.setTimestamp(index, null);
  139. } else if (byte[].class.equals(dataType)) {
  140. pstmt.setBytes(index, null);
  141. } else {
  142. if (handleUnrecognizedTypeNullValue(i, pstmt, dataTypes)) {
  143. return;
  144. }
  145. throw new SQLException("Data type for parameter " + i
  146. + " not supported by SQLContainer: " + dataType.getName());
  147. }
  148. }
  149. /**
  150. * Handle unrecognized null values. Override this to handle null values for
  151. * platform specific data types that are not handled by the default
  152. * implementation of the {@link StatementHelper}.
  153. *
  154. * @param i
  155. * @param pstmt
  156. * @param dataTypes2
  157. *
  158. * @return true if handled, false otherwise
  159. *
  160. * @see {@link http://dev.vaadin.com/ticket/9148}
  161. */
  162. protected boolean handleUnrecognizedTypeNullValue(int i,
  163. PreparedStatement pstmt, Map<Integer, Class<?>> dataTypes)
  164. throws SQLException {
  165. return false;
  166. }
  167. }