📄 sqlchar.java
字号:
} public void setValue(double theValue) throws StandardException { setValue(Double.toString(theValue)); } public void setValue(float theValue) throws StandardException { setValue(Float.toString(theValue)); } public void setValue(short theValue) throws StandardException { setValue(Short.toString(theValue)); } public void setValue(long theValue) throws StandardException { setValue(Long.toString(theValue)); } public void setValue(byte theValue) throws StandardException { setValue(Byte.toString(theValue)); } public void setValue(byte[] theValue) throws StandardException { if (theValue == null) { restoreToNull(); return; } /* ** We can't just do a new String(theValue) ** because that method assumes we are converting ** ASCII and it will take on char per byte. ** So we need to convert the byte array to a ** char array and go from there. ** ** If we have an odd number of bytes pad out. */ int mod = (theValue.length % 2); int len = (theValue.length/2) + mod; char[] carray = new char[len]; int cindex = 0; int bindex = 0; /* ** If we have a left over byte, then get ** that now. */ if (mod == 1) { carray[--len] = (char)(theValue[theValue.length - 1] << 8); } for (; cindex < len; bindex+=2, cindex++) { carray[cindex] = (char)((theValue[bindex] << 8) | (theValue[bindex+1] & 0x00ff)); } setValue(new String(carray)); } /** Only to be called when an application through JDBC is setting a SQLChar to a java.math.BigDecimal. */ public void setBigDecimal(Number bigDecimal) throws StandardException { setValue((Object) bigDecimal); } /** @exception StandardException Thrown on error */ public void setValue(Date theValue, Calendar cal) throws StandardException { String strValue = null; if( theValue != null) { if( cal == null) strValue = theValue.toString(); else { cal.setTime( theValue); StringBuffer sb = new StringBuffer(); formatJDBCDate( cal, sb); strValue= sb.toString(); } } setValue( strValue); } /** @exception StandardException Thrown on error */ public void setValue(Time theValue, Calendar cal) throws StandardException { String strValue = null; if( theValue != null) { if( cal == null) strValue = theValue.toString(); else { cal.setTime( theValue); StringBuffer sb = new StringBuffer(); formatJDBCTime( cal, sb); strValue= sb.toString(); } } setValue( strValue); } /** @exception StandardException Thrown on error */ public void setValue(Timestamp theValue, Calendar cal) throws StandardException { String strValue = null; if( theValue != null) { if( cal == null) strValue = theValue.toString(); else { cal.setTime( theValue); StringBuffer sb = new StringBuffer(); formatJDBCDate( cal, sb); sb.append( ' '); formatJDBCTime( cal, sb); int micros = (theValue.getNanos() + SQLTimestamp.FRACTION_TO_NANO/2)/SQLTimestamp.FRACTION_TO_NANO; if( micros > 0) { sb.append( '.'); String microsStr = Integer.toString( micros); if( microsStr.length() > SQLTimestamp.MAX_FRACTION_DIGITS) sb.append( microsStr.substring( 0, SQLTimestamp.MAX_FRACTION_DIGITS)); else { for( int i = microsStr.length(); i < SQLTimestamp.MAX_FRACTION_DIGITS ; i++) sb.append( '0'); sb.append( microsStr); } } strValue= sb.toString(); } } setValue( strValue); } private void formatJDBCDate( Calendar cal, StringBuffer sb) { SQLDate.dateToString( cal.get( Calendar.YEAR), cal.get( Calendar.MONTH) - Calendar.JANUARY + 1, cal.get( Calendar.DAY_OF_MONTH), sb); } private void formatJDBCTime( Calendar cal, StringBuffer sb) { SQLTime.timeToString( cal.get( Calendar.HOUR), cal.get( Calendar.MINUTE), cal.get( Calendar.SECOND), sb); } /** * @see SQLChar#setValue * */ public void setValue(InputStream theStream, int streamLength) { value = null; rawLength = -1; stream = theStream; // clear out the int array as well, so it will stay current intArray = null; intLength = 0; cKey = null; } /** * @see DataValueDescriptor#setValue */ public void setValue(Object theValue) throws StandardException { if ((theValue instanceof String) || (theValue == null)) { setValue((String) theValue); } else { { setValue(theValue.toString()); } } } protected void setFrom(DataValueDescriptor theValue) throws StandardException { setValue(theValue.getString()); } private void setAsToNationalString(Object theValue) { String s = null; if (theValue != null) s = theValue.toString(); setValue(s); } /** * Normalization method - this method may be called when putting * a value into a SQLChar, for example, when inserting into a SQLChar * column. See NormalizeResultSet in execution. * * @param desiredType The type to normalize the source column to * @param source The value to normalize * @param cachedDest DataValueDescriptor, if non-null, to hold result * (Reuse if normalizing multiple rows) * * @return The normalized SQLChar * * @exception StandardException Thrown for null into * non-nullable column, and for * truncation error */ public void normalize( DataTypeDescriptor desiredType, DataValueDescriptor source) throws StandardException { normalize(desiredType, source.getString()); } protected void normalize(DataTypeDescriptor desiredType, String sourceValue) throws StandardException { int desiredWidth = desiredType.getMaximumWidth(); int sourceWidth = sourceValue.length(); /* ** If the input is already the right length, no normalization is ** necessary - just return the source. */ if (sourceWidth == desiredWidth) { setValue(sourceValue); return; } /* ** If the input is shorter than the desired type, construct a new ** SQLChar padded with blanks to the right length. */ if (sourceWidth < desiredWidth) { setToNull(); char[] ca; if ((rawData == null) || (desiredWidth > rawData.length)) { ca = rawData = new char[desiredWidth]; } else { ca = rawData; } sourceValue.getChars(0, sourceWidth, ca, 0); SQLChar.appendBlanks(ca, sourceWidth, desiredWidth - sourceWidth); rawLength = desiredWidth; return; } /* ** Check whether any non-blank characters will be truncated. */ hasNonBlankChars(sourceValue, desiredWidth, sourceWidth); /* ** No non-blank characters will be truncated. Truncate the blanks ** to the desired width. */ String truncatedString = sourceValue.substring(0, desiredWidth); setValue(truncatedString); } /* ** Method to check for truncation of non blank chars. */ protected final void hasNonBlankChars(String source, int start, int end) throws StandardException { /* ** Check whether any non-blank characters will be truncated. */ for (int posn = start; posn < end; posn++) { if (source.charAt(posn) != ' ') { throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), StringUtil.formatForPrint(source), String.valueOf(start)); } } } /////////////////////////////////////////////////////////////// // // VariableSizeDataValue INTERFACE // /////////////////////////////////////////////////////////////// /** * Set the width of the to the desired value. Used * when CASTing. Ideally we'd recycle normalize(), but * the behavior is different (we issue a warning instead * of an error, and we aren't interested in nullability). * * @param desiredWidth the desired length * @param desiredScale the desired scale (ignored) * @param errorOnTrunc throw an error on truncation * @return this with the new width * * @exception StandardException Thrown when errorOnTrunc * is true and when a shrink will truncate non-white * spaces. */ public DataValueDescriptor setWidth(int desiredWidth, int desiredScale, // Ignored boolean errorOnTrunc) throws StandardException { int sourceWidth; /* ** If the input is NULL, nothing to do. */ if (getString() == null) { return this; } sourceWidth = getLength(); /* ** If the input is shorter than the desired type, construct a new ** SQLChar padded with blanks to the right length. Only ** do this if we have a SQLChar -- SQLVarchars don't ** pad. */ if (sourceWidth < desiredWidth) { if (!(this instanceof SQLVarchar)) { StringBuffer strbuf; strbuf = new StringBuffer(getString()); for ( ; sourceWidth < desiredWidth; sourceWidth++) { strbuf.append(' '); } setValue(new String(strbuf)); } } else if (sourceWidth > desiredWidth && desiredWidth > 0) { /* ** Check whether any non-blank characters will be truncated. */ if (errorOnTrunc) hasNonBlankChars(getString(), desiredWidth, sourceWidth); //RESOLVE: should issue a warning instead /* ** Truncate to the desired width. */ setValue(getString().substring(0, desiredWidth)); } return this; } /* ** SQL Operators */ /** * The = operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the = * @param right The value on the right side of the = * * @return A SQL boolean value telling whether the two parameters are equal * * @exception StandardException Thrown on error */ public BooleanDataValue equals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean comparison; if ((left instanceof SQLChar) && (right instanceof SQLChar)) { comparison = stringCompare((SQLChar) left, (SQLChar) right) == 0; } else { comparison = stringCompare(left.getString(), right.getString()) == 0; } return SQLBoolean.truthValue(left, right, comparison); } /** * The <> operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the <> * @param right The value on the right side of the <> * * @return A SQL boolean value telling whether the two parameters * are not equal * * @exception StandardException Thrown on error */ public BooleanDataValue notEquals(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean comparison; if ((left instanceof SQLChar) && (right instanceof SQLChar)) { comparison = stringCompare((SQLChar) left, (SQLChar) right) != 0; } else { comparison = stringCompare(left.getString(), right.getString()) != 0; } return SQLBoolean.truthValue(left, right, comparison); } /** * The < operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the < * @param right The value on the right side of the < * * @return A SQL boolean value telling whether the first operand is * less than the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue lessThan(DataValueDescriptor left, DataValueDescriptor right) throws StandardException { boolean comparison; if ((left instanceof SQLChar) && (right instanceof SQLChar)) { comparison = stringCompare((SQLChar) left, (SQLChar) right) < 0; } else { comparison = stringCompare(left.getString(), right.getString()) < 0; } return SQLBoolean.truthValue(left, right, comparison); } /** * The > operator as called from the language module, as opposed to * the storage module. * * @param left The value on the left side of the > * @param right The value on the right side of the > * * @return A SQL boolean value telling whether the first operand is * greater than the second operand * * @exception StandardException Thrown on error */ public BooleanDataValue greaterThan(DataValueDescriptor left, DataValueDescriptor right) throws StandardException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -