1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.types.DataTypeDescriptor; 25 import org.apache.derby.iapi.types.DataValueDescriptor; 26 import org.apache.derby.iapi.types.TypeId; 27 import org.apache.derby.iapi.types.BitDataValue; 28 import org.apache.derby.iapi.types.DataValueDescriptor; 29 import org.apache.derby.iapi.reference.SQLState; 30 import org.apache.derby.iapi.reference.Limits; 31 import org.apache.derby.iapi.error.StandardException; 32 33 import org.apache.derby.iapi.types.Orderable; 34 35 import org.apache.derby.iapi.services.io.FormatIdUtil; 36 import org.apache.derby.iapi.services.io.StoredFormatIds; 37 38 import org.apache.derby.iapi.services.sanity.SanityManager; 39 40 import org.apache.derby.iapi.types.BooleanDataValue; 41 import org.apache.derby.iapi.types.StringDataValue; 42 import org.apache.derby.iapi.types.NumberDataValue; 43 44 import org.apache.derby.iapi.services.io.FormatableBitSet; 45 import org.apache.derby.iapi.util.StringUtil; 46 47 import java.io.ObjectOutput ; 48 import java.io.ObjectInput ; 49 import java.io.IOException ; 50 51 56 public class SQLVarbit extends SQLBit 57 { 58 59 60 public String getTypeName() 61 { 62 return TypeId.VARBIT_NAME; 63 } 64 65 68 int getMaxMemoryUsage() 69 { 70 return Limits.DB2_VARCHAR_MAXWIDTH; 71 } 72 73 76 public DataValueDescriptor getNewNull() 77 { 78 return new SQLVarbit(); 79 } 80 81 86 public int getTypeFormatId() 87 { 88 return StoredFormatIds.SQL_VARBIT_ID; 89 } 90 91 103 104 public void normalize( 105 DataTypeDescriptor desiredType, 106 DataValueDescriptor source) 107 throws StandardException 108 { 109 int desiredWidth = desiredType.getMaximumWidth(); 110 111 byte[] sourceData = source.getBytes(); 112 setValue(sourceData); 113 if (sourceData.length > desiredWidth) 114 setWidth(desiredWidth, 0, true); 115 } 116 117 130 public void setWidth(int desiredWidth, 131 int desiredScale, boolean errorOnTrunc) 133 throws StandardException 134 { 135 138 if (getValue() == null) 139 { 140 return; 141 } 142 143 int sourceWidth = dataValue.length; 144 145 if (sourceWidth > desiredWidth) 146 { 147 if (errorOnTrunc) 148 { 149 for (int i = desiredWidth; i < dataValue.length; i++) { 151 152 if (dataValue[i] != SQLBinary.PAD) 153 throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), 154 StringUtil.formatForPrint(this.toString()), 155 String.valueOf(desiredWidth)); 156 } 157 } 158 164 167 byte[] shrunkData = new byte[desiredWidth]; 168 System.arraycopy(dataValue, 0, shrunkData, 0, desiredWidth); 169 dataValue = shrunkData; 170 171 } 172 } 173 174 175 178 179 180 183 184 187 public SQLVarbit() 188 { 189 } 190 191 public SQLVarbit(byte[] val) 192 { 193 super(val); 194 } 195 196 199 200 201 public int typePrecedence() 202 { 203 return TypeId.VARBIT_PRECEDENCE; 204 } 205 } 206 | Popular Tags |