1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2008, Red Hat Middleware LLC, 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.ejb.plugins.cmp.jdbc;
23
24 import java.sql.Types;
25
26
27 /**
28 * This class provides a simple mapping of a Java type type to a single column.
29 *
30 * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
31 * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
32 * @version $Revision: 81030 $
33 */
34 public final class JDBCTypeSimple implements JDBCType
35 {
36 private final String[] columnNames;
37 private final Class[] javaTypes;
38 private final int[] jdbcTypes;
39 private final String[] sqlTypes;
40 private final boolean[] notNull;
41 private final boolean[] autoIncrement;
42 private final JDBCResultSetReader[] resultSetReader;
43 private final JDBCParameterSetter[] paramSetter;
44
45 private final Mapper mapper;
46
47 public JDBCTypeSimple(
48 String columnName,
49 Class javaType,
50 int jdbcType,
51 String sqlType,
52 boolean notNull,
53 boolean autoIncrement,
54 Mapper mapper,
55 JDBCParameterSetter paramSetter,
56 JDBCResultSetReader resultReader
57 )
58 {
59 columnNames = new String[]{columnName};
60 javaTypes = new Class[]{javaType};
61 jdbcTypes = new int[]{jdbcType};
62 sqlTypes = new String[]{sqlType};
63 this.notNull = new boolean[]{notNull};
64 this.autoIncrement = new boolean[]{autoIncrement};
65 this.mapper = mapper;
66 resultSetReader = new JDBCResultSetReader[]{resultReader};
67 this.paramSetter = new JDBCParameterSetter[]{paramSetter};
68 }
69
70 public final String[] getColumnNames()
71 {
72 return columnNames;
73 }
74
75 public final Class[] getJavaTypes()
76 {
77 return javaTypes;
78 }
79
80 public final int[] getJDBCTypes()
81 {
82 return jdbcTypes;
83 }
84
85 public final String[] getSQLTypes()
86 {
87 return sqlTypes;
88 }
89
90 public final boolean[] getNotNull()
91 {
92 return notNull;
93 }
94
95 public final boolean[] getAutoIncrement()
96 {
97 return autoIncrement;
98 }
99
100 public final Object getColumnValue(int index, Object value)
101 {
102 if(index != 0)
103 {
104 throw new IndexOutOfBoundsException("JDBCSimpleType does not support an index>0.");
105 }
106 return mapper == null ? value : mapper.toColumnValue(value);
107 }
108
109 public final Object setColumnValue(int index, Object value, Object columnValue)
110 {
111 if(index != 0)
112 {
113 throw new IndexOutOfBoundsException("JDBCSimpleType does not support an index>0.");
114 }
115 return mapper == null ? columnValue : mapper.toFieldValue(columnValue);
116 }
117
118 public boolean hasMapper()
119 {
120 return mapper != null;
121 }
122
123 public boolean isSearchable()
124 {
125 int jdbcType = jdbcTypes[0];
126 return jdbcType != Types.BINARY &&
127 jdbcType != Types.BLOB &&
128 jdbcType != Types.CLOB &&
129 jdbcType != Types.LONGVARBINARY &&
130 jdbcType != Types.LONGVARCHAR &&
131 jdbcType != Types.VARBINARY;
132 }
133
134 public final JDBCResultSetReader[] getResultSetReaders()
135 {
136 return resultSetReader;
137 }
138
139 public JDBCParameterSetter[] getParameterSetter()
140 {
141 return paramSetter;
142 }
143 }