001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.catalog.SystemColumnImpl
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.catalog;
023:
024: import java.sql.Types;
025:
026: import org.apache.derby.iapi.sql.dictionary.SystemColumn;
027: import org.apache.derby.iapi.error.StandardException;
028: import org.apache.derby.iapi.types.DataTypeDescriptor;
029: import org.apache.derby.iapi.types.DataValueDescriptor;
030: import org.apache.derby.iapi.types.DataValueFactory;
031: import org.apache.derby.iapi.types.TypeId;
032:
033: /**
034: * Implements the description of a column in a system table.
035: *
036: *
037: * @version 0.1
038: * @author Rick Hillegas
039: */
040:
041: class SystemColumnImpl implements SystemColumn {
042: private final String name;
043:
044: /**
045: * Fully described type of the column.
046: */
047: private final DataTypeDescriptor type;
048:
049: /**
050: * Create a system column for a builtin type.
051: *
052: * @param name
053: * name of column
054: * @param jdbcTypeId
055: * JDBC type id from java.sql.Types
056: * @param nullability
057: * Whether or not column accepts nulls.
058: */
059: static SystemColumn getColumn(String name, int jdbcTypeId,
060: boolean nullability) {
061: return new SystemColumnImpl(name, DataTypeDescriptor
062: .getBuiltInDataTypeDescriptor(jdbcTypeId, nullability));
063: }
064:
065: /**
066: * Create a system column for a builtin type.
067: *
068: * @param name
069: * name of column
070: * @param jdbcTypeId
071: * JDBC type id from java.sql.Types
072: * @param nullability
073: * Whether or not column accepts nulls.
074: */
075: static SystemColumn getColumn(String name, int jdbcTypeId,
076: boolean nullability, int maxLength) {
077: return new SystemColumnImpl(name, DataTypeDescriptor
078: .getBuiltInDataTypeDescriptor(jdbcTypeId, nullability,
079: maxLength));
080: }
081:
082: /**
083: * Create a system column for an identifer with consistent type of
084: * VARCHAR(128)
085: *
086: * @param name
087: * Name of the column.
088: * @param nullability
089: * Nullability of the column.
090: * @return Object representing the column.
091: */
092: static SystemColumn getIdentifierColumn(String name,
093: boolean nullability) {
094: return new SystemColumnImpl(name, DataTypeDescriptor
095: .getBuiltInDataTypeDescriptor(Types.VARCHAR,
096: nullability, 128));
097: }
098:
099: /**
100: * Create a system column for a character representation of a UUID with
101: * consistent type of CHAR(36)
102: *
103: * @param name
104: * Name of the column.
105: * @param nullability
106: * Nullability of the column.
107: * @return Object representing the column.
108: */
109: static SystemColumn getUUIDColumn(String name, boolean nullability) {
110: return new SystemColumnImpl(name, DataTypeDescriptor
111: .getBuiltInDataTypeDescriptor(Types.CHAR, nullability,
112: 36));
113: }
114:
115: /**
116: * Create a system column for a character representation of an indicator
117: * column with consistent type of CHAR(1) NOT NULL
118: *
119: * @param name
120: * Name of the column.
121: * @return Object representing the column.
122: */
123: static SystemColumn getIndicatorColumn(String name) {
124: return new SystemColumnImpl(name, DataTypeDescriptor
125: .getBuiltInDataTypeDescriptor(Types.CHAR, false, 1));
126: }
127:
128: /**
129: * Create a system column for a java column.
130: *
131: * @param name
132: * Name of the column.
133: * @param javaClassName
134: * @param nullability
135: * Nullability of the column.
136: * @return Object representing the column.
137: */
138: static SystemColumn getJavaColumn(String name,
139: String javaClassName, boolean nullability) {
140:
141: TypeId typeId = TypeId.getUserDefinedTypeId(javaClassName,
142: false);
143:
144: DataTypeDescriptor dtd = new DataTypeDescriptor(typeId,
145: nullability);
146: return new SystemColumnImpl(name, dtd);
147: }
148:
149: /**
150: * Create a SystemColumnImpl representing the given name and type.
151: */
152: private SystemColumnImpl(String name, DataTypeDescriptor type) {
153: this .name = name;
154: this .type = type;
155: }
156:
157: /**
158: * Constructor to create a description of a column in a system table.
159: *
160: * @param name
161: * of column.
162: * @param id
163: * of column.
164: * @param nullability
165: * Whether or not column accepts nulls.
166: * @param dataType
167: * Datatype of column.
168: * @param maxLength
169: * Maximum length of data in column.
170: */
171: SystemColumnImpl(String name, int id, boolean nullability,
172: String dataType, boolean builtInType, int maxLength) {
173: this .name = name;
174:
175: TypeId typeId;
176:
177: if (builtInType) {
178: typeId = TypeId.getBuiltInTypeId(dataType);
179: } else {
180:
181: typeId = TypeId.getUserDefinedTypeId(dataType, false);
182: }
183:
184: this .type = new DataTypeDescriptor(typeId, 0, 0, nullability,
185: maxLength);
186: }
187:
188: SystemColumnImpl(String name, int id, int ignoreP, int ignoreS,
189: boolean nullability, String dataType, boolean builtInType,
190: int maxLength) {
191: this (name, id, nullability, dataType, builtInType, maxLength);
192: }
193:
194: /**
195: * Constructor to create a description of a column in a system table.
196: * This constructor is used for SQL Identifiers (varchar 128).
197: *
198: * @param name of column.
199: * @param id of column.
200: * @param nullability Whether or not column accepts nulls.
201: */
202: SystemColumnImpl(String name, int id, boolean nullability) {
203: this (name, id, nullability, "VARCHAR", true, 128);
204: }
205:
206: /**
207: * Gets the name of this column.
208: *
209: * @return The column name.
210: */
211: public String getName() {
212: return name;
213: }
214:
215: /**
216: * Return the type of this column.
217: */
218: public DataTypeDescriptor getType() {
219: return type;
220: }
221: }
|