PageRenderTime 54ms CodeModel.GetById 26ms app.highlight 20ms RepoModel.GetById 1ms 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 */
 22package org.jboss.as.cmp.jdbc;
 23
 24import java.sql.Types;
 25import static org.jboss.as.cmp.CmpMessages.MESSAGES;
 26
 27
 28/**
 29 * This class provides a simple mapping of a Java type type to a single column.
 30 *
 31 * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
 32 * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
 33 * @version $Revision: 81030 $
 34 */
 35public final class JDBCTypeSimple implements JDBCType {
 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        columnNames = new String[]{columnName};
 59        javaTypes = new Class[]{javaType};
 60        jdbcTypes = new int[]{jdbcType};
 61        sqlTypes = new String[]{sqlType};
 62        this.notNull = new boolean[]{notNull};
 63        this.autoIncrement = new boolean[]{autoIncrement};
 64        this.mapper = mapper;
 65        resultSetReader = new JDBCResultSetReader[]{resultReader};
 66        this.paramSetter = new JDBCParameterSetter[]{paramSetter};
 67    }
 68
 69    public String[] getColumnNames() {
 70        return columnNames;
 71    }
 72
 73    public Class[] getJavaTypes() {
 74        return javaTypes;
 75    }
 76
 77    public int[] getJDBCTypes() {
 78        return jdbcTypes;
 79    }
 80
 81    public String[] getSQLTypes() {
 82        return sqlTypes;
 83    }
 84
 85    public boolean[] getNotNull() {
 86        return notNull;
 87    }
 88
 89    public boolean[] getAutoIncrement() {
 90        return autoIncrement;
 91    }
 92
 93    public Object getColumnValue(int index, Object value) {
 94        if (index != 0) {
 95            throw MESSAGES.simpleTypeRequiresOneIndex();
 96        }
 97        return mapper == null ? value : mapper.toColumnValue(value);
 98    }
 99
100    public Object setColumnValue(int index, Object value, Object columnValue) {
101        if (index != 0) {
102            throw MESSAGES.simpleTypeRequiresOneIndex();
103        }
104        return mapper == null ? columnValue : mapper.toFieldValue(columnValue);
105    }
106
107    public boolean hasMapper() {
108        return mapper != null;
109    }
110
111    public boolean isSearchable() {
112        int jdbcType = jdbcTypes[0];
113        return jdbcType != Types.BINARY &&
114                jdbcType != Types.BLOB &&
115                jdbcType != Types.CLOB &&
116                jdbcType != Types.LONGVARBINARY &&
117                jdbcType != Types.LONGVARCHAR &&
118                jdbcType != Types.VARBINARY;
119    }
120
121    public JDBCResultSetReader[] getResultSetReaders() {
122        return resultSetReader;
123    }
124
125    public JDBCParameterSetter[] getParameterSetter() {
126        return paramSetter;
127    }
128}