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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.sqlcontainer.query.generator;
  5. import java.io.Serializable;
  6. import java.math.BigDecimal;
  7. import java.sql.Date;
  8. import java.sql.PreparedStatement;
  9. import java.sql.SQLException;
  10. import java.sql.Time;
  11. import java.sql.Timestamp;
  12. import java.sql.Types;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * StatementHelper is a simple helper class that assists TableQuery and the
  19. * query generators in filling a PreparedStatement. The actual statement is
  20. * generated by the query generator methods, but the resulting statement and all
  21. * the parameter values are stored in an instance of StatementHelper.
  22. *
  23. * This class will also fill the values with correct setters into the
  24. * PreparedStatement on request.
  25. */
  26. public class StatementHelper implements Serializable {
  27. private String queryString;
  28. private List<Object> parameters = new ArrayList<Object>();
  29. private Map<Integer, Class<?>> dataTypes = new HashMap<Integer, Class<?>>();
  30. public StatementHelper() {
  31. }
  32. public void setQueryString(String queryString) {
  33. this.queryString = queryString;
  34. }
  35. public String getQueryString() {
  36. return queryString;
  37. }
  38. public void addParameterValue(Object parameter) {
  39. if (parameter != null) {
  40. parameters.add(parameter);
  41. dataTypes.put(parameters.size() - 1, parameter.getClass());
  42. }
  43. }
  44. public void addParameterValue(Object parameter, Class<?> type) {
  45. parameters.add(parameter);
  46. dataTypes.put(parameters.size() - 1, type);
  47. }
  48. public void setParameterValuesToStatement(PreparedStatement pstmt)
  49. throws SQLException {
  50. for (int i = 0; i < parameters.size(); i++) {
  51. if (parameters.get(i) == null) {
  52. handleNullValue(i, pstmt);
  53. } else {
  54. pstmt.setObject(i + 1, parameters.get(i));
  55. }
  56. }
  57. /*
  58. * The following list contains the data types supported by
  59. * PreparedStatement but not supported by SQLContainer:
  60. *
  61. * [The list is provided as PreparedStatement method signatures]
  62. *
  63. * setNCharacterStream(int parameterIndex, Reader value)
  64. *
  65. * setNClob(int parameterIndex, NClob value)
  66. *
  67. * setNString(int parameterIndex, String value)
  68. *
  69. * setRef(int parameterIndex, Ref x)
  70. *
  71. * setRowId(int parameterIndex, RowId x)
  72. *
  73. * setSQLXML(int parameterIndex, SQLXML xmlObject)
  74. *
  75. * setBytes(int parameterIndex, byte[] x)
  76. *
  77. * setCharacterStream(int parameterIndex, Reader reader)
  78. *
  79. * setClob(int parameterIndex, Clob x)
  80. *
  81. * setURL(int parameterIndex, URL x)
  82. *
  83. * setArray(int parameterIndex, Array x)
  84. *
  85. * setAsciiStream(int parameterIndex, InputStream x)
  86. *
  87. * setBinaryStream(int parameterIndex, InputStream x)
  88. *
  89. * setBlob(int parameterIndex, Blob x)
  90. */
  91. }
  92. private void handleNullValue(int i, PreparedStatement pstmt)
  93. throws SQLException {
  94. if (BigDecimal.class.equals(dataTypes.get(i))) {
  95. pstmt.setBigDecimal(i + 1, null);
  96. } else if (Boolean.class.equals(dataTypes.get(i))) {
  97. pstmt.setNull(i + 1, Types.BOOLEAN);
  98. } else if (Byte.class.equals(dataTypes.get(i))) {
  99. pstmt.setNull(i + 1, Types.SMALLINT);
  100. } else if (Date.class.equals(dataTypes.get(i))) {
  101. pstmt.setDate(i + 1, null);
  102. } else if (Double.class.equals(dataTypes.get(i))) {
  103. pstmt.setNull(i + 1, Types.DOUBLE);
  104. } else if (Float.class.equals(dataTypes.get(i))) {
  105. pstmt.setNull(i + 1, Types.FLOAT);
  106. } else if (Integer.class.equals(dataTypes.get(i))) {
  107. pstmt.setNull(i + 1, Types.INTEGER);
  108. } else if (Long.class.equals(dataTypes.get(i))) {
  109. pstmt.setNull(i + 1, Types.BIGINT);
  110. } else if (Short.class.equals(dataTypes.get(i))) {
  111. pstmt.setNull(i + 1, Types.SMALLINT);
  112. } else if (String.class.equals(dataTypes.get(i))) {
  113. pstmt.setString(i + 1, null);
  114. } else if (Time.class.equals(dataTypes.get(i))) {
  115. pstmt.setTime(i + 1, null);
  116. } else if (Timestamp.class.equals(dataTypes.get(i))) {
  117. pstmt.setTimestamp(i + 1, null);
  118. } else {
  119. throw new SQLException("Data type not supported by SQLContainer: "
  120. + parameters.get(i).getClass().toString());
  121. }
  122. }
  123. }