001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.schema;
020:
021: import java.sql.Types;
022: import java.util.Date;
023:
024: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
025:
026: /**
027: * Helper class to deal with schemas.
028: *
029: * @author Abe White
030: * @nojavadoc
031: */
032: public class Schemas {
033:
034: public static final Column[] EMPTY_COLUMNS = new Column[0];
035: public static final ForeignKey[] EMPTY_FOREIGN_KEYS = new ForeignKey[0];
036: public static final Index[] EMPTY_INDEXES = new Index[0];
037: public static final Unique[] EMPTY_UNIQUES = new Unique[0];
038: public static final Object[] EMPTY_VALUES = new Object[0];
039:
040: /**
041: * Return the schema name that should be used for new tables, or null if
042: * none.
043: */
044: public static String getNewTableSchema(JDBCConfiguration conf) {
045: if (conf.getSchema() != null)
046: return conf.getSchema();
047:
048: String[] schemas = conf.getSchemasList();
049: if (schemas.length == 0)
050: return null;
051: int dotIdx = schemas[0].lastIndexOf('.');
052: if (dotIdx == 0)
053: return null;
054: if (dotIdx == -1)
055: return schemas[0];
056: return schemas[0].substring(0, dotIdx);
057: }
058:
059: /**
060: * Return the SQL type name for the given {@link Types} constant.
061: */
062: public static String getJDBCName(int type) {
063: switch (type) {
064: case Types.ARRAY:
065: return "array";
066: case Types.BIGINT:
067: return "bigint";
068: case Types.BINARY:
069: return "binary";
070: case Types.BIT:
071: return "bit";
072: case Types.BLOB:
073: return "blob";
074: case Types.CHAR:
075: return "char";
076: case Types.CLOB:
077: return "clob";
078: case Types.DATE:
079: return "date";
080: case Types.DECIMAL:
081: return "decimal";
082: case Types.DISTINCT:
083: return "distinct";
084: case Types.DOUBLE:
085: return "double";
086: case Types.FLOAT:
087: return "float";
088: case Types.INTEGER:
089: return "integer";
090: case Types.JAVA_OBJECT:
091: return "java_object";
092: case Types.LONGVARBINARY:
093: return "longvarbinary";
094: case Types.LONGVARCHAR:
095: return "longvarchar";
096: case Types.NULL:
097: return "null";
098: case Types.NUMERIC:
099: return "numeric";
100: case Types.OTHER:
101: return "other";
102: case Types.REAL:
103: return "real";
104: case Types.REF:
105: return "ref";
106: case Types.SMALLINT:
107: return "smallint";
108: case Types.STRUCT:
109: return "struct";
110: case Types.TIME:
111: return "time";
112: case Types.TIMESTAMP:
113: return "timestamp";
114: case Types.TINYINT:
115: return "tinyint";
116: case Types.VARBINARY:
117: return "varbinary";
118: case Types.VARCHAR:
119: return "varchar";
120: default:
121: return "unknown(" + type + ")";
122: }
123: }
124:
125: /**
126: * Return the {@link Types} constant for the given SQL type name.
127: */
128: public static int getJDBCType(String name) {
129: if ("array".equalsIgnoreCase(name))
130: return Types.ARRAY;
131: if ("bigint".equalsIgnoreCase(name))
132: return Types.BIGINT;
133: if ("binary".equalsIgnoreCase(name))
134: return Types.BINARY;
135: if ("bit".equalsIgnoreCase(name))
136: return Types.BIT;
137: if ("blob".equalsIgnoreCase(name))
138: return Types.BLOB;
139: if ("char".equalsIgnoreCase(name))
140: return Types.CHAR;
141: if ("clob".equalsIgnoreCase(name))
142: return Types.CLOB;
143: if ("date".equalsIgnoreCase(name))
144: return Types.DATE;
145: if ("decimal".equalsIgnoreCase(name))
146: return Types.DECIMAL;
147: if ("distinct".equalsIgnoreCase(name))
148: return Types.DISTINCT;
149: if ("double".equalsIgnoreCase(name))
150: return Types.DOUBLE;
151: if ("float".equalsIgnoreCase(name))
152: return Types.FLOAT;
153: if ("integer".equalsIgnoreCase(name))
154: return Types.INTEGER;
155: if ("java_object".equalsIgnoreCase(name))
156: return Types.JAVA_OBJECT;
157: if ("longvarbinary".equalsIgnoreCase(name))
158: return Types.LONGVARBINARY;
159: if ("longvarchar".equalsIgnoreCase(name))
160: return Types.LONGVARCHAR;
161: if ("null".equalsIgnoreCase(name))
162: return Types.NULL;
163: if ("numeric".equalsIgnoreCase(name))
164: return Types.NUMERIC;
165: if ("other".equalsIgnoreCase(name))
166: return Types.OTHER;
167: if ("real".equalsIgnoreCase(name))
168: return Types.REAL;
169: if ("ref".equalsIgnoreCase(name))
170: return Types.REF;
171: if ("smallint".equalsIgnoreCase(name))
172: return Types.SMALLINT;
173: if ("struct".equalsIgnoreCase(name))
174: return Types.STRUCT;
175: if ("time".equalsIgnoreCase(name))
176: return Types.TIME;
177: if ("timestamp".equalsIgnoreCase(name))
178: return Types.TIMESTAMP;
179: if ("tinyint".equalsIgnoreCase(name))
180: return Types.TINYINT;
181: if ("varbinary".equalsIgnoreCase(name))
182: return Types.VARBINARY;
183: if ("varchar".equalsIgnoreCase(name))
184: return Types.VARCHAR;
185: if (name == null || name.toLowerCase().startsWith("unknown"))
186: return Types.OTHER;
187: throw new IllegalArgumentException("name = " + name);
188: }
189:
190: /**
191: * Return the java type for the given SQL type from {@link Types}.
192: */
193: public static Class getJavaType(int type, int size, int decimals) {
194: switch (type) {
195: case Types.CHAR:
196: if (size == 1)
197: return char.class;
198: // no break
199: case Types.VARCHAR:
200: case Types.LONGVARCHAR:
201: case Types.CLOB:
202: return String.class;
203: case Types.BIT:
204: return boolean.class;
205: case Types.TINYINT:
206: return byte.class;
207: case Types.SMALLINT:
208: return short.class;
209: case Types.INTEGER:
210: return int.class;
211: case Types.BIGINT:
212: return long.class;
213: case Types.REAL:
214: case Types.FLOAT:
215: return float.class;
216: case Types.DOUBLE:
217: case Types.NUMERIC:
218: return double.class;
219: case Types.DECIMAL:
220: // oracle uses this for everything, so look at size and decimals
221: if (decimals == 0 && size < 10)
222: return int.class;
223: else if (decimals == 0)
224: return long.class;
225: return double.class;
226: // ### return a BigDecimal if the size if out of double range?
227: case Types.DATE:
228: case Types.TIME:
229: case Types.TIMESTAMP:
230: return Date.class;
231: default:
232: return Object.class;
233: }
234: }
235: }
|