001: /*
002:
003: Derby - Class org.apache.derby.iapi.types.SQLVarbit
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.iapi.types;
023:
024: import org.apache.derby.iapi.types.DataTypeDescriptor;
025: import org.apache.derby.iapi.types.DataValueDescriptor;
026: import org.apache.derby.iapi.types.TypeId;
027: import org.apache.derby.iapi.types.BitDataValue;
028: import org.apache.derby.iapi.types.DataValueDescriptor;
029: import org.apache.derby.iapi.reference.SQLState;
030: import org.apache.derby.iapi.reference.Limits;
031: import org.apache.derby.iapi.error.StandardException;
032:
033: import org.apache.derby.iapi.types.Orderable;
034:
035: import org.apache.derby.iapi.services.io.FormatIdUtil;
036: import org.apache.derby.iapi.services.io.StoredFormatIds;
037:
038: import org.apache.derby.iapi.services.sanity.SanityManager;
039:
040: import org.apache.derby.iapi.types.BooleanDataValue;
041: import org.apache.derby.iapi.types.StringDataValue;
042: import org.apache.derby.iapi.types.NumberDataValue;
043:
044: import org.apache.derby.iapi.services.io.FormatableBitSet;
045: import org.apache.derby.iapi.util.StringUtil;
046:
047: import java.io.ObjectOutput;
048: import java.io.ObjectInput;
049: import java.io.IOException;
050:
051: /**
052: * SQLVarbit represents the SQL type VARCHAR FOR BIT DATA
053: * It is an extension of SQLBit and is virtually indistinguishable
054: * other than normalization.
055: */
056: public class SQLVarbit extends SQLBit {
057:
058: public String getTypeName() {
059: return TypeId.VARBIT_NAME;
060: }
061:
062: /**
063: * Return max memory usage for a SQL Varbit
064: */
065: int getMaxMemoryUsage() {
066: return Limits.DB2_VARCHAR_MAXWIDTH;
067: }
068:
069: /**
070: * @see DataValueDescriptor#getNewNull
071: */
072: public DataValueDescriptor getNewNull() {
073: return new SQLVarbit();
074: }
075:
076: /**
077: Return my format identifier.
078:
079: @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
080: */
081: public int getTypeFormatId() {
082: return StoredFormatIds.SQL_VARBIT_ID;
083: }
084:
085: /**
086: * Normalization method - this method may be called when putting
087: * a value into a SQLBit, for example, when inserting into a SQLBit
088: * column. See NormalizeResultSet in execution.
089: *
090: * @param desiredType The type to normalize the source column to
091: * @param source The value to normalize
092: *
093: * @exception StandardException Thrown for null into
094: * non-nullable column, and for
095: * truncation error
096: */
097:
098: public void normalize(DataTypeDescriptor desiredType,
099: DataValueDescriptor source) throws StandardException {
100: int desiredWidth = desiredType.getMaximumWidth();
101:
102: byte[] sourceData = source.getBytes();
103: setValue(sourceData);
104: if (sourceData.length > desiredWidth)
105: setWidth(desiredWidth, 0, true);
106: }
107:
108: /**
109: * Set the width of the to the desired value. Used
110: * when CASTing. Ideally we'd recycle normalize(), but
111: * the behavior is different (we issue a warning instead
112: * of an error, and we aren't interested in nullability).
113: *
114: * @param desiredWidth the desired length
115: * @param desiredScale the desired scale (ignored)
116: * @param errorOnTrunc throw error on truncation
117: *
118: * @exception StandardException Thrown on non-zero truncation
119: * if errorOnTrunc is true
120: */
121: public void setWidth(int desiredWidth, int desiredScale, // Ignored
122: boolean errorOnTrunc) throws StandardException {
123: /*
124: ** If the input is NULL, nothing to do.
125: */
126: if (getValue() == null) {
127: return;
128: }
129:
130: int sourceWidth = dataValue.length;
131:
132: if (sourceWidth > desiredWidth) {
133: if (errorOnTrunc) {
134: // error if truncating non pad characters.
135: for (int i = desiredWidth; i < dataValue.length; i++) {
136:
137: if (dataValue[i] != SQLBinary.PAD)
138: throw StandardException.newException(
139: SQLState.LANG_STRING_TRUNCATION,
140: getTypeName(),
141: StringUtil.formatForPrint(this
142: .toString()), String
143: .valueOf(desiredWidth));
144: }
145: }
146: //else
147: //{
148: // RESOLVE: when we have warnings, issue a warning if
149: // truncation of non-zero bits will occur
150: //}
151:
152: /*
153: ** Truncate to the desired width.
154: */
155: byte[] shrunkData = new byte[desiredWidth];
156: System.arraycopy(dataValue, 0, shrunkData, 0, desiredWidth);
157: dataValue = shrunkData;
158:
159: }
160: }
161:
162: /*
163: * Column interface
164: */
165:
166: /*
167: * class interface
168: */
169:
170: /*
171: * constructors
172: */
173: public SQLVarbit() {
174: }
175:
176: public SQLVarbit(byte[] val) {
177: super (val);
178: }
179:
180: /*
181: * DataValueDescriptor interface
182: */
183:
184: /** @see DataValueDescriptor#typePrecedence */
185: public int typePrecedence() {
186: return TypeId.VARBIT_PRECEDENCE;
187: }
188: }
|