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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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
  39. public class StatementHelper implements Serializable {
  40. private String queryString;
  41. private List<Object> parameters = new ArrayList<Object>();
  42. private Map<Integer, Class<?>> dataTypes = new HashMap<Integer, Class<?>>();
  43. public StatementHelper() {
  44. }
  45. public void setQueryString(String queryString) {
  46. this.queryString = queryString;
  47. }
  48. public String getQueryString() {
  49. return queryString;
  50. }
  51. public void addParameterValue(Object parameter) {
  52. if (parameter != null) {
  53. parameters.add(parameter);
  54. dataTypes.put(parameters.size() - 1, parameter.getClass());
  55. } else {
  56. throw new IllegalArgumentException(
  57. "You cannot add null parameters using addParamaters(Object). "
  58. + "Use addParameters(Object,Class) instead");
  59. }
  60. }
  61. public void addParameterValue(Object parameter, Class<?> type) {
  62. parameters.add(parameter);
  63. dataTypes.put(parameters.size() - 1, type);
  64. }
  65. public void setParameterValuesToStatement(PreparedStatement pstmt)
  66. throws SQLException {
  67. for (int i = 0; i < parameters.size(); i++) {
  68. if (parameters.get(i) == null) {
  69. handleNullValue(i, pstmt);
  70. } else {
  71. pstmt.setObject(i + 1, parameters.get(i));
  72. }
  73. }
  74. /*
  75. * The following list contains the data types supported by
  76. * PreparedStatement but not supported by SQLContainer:
  77. *
  78. * [The list is provided as PreparedStatement method signatures]
  79. *
  80. * setNCharacterStream(int parameterIndex, Reader value)
  81. *
  82. * setNClob(int parameterIndex, NClob value)
  83. *
  84. * setNString(int parameterIndex, String value)
  85. *
  86. * setRef(int parameterIndex, Ref x)
  87. *
  88. * setRowId(int parameterIndex, RowId x)
  89. *
  90. * setSQLXML(int parameterIndex, SQLXML xmlObject)
  91. *
  92. * setBytes(int parameterIndex, byte[] x)
  93. *
  94. * setCharacterStream(int parameterIndex, Reader reader)
  95. *
  96. * setClob(int parameterIndex, Clob x)
  97. *
  98. * setURL(int parameterIndex, URL x)
  99. *
  100. * setArray(int parameterIndex, Array x)
  101. *
  102. * setAsciiStream(int parameterIndex, InputStream x)
  103. *
  104. * setBinaryStream(int parameterIndex, InputStream x)
  105. *
  106. * setBlob(int parameterIndex, Blob x)
  107. */
  108. }
  109. private void handleNullValue(int i, PreparedStatement pstmt)
  110. throws SQLException {
  111. Class<?> dataType = dataTypes.get(i);
  112. int index = i + 1;
  113. if (BigDecimal.class.equals(dataType)) {
  114. pstmt.setBigDecimal(index, null);
  115. } else if (Boolean.class.equals(dataType)) {
  116. pstmt.setNull(index, Types.BOOLEAN);
  117. } else if (Byte.class.equals(dataType)) {
  118. pstmt.setNull(index, Types.SMALLINT);
  119. } else if (Date.class.equals(dataType)) {
  120. pstmt.setDate(index, null);
  121. } else if (Double.class.equals(dataType)) {
  122. pstmt.setNull(index, Types.DOUBLE);
  123. } else if (Float.class.equals(dataType)) {
  124. pstmt.setNull(index, Types.FLOAT);
  125. } else if (Integer.class.equals(dataType)) {
  126. pstmt.setNull(index, Types.INTEGER);
  127. } else if (Long.class.equals(dataType)) {
  128. pstmt.setNull(index, Types.BIGINT);
  129. } else if (Short.class.equals(dataType)) {
  130. pstmt.setNull(index, Types.SMALLINT);
  131. } else if (String.class.equals(dataType)) {
  132. pstmt.setString(index, null);
  133. } else if (Time.class.equals(dataType)) {
  134. pstmt.setTime(index, null);
  135. } else if (Timestamp.class.equals(dataType)) {
  136. pstmt.setTimestamp(index, null);
  137. } else if (byte[].class.equals(dataType)) {
  138. pstmt.setBytes(index, null);
  139. } else {
  140. if (handleUnrecognizedTypeNullValue(i, pstmt, dataTypes)) {
  141. return;
  142. }
  143. throw new SQLException("Data type for parameter " + i
  144. + " not supported by SQLContainer: " + dataType.getName());
  145. }
  146. }
  147. /**
  148. * Handle unrecognized null values. Override this to handle null values for
  149. * platform specific data types that are not handled by the default
  150. * implementation of the {@link StatementHelper}.
  151. *
  152. * @param i
  153. * @param pstmt
  154. * @param dataTypes2
  155. *
  156. * @return true if handled, false otherwise
  157. *
  158. * @see {@link http://dev.vaadin.com/ticket/9148}
  159. */
  160. protected boolean handleUnrecognizedTypeNullValue(int i,
  161. PreparedStatement pstmt, Map<Integer, Class<?>> dataTypes)
  162. throws SQLException {
  163. return false;
  164. }
  165. }