PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCTypeSimple.java

#
Java | 128 lines | 83 code | 17 blank | 28 comment | 13 complexity | a79e6610f93c1b168b018e2a3944705a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.cmp.jdbc;
  23. import java.sql.Types;
  24. import static org.jboss.as.cmp.CmpMessages.MESSAGES;
  25. /**
  26. * This class provides a simple mapping of a Java type type to a single column.
  27. *
  28. * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
  29. * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
  30. * @version $Revision: 81030 $
  31. */
  32. public final class JDBCTypeSimple implements JDBCType {
  33. private final String[] columnNames;
  34. private final Class[] javaTypes;
  35. private final int[] jdbcTypes;
  36. private final String[] sqlTypes;
  37. private final boolean[] notNull;
  38. private final boolean[] autoIncrement;
  39. private final JDBCResultSetReader[] resultSetReader;
  40. private final JDBCParameterSetter[] paramSetter;
  41. private final Mapper mapper;
  42. public JDBCTypeSimple(
  43. String columnName,
  44. Class javaType,
  45. int jdbcType,
  46. String sqlType,
  47. boolean notNull,
  48. boolean autoIncrement,
  49. Mapper mapper,
  50. JDBCParameterSetter paramSetter,
  51. JDBCResultSetReader resultReader
  52. ) {
  53. columnNames = new String[]{columnName};
  54. javaTypes = new Class[]{javaType};
  55. jdbcTypes = new int[]{jdbcType};
  56. sqlTypes = new String[]{sqlType};
  57. this.notNull = new boolean[]{notNull};
  58. this.autoIncrement = new boolean[]{autoIncrement};
  59. this.mapper = mapper;
  60. resultSetReader = new JDBCResultSetReader[]{resultReader};
  61. this.paramSetter = new JDBCParameterSetter[]{paramSetter};
  62. }
  63. public String[] getColumnNames() {
  64. return columnNames;
  65. }
  66. public Class[] getJavaTypes() {
  67. return javaTypes;
  68. }
  69. public int[] getJDBCTypes() {
  70. return jdbcTypes;
  71. }
  72. public String[] getSQLTypes() {
  73. return sqlTypes;
  74. }
  75. public boolean[] getNotNull() {
  76. return notNull;
  77. }
  78. public boolean[] getAutoIncrement() {
  79. return autoIncrement;
  80. }
  81. public Object getColumnValue(int index, Object value) {
  82. if (index != 0) {
  83. throw MESSAGES.simpleTypeRequiresOneIndex();
  84. }
  85. return mapper == null ? value : mapper.toColumnValue(value);
  86. }
  87. public Object setColumnValue(int index, Object value, Object columnValue) {
  88. if (index != 0) {
  89. throw MESSAGES.simpleTypeRequiresOneIndex();
  90. }
  91. return mapper == null ? columnValue : mapper.toFieldValue(columnValue);
  92. }
  93. public boolean hasMapper() {
  94. return mapper != null;
  95. }
  96. public boolean isSearchable() {
  97. int jdbcType = jdbcTypes[0];
  98. return jdbcType != Types.BINARY &&
  99. jdbcType != Types.BLOB &&
  100. jdbcType != Types.CLOB &&
  101. jdbcType != Types.LONGVARBINARY &&
  102. jdbcType != Types.LONGVARCHAR &&
  103. jdbcType != Types.VARBINARY;
  104. }
  105. public JDBCResultSetReader[] getResultSetReaders() {
  106. return resultSetReader;
  107. }
  108. public JDBCParameterSetter[] getParameterSetter() {
  109. return paramSetter;
  110. }
  111. }