1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 import org.apache.derby.iapi.reference.Limits; 26 27 import org.apache.derby.iapi.services.io.ArrayInputStream; 28 29 import org.apache.derby.iapi.types.DataTypeDescriptor; 30 import org.apache.derby.iapi.types.DataValueDescriptor; 31 import org.apache.derby.iapi.types.TypeId; 32 import org.apache.derby.iapi.types.BitDataValue; 33 import org.apache.derby.iapi.types.DataValueDescriptor; 34 import org.apache.derby.iapi.types.ConcatableDataValue; 35 import org.apache.derby.iapi.types.VariableSizeDataValue; 36 import org.apache.derby.iapi.error.StandardException; 37 38 import org.apache.derby.iapi.services.io.FormatIdUtil; 39 import org.apache.derby.iapi.services.io.StoredFormatIds; 40 import org.apache.derby.iapi.services.io.StreamStorable; 41 import org.apache.derby.iapi.services.io.FormatIdInputStream; 42 43 import org.apache.derby.iapi.services.sanity.SanityManager; 44 45 import org.apache.derby.iapi.types.BooleanDataValue; 46 import org.apache.derby.iapi.types.StringDataValue; 47 import org.apache.derby.iapi.types.NumberDataValue; 48 49 import org.apache.derby.iapi.services.cache.ClassSize; 50 import org.apache.derby.iapi.util.StringUtil; 51 52 import org.apache.derby.iapi.types.SQLInteger; 53 54 import java.io.ObjectOutput ; 55 import java.io.ObjectInput ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 59 import java.sql.ResultSet ; 60 import java.sql.SQLException ; 61 62 65 public class SQLBit 66 extends SQLBinary 67 { 68 69 73 public Object getObject() throws StandardException 74 { 75 return getBytes(); 76 } 77 78 79 public String getTypeName() 80 { 81 return TypeId.BIT_NAME; 82 } 83 84 87 int getMaxMemoryUsage() 88 { 89 return Limits.DB2_CHAR_MAXWIDTH; 90 } 91 92 95 96 101 public int getTypeFormatId() 102 { 103 return StoredFormatIds.SQL_BIT_ID; 104 } 105 106 107 108 public DataValueDescriptor getNewNull() 109 { 110 return new SQLBit(); 111 } 112 113 122 public final void setValueFromResultSet(ResultSet resultSet, int colNumber, 123 boolean isNullable) 124 throws SQLException 125 { 126 setValue(resultSet.getBytes(colNumber)); 127 } 128 129 132 133 134 public int typePrecedence() 135 { 136 return TypeId.BIT_PRECEDENCE; 137 } 138 139 142 final void setObject(Object theValue) 143 throws StandardException 144 { 145 setValue((byte[]) theValue); 146 } 147 148 151 152 155 public SQLBit() 156 { 157 } 158 159 public SQLBit(byte[] val) 160 { 161 dataValue = val; 162 } 163 164 176 177 public void normalize( 178 DataTypeDescriptor desiredType, 179 DataValueDescriptor source) 180 throws StandardException 181 { 182 int desiredWidth = desiredType.getMaximumWidth(); 183 184 ((SQLBinary) this).setValue(source.getBytes()); 185 setWidth(desiredWidth, 0, true); 186 } 187 188 201 public void setWidth(int desiredWidth, 202 int desiredScale, boolean errorOnTrunc) 204 throws StandardException 205 { 206 209 if (getValue() == null) 210 { 211 return; 212 } 213 214 int sourceWidth = dataValue.length; 215 216 220 if (sourceWidth < desiredWidth) 221 { 222 byte[] actualData = new byte[desiredWidth]; 223 System.arraycopy(dataValue, 0, actualData, 0, dataValue.length); 224 java.util.Arrays.fill(actualData, dataValue.length, actualData.length, SQLBinary.PAD); 225 dataValue = actualData; 226 } 227 230 else if (sourceWidth > desiredWidth) 231 { 232 if (errorOnTrunc) 233 { 234 for (int i = desiredWidth; i < dataValue.length; i++) { 236 237 if (dataValue[i] != SQLBinary.PAD) 238 throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), 239 StringUtil.formatForPrint(this.toString()), 240 String.valueOf(desiredWidth)); 241 } 242 } 243 249 252 byte[] shrunkData = new byte[desiredWidth]; 253 System.arraycopy(dataValue, 0, shrunkData, 0, desiredWidth); 254 dataValue = shrunkData; 255 256 } 257 } 258 259 260 261 262 263 } 264 | Popular Tags |