Skip to main content
summaryrefslogtreecommitdiffstats
blob: 96fd283a7d614eac0acf593983bc6066b3f14424 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2005, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.Types;

/**
 * Associate the Java constant and the JDBC type name.
 * These are derived from java.sql.Types.
 * 
 * @see java.sql.Types
 */
public final class JDBCType
	implements Cloneable, Serializable
{

	/**
	 * the constant name (e.g. VARCHAR)
	 */
	private final String name;

	/**
	 * the JDBC code used by JDBC drivers
	 */
	private final int code;

	private static final long serialVersionUID = 1L;


	// ********** constructors **********

	/**
	 * Construct a JDBC type with the specified name and type code.
	 * This is private because all the possible JDBC types are built and
	 * stored in the static array TYPES.
	 * @see #types()
	 */
	private JDBCType(String name, int code) {
		super();
		this.name = name;
		this.code = code;
	}


	// ********** accessors **********

	/**
	 * Return the name of the type, as defined in java.sql.Types.
	 */
	public String name() {
		return this.name;
	}


	/**
	 * Return the type code, as defined in java.sql.Types.
	 */
	public int code() {
		return this.code;
	}


	// ********** printing and displaying **********

	public void appendTo(StringBuilder sb) {
		sb.append(this.name);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.getClass().getSimpleName());
		sb.append('(');
		this.appendTo(sb);
		sb.append(')');
		return sb.toString();
	}

	@Override
	public JDBCType clone() {
		try {
			return (JDBCType) super.clone();
		} catch (CloneNotSupportedException ex) {
			throw new InternalError();
		}
	}


	// ********** static stuff **********

	/**
	 * all the JDBC type defined in java.sql.Types
	 */
	private static JDBCType[] TYPES;		// pseudo 'final' - lazy-initialized


	public synchronized static JDBCType[] types() {
		if (TYPES == null) {
			TYPES = buildTypes();
		}
		return TYPES;
	}

	/**
	 * Return the JDBC type for the specified type code (e.g. Types.VARCHAR).
	 * @see java.sql.Types
	 */
	public static JDBCType type(int code) {
		JDBCType[] types = types();
		for (int i = types.length; i-- > 0; ) {
			if (types[i].code() == code) {
				return types[i];
			}
		}
		throw new IllegalArgumentException("invalid JDBC type code: " + code); //$NON-NLS-1$
	}

	/**
	 * Return the JDBC type for the specified type name (e.g. "VARCHAR").
	 * @see java.sql.Types
	 */
	public static JDBCType type(String name) {
		JDBCType[] types = types();
		for (int i = types.length; i-- > 0; ) {
			if (types[i].name().equals(name)) {
				return types[i];
			}
		}
		throw new IllegalArgumentException("invalid JDBC type name: " + name); //$NON-NLS-1$
	}

	/**
	 * build up the JDBC types via reflection
	 * @see java.sql.Types
	 */
	private static JDBCType[] buildTypes() {
		Field[] fields = Types.class.getDeclaredFields();
		int len = fields.length;
		JDBCType[] types = new JDBCType[len];
		for (int i = len; i-- > 0; ) {
			String name = fields[i].getName();
			int code;
			try {
				code = ((Integer) fields[i].get(null)).intValue();
			} catch (IllegalAccessException ex) {
				throw new RuntimeException(ex);	// shouldn't happen...
			}
			types[i] = new JDBCType(name, code);
		}
		return types;
	}

}

Back to the top