1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.services.context.ContextService; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.services.io.Storable; 29 import org.apache.derby.iapi.services.io.StoredFormatIds; 30 import org.apache.derby.iapi.services.io.StreamStorable; 31 import org.apache.derby.iapi.services.io.FormatIdInputStream; 32 33 import org.apache.derby.iapi.types.DataTypeDescriptor; 34 import org.apache.derby.iapi.types.DataValueDescriptor; 35 import org.apache.derby.iapi.types.TypeId; 36 import org.apache.derby.iapi.types.StringDataValue; 37 import org.apache.derby.iapi.types.NumberDataValue; 38 import org.apache.derby.iapi.types.BooleanDataValue; 39 import org.apache.derby.iapi.types.ConcatableDataValue; 40 import org.apache.derby.iapi.reference.SQLState; 41 42 import org.apache.derby.iapi.error.StandardException; 43 44 import org.apache.derby.iapi.services.cache.ClassSize; 45 import org.apache.derby.iapi.services.io.ArrayInputStream; 46 import org.apache.derby.iapi.util.StringUtil; 47 import org.apache.derby.iapi.services.i18n.LocaleFinder; 48 49 import org.apache.derby.iapi.db.DatabaseContext; 50 51 import org.apache.derby.iapi.types.SQLInteger; 52 import org.apache.derby.iapi.types.SQLDate; 53 import org.apache.derby.iapi.types.SQLTime; 54 import org.apache.derby.iapi.types.SQLTimestamp; 55 56 import java.io.InputStream ; 57 import java.io.ObjectOutput ; 58 import java.io.ObjectInput ; 59 import java.io.IOException ; 60 import java.io.UTFDataFormatException ; 61 import java.io.EOFException ; 62 import java.sql.Date ; 63 import java.sql.ResultSet ; 64 import java.sql.PreparedStatement ; 65 import java.sql.SQLException ; 66 import java.sql.Time ; 67 import java.sql.Timestamp ; 68 import java.text.CollationElementIterator ; 69 import java.text.RuleBasedCollator ; 70 import java.text.CollationKey ; 71 import java.text.DateFormat ; 72 import java.util.Locale ; 73 import java.util.Calendar ; 74 75 86 public class SQLChar 87 extends DataType implements StringDataValue, StreamStorable 88 { 89 90 94 protected final static int RETURN_SPACE_THRESHOLD = 4096; 95 96 101 private final static int GROWBY_FOR_CHAR = 64; 102 105 private static final char[] BLANKS = new char[40]; 106 static { 107 for (int i = 0; i < BLANKS.length; i++) { 108 BLANKS[i] = ' '; 109 } 110 } 111 112 private static void appendBlanks(char[] ca, int offset, int howMany) { 113 while (howMany > 0) { 114 115 int count = howMany > BLANKS.length ? BLANKS.length : howMany; 116 117 System.arraycopy(BLANKS, 0, ca, offset, count); 118 howMany -= count; 119 offset += count; 120 } 121 } 122 128 129 134 public boolean getBoolean() 135 throws StandardException 136 { 137 if (isNull()) return false; 138 139 142 String cleanedValue = getString().trim(); 143 144 return !(cleanedValue.equals("0") || cleanedValue.equals("false")); 145 } 146 147 151 public byte getByte() throws StandardException 152 { 153 if (isNull()) return (byte)0; 154 try { 155 return Byte.parseByte(getString().trim()); 156 } catch (NumberFormatException nfe) { 157 throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "byte"); 158 } 159 } 160 161 165 public short getShort() throws StandardException 166 { 167 if (isNull()) return (short)0; 168 try { 169 return Short.parseShort(getString().trim()); 170 } catch (NumberFormatException nfe) { 171 throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "short"); 172 } 173 } 174 175 179 public int getInt() throws StandardException 180 { 181 if (isNull()) return 0; 182 try { 183 return Integer.parseInt(getString().trim()); 184 } catch (NumberFormatException nfe) { 185 throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "int"); 186 } 187 } 188 189 193 public long getLong() throws StandardException 194 { 195 if (isNull()) return 0; 196 try { 197 return Long.parseLong(getString().trim()); 198 } catch (NumberFormatException nfe) { 199 throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "long"); 200 } 201 } 202 203 207 public float getFloat() throws StandardException 208 { 209 if (isNull()) return 0; 210 try { 211 return new Float (getString().trim()).floatValue(); 212 } catch (NumberFormatException nfe) { 213 throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "float"); 214 } 215 } 216 217 221 public double getDouble() throws StandardException 222 { 223 if (isNull()) return 0; 224 try { 225 return new Double (getString().trim()).doubleValue(); 226 } catch (NumberFormatException nfe) { 227 throw StandardException.newException(SQLState.LANG_FORMAT_EXCEPTION, "double"); 228 } 229 } 230 231 234 public int typeToBigDecimal() throws StandardException 235 { 236 return java.sql.Types.CHAR; 237 } 238 242 public Date getDate( Calendar cal) throws StandardException 243 { 244 return getDate( cal, getString(), getLocaleFinder()); 245 } 246 247 public static Date getDate(java.util.Calendar cal, String str, LocaleFinder localeFinder) throws StandardException 248 { 249 if( str == null) 250 return null; 251 SQLDate internalDate = new SQLDate( str, false, localeFinder); 252 return internalDate.getDate( cal); 253 } 254 255 259 public Time getTime(Calendar cal) throws StandardException 260 { 261 return getTime( cal, getString(), getLocaleFinder()); 262 } 263 264 267 public static Time getTime( Calendar cal, String str, LocaleFinder localeFinder) throws StandardException 268 { 269 if( str == null) 270 return null; 271 SQLTime internalTime = new SQLTime( str, false, localeFinder, cal); 272 return internalTime.getTime( cal); 273 } 274 275 279 public Timestamp getTimestamp( Calendar cal) throws StandardException 280 { 281 return getTimestamp( cal, getString(), getLocaleFinder()); 282 } 283 284 288 public static Timestamp getTimestamp(java.util.Calendar cal, String str, LocaleFinder localeFinder) 289 throws StandardException 290 { 291 if( str == null) 292 return null; 293 SQLTimestamp internalTimestamp = new SQLTimestamp( str, false, localeFinder, cal); 294 return internalTimestamp.getTimestamp( cal); 295 } 296 297 300 public Object getObject() throws StandardException 301 { 302 return getString(); 303 } 304 305 308 public InputStream getStream() throws StandardException 309 { 310 return stream; 311 } 312 313 316 public int getLength() throws StandardException 317 { 318 if (rawLength != -1) 319 return rawLength; 320 321 String tmpString = getString(); 322 return (tmpString == null) ? 323 0 : tmpString.length(); 324 } 325 326 public String getTypeName() 327 { 328 return TypeId.CHAR_NAME; 329 } 330 331 340 public String getString() throws StandardException 341 { 342 if (value == null) { 343 344 int len = rawLength; 345 346 if (len != -1) { 347 348 350 value = new String (rawData, 0, len); 351 if (len > RETURN_SPACE_THRESHOLD) { 352 rawData = null; 354 rawLength = -1; 355 intArray = null; 357 intLength = 0; 358 cKey = null; 359 } 360 361 } else if (stream != null) { 362 363 try { 365 366 if (stream instanceof FormatIdInputStream) { 367 readExternal((FormatIdInputStream) stream); 368 } else { 369 readExternal(new FormatIdInputStream(stream)); 370 } 371 stream = null; 372 373 return getString(); 376 377 } catch (IOException ioe) { 378 379 throw StandardException.newException( 380 SQLState.LANG_STREAMING_COLUMN_I_O_EXCEPTION, 381 ioe, 382 "java.sql.String"); 383 } 384 } 385 } 386 387 return value; 388 } 389 390 403 public char[] getCharArray() throws StandardException 404 { 405 if (isNull()) 406 { 407 return (char[])null; 408 } 409 else if (rawLength != -1) 410 { 411 return rawData; 412 } 413 else 414 { 415 getString(); 419 rawData = value.toCharArray(); 420 rawLength = rawData.length; 421 intArray = null; 423 intLength = 0; 424 cKey = null; 425 return rawData; 426 } 427 } 428 429 432 public InputStream returnStream() 433 { 434 return stream; 435 } 436 437 440 public final void setStream(InputStream newStream) 441 { 442 this.value = null; 443 this.rawLength = -1; 444 this.stream = newStream; 445 intArray = null; 447 intLength = 0; 448 cKey = null; 449 } 450 451 public void loadStream() throws StandardException 452 { 453 getString(); 454 } 455 456 459 460 465 public int getTypeFormatId() { 466 return StoredFormatIds.SQL_CHAR_ID; 467 } 468 469 473 public boolean isNull() 474 { 475 return ((value == null) && (rawLength == -1) && (stream == null)); 476 } 477 478 526 public void writeExternal(ObjectOutput out) throws IOException 527 { 528 if (SanityManager.DEBUG) 530 SanityManager.ASSERT(!isNull()); 531 532 String lvalue = null; 533 char[] data = null; 534 535 int strlen = rawLength; 536 boolean isRaw; 537 538 if (strlen < 0) { 539 lvalue = value; 540 strlen = lvalue.length(); 541 isRaw = false; 542 } else { 543 data = rawData; 544 isRaw = true; 545 } 546 547 int utflen = strlen; 549 550 for (int i = 0 ; (i < strlen) && (utflen <= 65535); i++) 551 { 552 int c = isRaw ? data[i] : lvalue.charAt(i); 553 if ((c >= 0x0001) && (c <= 0x007F)) 554 { 555 } 557 else if (c > 0x07FF) 558 { 559 utflen += 2; } 561 else 562 { 563 utflen += 1; } 565 } 566 567 boolean isLongUTF = false; 568 if (utflen > 65535) 570 { 571 isLongUTF = true; 572 utflen = 0; 573 } 574 575 out.write((utflen >>> 8) & 0xFF); 576 out.write((utflen >>> 0) & 0xFF); 577 for (int i = 0 ; i < strlen ; i++) 578 { 579 int c = isRaw ? data[i] : lvalue.charAt(i); 580 if ((c >= 0x0001) && (c <= 0x007F)) 581 { 582 out.write(c); 583 } 584 else if (c > 0x07FF) 585 { 586 out.write(0xE0 | ((c >> 12) & 0x0F)); 587 out.write(0x80 | ((c >> 6) & 0x3F)); 588 out.write(0x80 | ((c >> 0) & 0x3F)); 589 } 590 else 591 { 592 out.write(0xC0 | ((c >> 6) & 0x1F)); 593 out.write(0x80 | ((c >> 0) & 0x3F)); 594 } 595 } 596 597 if (isLongUTF) 598 { 599 out.write(0xE0); 602 out.write(0); 603 out.write(0); 604 } 605 } 606 607 631 public void readExternalFromArray(ArrayInputStream in) 632 throws IOException 633 { 634 arg_passer[0] = rawData; 635 636 rawLength = in.readCloudscapeUTF(arg_passer); 637 638 rawData = arg_passer[0]; 639 640 value = null; 642 stream = null; 643 644 intArray = null; 646 intLength = 0; 647 cKey = null; 648 } 649 char[][] arg_passer = new char[1][]; 650 651 public void readExternal(ObjectInput in) throws IOException 652 { 653 655 int utflen = in.readUnsignedShort(); 656 657 int requiredLength; 658 int minGrowBy = growBy(); 663 if (utflen != 0) 664 { 665 requiredLength = utflen; 668 } 669 else 670 { 671 requiredLength = in.available(); 676 if (requiredLength < minGrowBy) 677 requiredLength = minGrowBy; 678 } 679 680 char str[]; 681 if ((rawData == null) || (requiredLength > rawData.length)) { 682 683 str = new char[requiredLength]; 684 } else { 685 str = rawData; 686 } 687 int arrayLength = str.length; 688 689 rawData = null; 691 restoreToNull(); 692 693 int count = 0; 694 int strlen = 0; 695 696 readingLoop: 697 while ( ((count < utflen) || (utflen == 0))) 698 { 699 int c; 700 701 try { 702 703 c = in.readUnsignedByte(); 704 } catch (EOFException eof) { 705 if (utflen != 0) 706 throw new EOFException (); 707 708 break readingLoop; 713 } 714 715 720 723 726 if (strlen >= arrayLength) { 728 int growby = in.available(); 729 if (growby < minGrowBy) 748 growby = minGrowBy; 749 750 int newstrlength = arrayLength + growby; 751 char oldstr[] = str; 752 str = new char[newstrlength]; 753 754 System.arraycopy(oldstr, 0, str, 0, arrayLength); 755 arrayLength = newstrlength; 756 } 757 758 778 int char2, char3; 779 char actualChar; 780 if ((c & 0x80) == 0x00) 781 { 782 count++; 784 actualChar = (char) c; 785 } 786 else if ((c & 0x60) == 0x40) { 788 count += 2; 790 if (utflen != 0 && count > utflen) 791 throw new UTFDataFormatException (); 792 char2 = in.readUnsignedByte(); 793 if ((char2 & 0xC0) != 0x80) 794 throw new UTFDataFormatException (); 795 actualChar = (char)(((c & 0x1F) << 6) | (char2 & 0x3F)); 796 } 797 else if ((c & 0x70) == 0x60) { 799 count += 3; 801 if (utflen != 0 && count > utflen) 802 throw new UTFDataFormatException (); 803 char2 = in.readUnsignedByte(); 804 char3 = in.readUnsignedByte(); 805 if ((c == 0xE0) && (char2 == 0) && (char3 == 0) 806 && (utflen == 0)) 807 { 808 break readingLoop; 812 } 813 814 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) 815 throw new UTFDataFormatException (); 816 817 818 actualChar = (char)(((c & 0x0F) << 12) | 819 ((char2 & 0x3F) << 6) | 820 ((char3 & 0x3F) << 0)); 821 } 822 else { 823 824 throw new UTFDataFormatException (); 825 } 826 827 str[strlen++] = actualChar; 828 } 829 830 831 rawData = str; 832 rawLength = strlen; 833 834 intArray = null; 836 intLength = 0; 837 cKey = null; 838 } 839 840 848 protected int growBy() 849 { 850 return GROWBY_FOR_CHAR; } 852 856 public void restoreToNull() 857 { 858 value = null; 859 stream = null; 860 rawLength = -1; 861 intArray = null; 863 intLength = 0; 864 cKey = null; 865 } 866 867 870 public boolean compare(int op, 871 DataValueDescriptor other, 872 boolean orderedNulls, 873 boolean unknownRV) 874 throws StandardException 875 { 876 if (!orderedNulls) { 878 if (this.isNull() || ((DataValueDescriptor) other).isNull()) 879 return unknownRV; 880 } 881 882 885 if (! (other instanceof SQLChar)) 886 { 887 return other.compare(flip(op), this, orderedNulls, unknownRV); 888 } 889 890 891 return super.compare(op, other, orderedNulls, unknownRV); 892 } 893 894 897 public int compare(DataValueDescriptor other) throws StandardException 898 { 899 902 if (typePrecedence() < other.typePrecedence()) 903 { 904 return - (other.compare(this)); 905 } 906 907 return stringCompare(this, (SQLChar)other); 909 } 910 911 914 915 920 public Object cloneObject() 921 { 922 if (stream == null) 923 return getClone(); 924 SQLChar self = (SQLChar) getNewNull(); 925 self.copyState(this); 926 return self; 927 } 928 929 932 933 934 public DataValueDescriptor getClone() 935 { 936 try 937 { 938 return new SQLChar(getString()); 939 } 940 catch (StandardException se) 941 { 942 if (SanityManager.DEBUG) 943 SanityManager.THROWASSERT("Unexpected exception " + se); 944 return null; 945 } 946 } 947 948 952 public DataValueDescriptor getNewNull() 953 { 954 return new SQLChar(); 955 } 956 957 962 public final void setValueFromResultSet(ResultSet resultSet, int colNumber, 963 boolean isNullable) 964 throws SQLException 965 { 966 setValue(resultSet.getString(colNumber)); 967 } 968 969 972 public final void setInto(PreparedStatement ps, int position) throws SQLException , StandardException { 973 974 ps.setString(position, getString()); 975 } 976 977 978 981 982 985 986 989 public SQLChar() 990 { 991 } 992 993 public SQLChar(String val) 994 { 995 value = val; 996 } 997 998 public void setValue(String theValue) 999 { 1000 stream = null; 1001 rawLength = -1; 1002 intArray = null; 1004 intLength = 0; 1005 cKey = null; 1006 1007 value = theValue; 1008 } 1009 1010 public void setValue(boolean theValue) throws StandardException 1011 { 1012 setValue(theValue ? "1" : "0"); 1014 } 1015 1016 public void setValue(int theValue) throws StandardException 1017 { 1018 setValue(Integer.toString(theValue)); 1019 } 1020 1021 public void setValue(double theValue) throws StandardException 1022 { 1023 setValue(Double.toString(theValue)); 1024 } 1025 1026 public void setValue(float theValue) throws StandardException 1027 { 1028 setValue(Float.toString(theValue)); 1029 } 1030 1031 public void setValue(short theValue) throws StandardException 1032 { 1033 setValue(Short.toString(theValue)); 1034 } 1035 1036 public void setValue(long theValue) throws StandardException 1037 { 1038 setValue(Long.toString(theValue)); 1039 } 1040 1041 public void setValue(byte theValue) throws StandardException 1042 { 1043 setValue(Byte.toString(theValue)); 1044 } 1045 1046 public void setValue(byte[] theValue) throws StandardException 1047 { 1048 if (theValue == null) 1049 { 1050 restoreToNull(); 1051 return; 1052 } 1053 1054 1063 int mod = (theValue.length % 2); 1064 int len = (theValue.length/2) + mod; 1065 char[] carray = new char[len]; 1066 int cindex = 0; 1067 int bindex = 0; 1068 1069 1073 if (mod == 1) 1074 { 1075 carray[--len] = (char)(theValue[theValue.length - 1] << 8); 1076 } 1077 1078 for (; cindex < len; bindex+=2, cindex++) 1079 { 1080 carray[cindex] = (char)((theValue[bindex] << 8) | 1081 (theValue[bindex+1] & 0x00ff)); 1082 } 1083 1084 setValue(new String (carray)); 1085 } 1086 1087 1091 public void setBigDecimal(Number bigDecimal) throws StandardException 1092 { 1093 if (bigDecimal == null) 1094 setToNull(); 1095 else 1096 setValue(bigDecimal.toString()); 1097 } 1098 1099 1100 public void setValue(Date theValue, Calendar cal) throws StandardException 1101 { 1102 String strValue = null; 1103 if( theValue != null) 1104 { 1105 if( cal == null) 1106 strValue = theValue.toString(); 1107 else 1108 { 1109 cal.setTime( theValue); 1110 StringBuffer sb = new StringBuffer (); 1111 formatJDBCDate( cal, sb); 1112 strValue= sb.toString(); 1113 } 1114 } 1115 setValue( strValue); 1116 } 1117 1118 1119 public void setValue(Time theValue, Calendar cal) throws StandardException 1120 { 1121 String strValue = null; 1122 if( theValue != null) 1123 { 1124 if( cal == null) 1125 strValue = theValue.toString(); 1126 else 1127 { 1128 cal.setTime( theValue); 1129 StringBuffer sb = new StringBuffer (); 1130 formatJDBCTime( cal, sb); 1131 strValue= sb.toString(); 1132 } 1133 } 1134 setValue( strValue); 1135 } 1136 1137 1138 public void setValue(Timestamp theValue, Calendar cal) throws StandardException 1139 { 1140 String strValue = null; 1141 if( theValue != null) 1142 { 1143 if( cal == null) 1144 strValue = theValue.toString(); 1145 else 1146 { 1147 cal.setTime( theValue); 1148 StringBuffer sb = new StringBuffer (); 1149 formatJDBCDate( cal, sb); 1150 sb.append( ' '); 1151 formatJDBCTime( cal, sb); 1152 int micros = (theValue.getNanos() + SQLTimestamp.FRACTION_TO_NANO/2)/SQLTimestamp.FRACTION_TO_NANO; 1153 if( micros > 0) 1154 { 1155 sb.append( '.'); 1156 String microsStr = Integer.toString( micros); 1157 if( microsStr.length() > SQLTimestamp.MAX_FRACTION_DIGITS) 1158 sb.append( microsStr.substring( 0, SQLTimestamp.MAX_FRACTION_DIGITS)); 1159 else 1160 { 1161 for( int i = microsStr.length(); i < SQLTimestamp.MAX_FRACTION_DIGITS ; i++) 1162 sb.append( '0'); 1163 sb.append( microsStr); 1164 } 1165 } 1166 strValue= sb.toString(); 1167 } 1168 } 1169 setValue( strValue); 1170 } 1171 1172 private void formatJDBCDate( Calendar cal, StringBuffer sb) 1173 { 1174 SQLDate.dateToString( cal.get( Calendar.YEAR), 1175 cal.get( Calendar.MONTH) - Calendar.JANUARY + 1, 1176 cal.get( Calendar.DAY_OF_MONTH), 1177 sb); 1178 } 1179 1180 private void formatJDBCTime( Calendar cal, StringBuffer sb) 1181 { 1182 SQLTime.timeToString( cal.get( Calendar.HOUR), cal.get( Calendar.MINUTE), cal.get( Calendar.SECOND), sb); 1183 } 1184 1185 1190 public final void setValue(InputStream theStream, int valueLength) 1191 { 1192 setStream(theStream); 1193 } 1194 1195 1203 public void setObjectForCast(Object theValue, boolean instanceOfResultType, 1204 String resultTypeClassName) throws StandardException { 1205 1206 if (theValue == null) 1207 { 1208 setToNull(); 1209 return; 1210 } 1211 1212 if ("java.lang.String".equals(resultTypeClassName)) 1213 setValue(theValue.toString()); 1214 else 1215 super.setObjectForCast(theValue, instanceOfResultType, resultTypeClassName); 1216 } 1217 1218 protected void setFrom(DataValueDescriptor theValue) throws StandardException { 1219 1220 setValue(theValue.getString()); 1221 } 1222 1223 1236 1237 public void normalize( 1238 DataTypeDescriptor desiredType, 1239 DataValueDescriptor source) 1240 throws StandardException 1241 { 1242 1243 normalize(desiredType, source.getString()); 1244 1245 } 1246 1247 protected void normalize(DataTypeDescriptor desiredType, String sourceValue) 1248 throws StandardException 1249 { 1250 1251 1252 int desiredWidth = desiredType.getMaximumWidth(); 1253 int sourceWidth = sourceValue.length(); 1254 1255 1259 if (sourceWidth == desiredWidth) { 1260 setValue(sourceValue); 1261 return; 1262 } 1263 1264 1268 if (sourceWidth < desiredWidth) 1269 { 1270 setToNull(); 1271 1272 char[] ca; 1273 if ((rawData == null) || (desiredWidth > rawData.length)) { 1274 1275 ca = rawData = new char[desiredWidth]; 1276 } else { 1277 ca = rawData; 1278 } 1279 1280 sourceValue.getChars(0, sourceWidth, ca, 0); 1281 SQLChar.appendBlanks(ca, sourceWidth, desiredWidth - sourceWidth); 1282 1283 rawLength = desiredWidth; 1284 1285 return; 1286 } 1287 1288 1291 hasNonBlankChars(sourceValue, desiredWidth, sourceWidth); 1292 1293 1297 1298 String truncatedString = sourceValue.substring(0, desiredWidth); 1299 setValue(truncatedString); 1300 } 1301 1302 1305 protected final void hasNonBlankChars(String source, int start, int end) 1306 throws StandardException 1307 { 1308 1311 for (int posn = start; posn < end; posn++) 1312 { 1313 if (source.charAt(posn) != ' ') 1314 { 1315 throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), StringUtil.formatForPrint(source), String.valueOf(start)); 1316 } 1317 } 1318 } 1319 1320 1326 1340 public void setWidth(int desiredWidth, 1341 int desiredScale, boolean errorOnTrunc) 1343 throws StandardException 1344 { 1345 int sourceWidth; 1346 1347 1350 if (getString() == null) 1351 { 1352 return; 1353 } 1354 1355 sourceWidth = getLength(); 1356 1357 1363 if (sourceWidth < desiredWidth) 1364 { 1365 if (!(this instanceof SQLVarchar)) 1366 { 1367 StringBuffer strbuf; 1368 1369 strbuf = new StringBuffer (getString()); 1370 1371 for ( ; sourceWidth < desiredWidth; sourceWidth++) 1372 { 1373 strbuf.append(' '); 1374 } 1375 1376 setValue(new String (strbuf)); 1377 } 1378 } 1379 else if (sourceWidth > desiredWidth && desiredWidth > 0) 1380 { 1381 1384 if (errorOnTrunc) 1385 hasNonBlankChars(getString(), desiredWidth, sourceWidth); 1386 1388 1391 setValue(getString().substring(0, desiredWidth)); 1392 } 1393 return; 1394 } 1395 1396 1399 1400 1411 1412 public BooleanDataValue equals(DataValueDescriptor left, 1413 DataValueDescriptor right) 1414 throws StandardException 1415 { 1416 boolean comparison; 1417 1418 if ((left instanceof SQLChar) && (right instanceof SQLChar)) 1419 { 1420 comparison = stringCompare((SQLChar) left, (SQLChar) right) == 0; 1421 } 1422 else 1423 { 1424 comparison = stringCompare(left.getString(), 1425 right.getString()) == 0; 1426 } 1427 1428 return SQLBoolean.truthValue(left, 1429 right, 1430 comparison); 1431 } 1432 1433 1445 1446 public BooleanDataValue notEquals(DataValueDescriptor left, 1447 DataValueDescriptor right) 1448 throws StandardException 1449 { 1450 boolean comparison; 1451 1452 if ((left instanceof SQLChar) && (right instanceof SQLChar)) 1453 { 1454 comparison = stringCompare((SQLChar) left, (SQLChar) right) != 0; 1455 } 1456 else 1457 { 1458 comparison = stringCompare(left.getString(), 1459 right.getString()) != 0; 1460 } 1461 1462 return SQLBoolean.truthValue(left, 1463 right, 1464 comparison); 1465 } 1466 1467 1479 1480 public BooleanDataValue lessThan(DataValueDescriptor left, 1481 DataValueDescriptor right) 1482 throws StandardException 1483 { 1484 boolean comparison; 1485 1486 if ((left instanceof SQLChar) && (right instanceof SQLChar)) 1487 { 1488 comparison = stringCompare((SQLChar) left, (SQLChar) right) < 0; 1489 } 1490 else 1491 { 1492 comparison = stringCompare(left.getString(), 1493 right.getString()) < 0; 1494 } 1495 1496 return SQLBoolean.truthValue(left, 1497 right, 1498 comparison); 1499 } 1500 1501 1513 1514 public BooleanDataValue greaterThan(DataValueDescriptor left, 1515 DataValueDescriptor right) 1516 throws StandardException 1517 { 1518 boolean comparison; 1519 1520 if ((left instanceof SQLChar) && (right instanceof SQLChar)) 1521 { 1522 comparison = stringCompare((SQLChar) left, (SQLChar) right) > 0; 1523 } 1524 else 1525 { 1526 comparison = stringCompare(left.getString(), 1527 right.getString()) > 0; 1528 } 1529 1530 return SQLBoolean.truthValue(left, 1531 right, 1532 comparison); 1533 } 1534 1535 1547 1548 public BooleanDataValue lessOrEquals(DataValueDescriptor left, 1549 DataValueDescriptor right) 1550 throws StandardException 1551 { 1552 boolean comparison; 1553 1554 if ((left instanceof SQLChar) && (right instanceof SQLChar)) 1555 { 1556 comparison = stringCompare((SQLChar) left, (SQLChar) right) <= 0; 1557 } 1558 else 1559 { 1560 comparison = stringCompare(left.getString(), 1561 right.getString()) <= 0; 1562 } 1563 1564 return SQLBoolean.truthValue(left, 1565 right, 1566 comparison); 1567 } 1568 1569 1581 1582 public BooleanDataValue greaterOrEquals(DataValueDescriptor left, 1583 DataValueDescriptor right) 1584 throws StandardException 1585 { 1586 boolean comparison; 1587 1588 if ((left instanceof SQLChar) && (right instanceof SQLChar)) 1589 { 1590 comparison = stringCompare((SQLChar) left, (SQLChar) right) >= 0; 1591 } 1592 else 1593 { 1594 comparison = stringCompare(left.getString(), 1595 right.getString()) >= 0; 1596 } 1597 1598 return SQLBoolean.truthValue(left, 1599 right, 1600 comparison); 1601 } 1602 1603 1606 1618 public NumberDataValue charLength(NumberDataValue result) 1619 throws StandardException 1620 { 1621 if (result == null) 1622 { 1623 result = new SQLInteger(); 1624 } 1625 1626 if (this.isNull()) 1627 { 1628 result.setToNull(); 1629 return result; 1630 } 1631 1632 result.setValue(this.getLength()); 1633 return result; 1634 } 1635 1636 1641 public StringDataValue concatenate( 1642 StringDataValue leftOperand, 1643 StringDataValue rightOperand, 1644 StringDataValue result) 1645 throws StandardException 1646 { 1647 if (leftOperand.isNull() || leftOperand.getString() == null || 1648 rightOperand.isNull() || rightOperand.getString() == null) 1649 { 1650 result.setToNull(); 1651 return result; 1652 } 1653 1654 result.setValue(leftOperand.getString().concat(rightOperand.getString())); 1655 return result; 1656 } 1657 1658 1659 1669 public BooleanDataValue like(DataValueDescriptor pattern) 1670 throws StandardException 1671 { 1672 Boolean likeResult; 1673 1674 if (! isNationalString()) 1675 { 1676 char[] evalCharArray = getCharArray(); 1681 char[] patternCharArray = ((SQLChar)pattern).getCharArray(); 1682 likeResult = Like.like(evalCharArray, 1683 getLength(), 1684 patternCharArray, 1685 pattern.getLength()); 1686 } 1687 else 1688 { 1689 SQLChar patternSQLChar = (SQLChar) pattern; 1690 likeResult = Like.like(getIntArray(), 1691 getIntLength(), 1692 patternSQLChar.getIntArray(), 1693 patternSQLChar.getIntLength(), 1694 getLocaleFinder().getCollator()); 1695 } 1696 1697 return SQLBoolean.truthValue(this, 1698 pattern, 1699 likeResult); 1700 } 1701 1702 1712 1713 public BooleanDataValue like( 1714 DataValueDescriptor pattern, 1715 DataValueDescriptor escape) 1716 throws StandardException 1717 { 1718 Boolean likeResult; 1719 1720 if (SanityManager.DEBUG) 1721 SanityManager.ASSERT( 1722 pattern instanceof StringDataValue && 1723 escape instanceof StringDataValue, 1724 "All three operands must be instances of StringDataValue"); 1725 1726 1731 if (escape.isNull()) 1732 { 1733 throw StandardException.newException(SQLState.LANG_ESCAPE_IS_NULL); 1734 } 1735 1736 if (! isNationalString()) 1737 { 1738 char[] evalCharArray = getCharArray(); 1743 char[] patternCharArray = ((SQLChar)pattern).getCharArray(); 1744 char[] escapeCharArray = (((SQLChar) escape).getCharArray()); 1745 int escapeLength = escape.getLength(); 1746 1747 if (escapeCharArray != null && escapeLength != 1 ) 1748 { 1749 throw StandardException.newException(SQLState.LANG_INVALID_ESCAPE_CHARACTER, 1750 new String (escapeCharArray)); 1751 } 1752 else 1753 { 1754 SQLChar escapeSQLChar = (SQLChar) escape; 1757 int[] escapeIntArray = escapeSQLChar.getIntArray(); 1758 if (escapeIntArray != null && (escapeIntArray.length != 1)) 1759 { 1760 throw StandardException.newException(SQLState.LANG_INVALID_ESCAPE_CHARACTER,new String (escapeSQLChar.getCharArray())); 1761 } 1762 } 1763 likeResult = Like.like(evalCharArray, 1764 getLength(), 1765 patternCharArray, 1766 pattern.getLength(), 1767 escapeCharArray, 1768 escapeLength); 1769 } 1770 else 1771 { 1772 SQLChar patternSQLChar = (SQLChar) pattern; 1773 SQLChar escapeSQLChar = (SQLChar) escape; 1774 int[] escapeIntArray = escapeSQLChar.getIntArray(); 1775 int escapeLength = escapeSQLChar.getIntLength(); 1776 1777 if (escapeIntArray != null && (escapeIntArray.length != 1)) 1778 { 1779 throw StandardException.newException(SQLState.LANG_INVALID_ESCAPE_CHARACTER, 1780 new String (escapeSQLChar.getCharArray())); 1781 } 1782 likeResult = Like.like(getIntArray(), 1783 getIntLength(), 1784 patternSQLChar.getIntArray(), 1785 patternSQLChar.getIntLength(), 1786 escapeIntArray, 1787 escapeLength, 1788 getLocaleFinder().getCollator()); 1789 } 1790 1791 return SQLBoolean.truthValue(this, 1792 pattern, 1793 likeResult); 1794 } 1795 1796 1808 public NumberDataValue locate( StringDataValue searchFrom, 1809 NumberDataValue start, 1810 NumberDataValue result) 1811 throws StandardException 1812 { 1813 int startVal; 1814 1815 if( result == null ) 1816 { 1817 result = new SQLInteger(); 1818 } 1819 1820 if( start.isNull() ) 1821 { 1822 startVal = 1; 1823 } 1824 else 1825 { 1826 startVal = start.getInt(); 1827 } 1828 1829 if( isNull() || searchFrom.isNull() ) 1830 { 1831 result.setToNull(); 1832 return result; 1833 } 1834 1835 String mySearchFrom = searchFrom.getString(); 1836 String mySearchFor = this.getString(); 1837 1838 1839 if( startVal < 1 ) 1840 { 1841 throw StandardException.newException( 1842 SQLState.LANG_INVALID_PARAMETER_FOR_SEARCH_POSITION, 1843 new String (getString()), new String (mySearchFrom), 1844 new Integer (startVal)); 1845 } 1846 1847 if( mySearchFor.length() == 0 ) 1848 { 1849 result.setValue( startVal ); 1850 return result; 1851 } 1852 1853 result.setValue( mySearchFrom.indexOf(mySearchFor, startVal - 1) + 1); 1854 return result; 1855 } 1856 1857 1870 public ConcatableDataValue substring( 1871 NumberDataValue start, 1872 NumberDataValue length, 1873 ConcatableDataValue result, 1874 int maxLen) 1875 throws StandardException 1876 { 1877 int startInt; 1878 int lengthInt; 1879 StringDataValue stringResult; 1880 1881 if (result == null) 1882 { 1883 result = getNewVarchar(); 1884 } 1885 1886 stringResult = (StringDataValue) result; 1887 1888 1892 if (this.isNull() || start.isNull() || (length != null && length.isNull())) 1893 { 1894 stringResult.setToNull(); 1895 return stringResult; 1896 } 1897 1898 startInt = start.getInt(); 1899 1900 if (length != null) 1902 { 1903 lengthInt = length.getInt(); 1904 } 1905 else lengthInt = maxLen - startInt + 1; 1906 1907 1911 if ((startInt <= 0 || lengthInt < 0 || startInt > maxLen || 1912 lengthInt > maxLen - startInt + 1)) 1913 throw StandardException.newException(SQLState.LANG_SUBSTR_START_OR_LEN_OUT_OF_RANGE); 1914 1915 if (lengthInt < 0) 1917 { 1918 stringResult.setToNull(); 1919 return stringResult; 1920 } 1921 1922 1923 if (startInt < 0) 1924 { 1925 if (startInt + getLength() < 0 && 1927 (startInt + getLength() + lengthInt <= 0)) 1928 { 1929 stringResult.setValue(""); 1930 return stringResult; 1931 } 1932 1933 startInt += getLength(); 1935 1936 while (startInt < 0) 1937 { 1938 startInt++; 1939 lengthInt--; 1940 } 1941 } 1942 else if (startInt > 0) 1943 { 1944 1945 startInt--; 1946 } 1947 1948 1952 if (lengthInt == 0 || 1953 lengthInt <= 0 - startInt || 1954 startInt > getLength()) 1955 { 1956 stringResult.setValue(""); 1957 return stringResult; 1958 } 1959 1960 if (lengthInt >= getLength() - startInt) 1961 { 1962 stringResult.setValue(getString().substring(startInt)); 1963 } 1964 else 1965 { 1966 stringResult.setValue(getString().substring(startInt, startInt + lengthInt)); 1967 } 1968 1969 return stringResult; 1970 } 1971 1972 1983 public StringDataValue trim( 1984 int trimType, 1985 StringDataValue result) 1986 throws StandardException 1987 { 1988 1989 if (result == null) 1990 { 1991 result = getNewVarchar(); 1992 } 1993 1994 1995 if (this.isNull()) 1996 { 1997 result.setToNull(); 1998 return result; 1999 } 2000 2001 char[] trimChars = {' '}; 2002 String tmpValue = getString(); 2003 2004 if (trimType == LEADING) 2006 { 2007 int start = 0; 2008 for ( ; start < tmpValue.length(); start++) 2010 { 2011 boolean found = false; 2012 for (int index = 0; index < trimChars.length; index++) 2013 { 2014 if (tmpValue.charAt(start) == trimChars[index]) 2015 { 2016 found = true; 2017 break; 2018 } 2019 } 2020 2021 if (! found) 2022 { 2023 break; 2024 } 2025 } 2026 2027 if (start == tmpValue.length()) 2029 { 2030 tmpValue = ""; 2031 } 2032 else if (start > 0) 2033 { 2034 tmpValue = tmpValue.substring(start); 2035 } 2036 } 2037 2038 if (trimType == TRAILING) 2040 { 2041 int start = tmpValue.length(); 2042 for ( ; start > 0; start--) 2044 { 2045 boolean found = false; 2046 for (int index = 0; index < trimChars.length; index++) 2047 { 2048 if (tmpValue.charAt(start - 1) == trimChars[index]) 2049 { 2050 found = true; 2051 break; 2052 } 2053 } 2054 2055 if (! found) 2056 { 2057 break; 2058 } 2059 } 2060 2061 if (start == 0) 2063 { 2064 tmpValue = ""; 2065 } 2066 else if (start < tmpValue.length()) 2067 { 2068 tmpValue = tmpValue.substring(0, start); 2069 } 2070 } 2071 2072 result.setValue(tmpValue); 2073 return result; 2074 } 2075 2076 2080 public StringDataValue upper(StringDataValue result) 2081 throws StandardException 2082 { 2083 if (result == null) 2084 { 2085 result = (StringDataValue) getNewNull(); 2086 } 2087 2088 if (this.isNull()) 2089 { 2090 result.setToNull(); 2091 return result; 2092 } 2093 2094 String upper = getString(); 2095 upper = upper.toUpperCase(getLocale()); 2096 result.setValue(upper); 2097 return result; 2098 } 2099 2100 2104 public StringDataValue lower(StringDataValue result) 2105 throws StandardException 2106 { 2107 if (result == null) 2108 { 2109 result = (StringDataValue) getNewNull(); 2110 } 2111 2112 if (this.isNull()) 2113 { 2114 result.setToNull(); 2115 return result; 2116 } 2117 2118 2119 String lower = getString(); 2120 lower = lower.toLowerCase(getLocale()); 2121 result.setValue(lower); 2122 return result; 2123 } 2124 2125 2128 2129 2130 public int typePrecedence() 2131 { 2132 return TypeId.CHAR_PRECEDENCE; 2133 } 2134 2135 2145 protected static int stringCompare(String op1, String op2) 2146 { 2147 int posn; 2148 char leftchar; 2149 char rightchar; 2150 int leftlen; 2151 int rightlen; 2152 int retvalIfLTSpace; 2153 String remainingString; 2154 int remainingLen; 2155 2156 2159 if (op1 == null || op2 == null) 2160 { 2161 if (op1 != null) return -1; 2163 if (op2 != null) return 1; 2165 return 0; } 2167 2171 2172 leftlen = op1.length(); 2173 rightlen = op2.length(); 2174 2175 int shorterLen = leftlen < rightlen ? leftlen : rightlen; 2176 2177 for (posn = 0; posn < shorterLen; posn++) 2178 { 2179 leftchar = op1.charAt(posn); 2180 rightchar = op2.charAt(posn); 2181 if (leftchar != rightchar) 2182 { 2183 if (leftchar < rightchar) 2184 return -1; 2185 else 2186 return 1; 2187 } 2188 } 2189 2190 2195 if (leftlen == rightlen) 2196 return 0; 2197 2198 2204 if (leftlen > rightlen) 2205 { 2206 2209 2210 2211 retvalIfLTSpace = -1; 2212 remainingString = op1; 2213 posn = rightlen; 2214 remainingLen = leftlen; 2215 } 2216 else 2217 { 2218 2221 2222 2223 retvalIfLTSpace = 1; 2224 remainingString = op2; 2225 posn = leftlen; 2226 remainingLen = rightlen; 2227 } 2228 2229 2230 for ( ; posn < remainingLen; posn++) 2231 { 2232 char remainingChar; 2233 2234 2238 2239 remainingChar = remainingString.charAt(posn); 2240 2241 if (remainingChar < ' ') 2242 return retvalIfLTSpace; 2243 else if (remainingChar > ' ') 2244 return -retvalIfLTSpace; 2245 } 2246 2247 2250 return 0; 2251 } 2252 2253 2260 protected int stringCompare(SQLChar char1, SQLChar char2) 2261 throws StandardException 2262 { 2263 return stringCompare(char1.getCharArray(), char1.getLength(), 2264 char2.getCharArray(), char2.getLength()); 2265 } 2266 2267 2277 protected static int stringCompare(char[] op1, int leftlen, char[] op2, int rightlen) 2278 { 2279 int posn; 2280 char leftchar; 2281 char rightchar; 2282 int retvalIfLTSpace; 2283 char[] remainingString; 2284 int remainingLen; 2285 2286 2289 if (op1 == null || op2 == null) 2290 { 2291 if (op1 != null) return -1; 2293 if (op2 != null) return 1; 2295 return 0; } 2297 2301 int shorterLen = leftlen < rightlen ? leftlen : rightlen; 2302 for (posn = 0; posn < shorterLen; posn++) 2303 { 2304 leftchar = op1[posn]; 2305 rightchar = op2[posn]; 2306 if (leftchar != rightchar) 2307 { 2308 if (leftchar < rightchar) 2309 return -1; 2310 else 2311 return 1; 2312 } 2313 } 2314 2315 2320 if (leftlen == rightlen) 2321 return 0; 2322 2323 2329 if (leftlen > rightlen) 2330 { 2331 2334 2335 2336 retvalIfLTSpace = -1; 2337 remainingString = op1; 2338 posn = rightlen; 2339 remainingLen = leftlen; 2340 } 2341 else 2342 { 2343 2346 2347 2348 retvalIfLTSpace = 1; 2349 remainingString = op2; 2350 posn = leftlen; 2351 remainingLen = rightlen; 2352 } 2353 2354 2355 for ( ; posn < remainingLen; posn++) 2356 { 2357 char remainingChar; 2358 2359 2363 2364 remainingChar = remainingString[posn]; 2365 2366 if (remainingChar < ' ') 2367 return retvalIfLTSpace; 2368 else if (remainingChar > ' ') 2369 return -retvalIfLTSpace; 2370 } 2371 2372 2375 return 0; 2376 } 2377 2378 2379 2388 protected int stringCollatorCompare(SQLChar str2) 2389 throws StandardException 2390 { 2391 CollationKey ckey1 = this.getCollationKey(); 2392 CollationKey ckey2 = str2.getCollationKey(); 2393 2394 2395 2398 if (ckey1 == null || ckey2 == null) 2399 { 2400 if (ckey1 != null) return -1; 2402 if (ckey2 != null) return 1; 2404 return 0; } 2406 2407 return ckey1.compareTo(ckey2); 2408 } 2409 2410 protected CollationKey getCollationKey() throws StandardException 2411 { 2412 char tmpCharArray[]; 2413 2414 if (cKey != null) 2415 return cKey; 2416 2417 if (rawLength == -1) 2418 { 2419 2420 tmpCharArray = getCharArray(); 2421 if (tmpCharArray == null) 2422 return null; 2423 } 2424 2425 int lastNonspaceChar = rawLength; 2426 2427 while (lastNonspaceChar > 0 && 2428 rawData[lastNonspaceChar - 1] == '\u0020') 2429 lastNonspaceChar--; 2431 RuleBasedCollator rbc = getLocaleFinder().getCollator(); 2432 cKey = rbc.getCollationKey(new String (rawData, 0, lastNonspaceChar)); 2433 2434 return cKey; 2435 } 2436 2437 2440 2441 public String toString() 2442 { 2443 if (isNull()) { 2444 return "NULL"; 2445 } 2446 2447 if ((value == null) && (rawLength != -1)) { 2448 2449 return new String (rawData, 0, rawLength); 2450 } 2451 2452 if (stream != null) { 2453 try { 2454 return getString(); 2455 } catch (Exception e) { 2456 return e.toString(); 2457 } 2458 } 2459 2460 return value; 2461 } 2462 2463 2466 public int hashCode() 2467 { 2468 try { 2469 if (getString() == null) 2470 { 2471 return 0; 2472 } 2473 } 2474 catch (StandardException se) 2475 { 2476 if (SanityManager.DEBUG) 2477 SanityManager.THROWASSERT("Unexpected exception " + se); 2478 return 0; 2479 } 2480 2481 2482 2491 int index; 2492 int hashcode = 0; 2493 2494 String lvalue = value; 2496 2497 for (index = lvalue.length() - 1; 2499 index >= 0 && lvalue.charAt(index) == ' '; 2500 index--) 2501 { 2502 ; 2503 } 2504 2505 for ( ; index >= 0; index--) 2507 { 2508 hashcode += lvalue.charAt(index); 2509 } 2510 2511 return hashcode; 2512 } 2513 2514 2518 protected int nationalHashCode() 2519 { 2520 CollationKey tmpCKey = null; 2521 2522 try 2523 { 2524 tmpCKey = getCollationKey(); 2525 } 2526 catch (StandardException se) 2527 { 2528 if (SanityManager.DEBUG) 2529 { 2530 SanityManager.THROWASSERT("Unexpected exception " + se); 2531 } 2532 } 2533 2534 if (tmpCKey == null) 2535 return 0; 2536 return tmpCKey.hashCode(); 2537 } 2538 2539 private int[] getIntArray() 2540 throws StandardException 2541 { 2542 if (isNull()) 2543 { 2544 return (int[]) null; 2545 } 2546 2547 if (intArray != null) 2548 { 2549 return intArray; 2550 } 2551 2552 if (SanityManager.DEBUG) 2554 { 2555 if (intLength != 0) 2556 { 2557 SanityManager.THROWASSERT( 2558 "intLength expected to be 0, not " + intLength); 2559 } 2560 } 2561 2562 intArray = new int[getLength()]; 2563 2564 RuleBasedCollator rbc = getLocaleFinder().getCollator(); 2565 CollationElementIterator cei = rbc.getCollationElementIterator(getString()); 2566 int nextInt; 2567 while ((nextInt = cei.next()) != CollationElementIterator.NULLORDER) 2568 { 2569 2574 if (intLength == intArray.length) 2575 { 2576 int[] tempArray = intArray; 2577 intArray = new int[intLength + 5]; 2578 for (int index = 0; index < tempArray.length; index++) 2579 { 2580 intArray[index] = tempArray[index]; 2581 } 2582 } 2583 intArray[intLength++] = nextInt; 2584 } 2585 2586 return intArray; 2587 } 2588 2589 private int getIntLength() 2590 { 2591 return intLength; 2592 } 2593 2594 2602 protected StringDataValue getNewVarchar() throws StandardException 2603 { 2604 return new SQLVarchar(); 2605 } 2606 2607 2610 protected boolean isNationalString() 2611 { 2612 return false; 2613 } 2614 2615 2621 protected Date nationalGetDate( Calendar cal) throws StandardException 2622 { 2623 if (isNull()) 2624 return null; 2625 SQLDate internalDate = new SQLDate( getString(), false, getLocaleFinder(), cal); 2626 return internalDate.getDate( cal); 2627 } 2628 2629 2635 protected Time nationalGetTime( Calendar cal) throws StandardException 2636 { 2637 if (isNull()) 2638 return null; 2639 SQLTime internalTime = new SQLTime( getString(), false, getLocaleFinder(), cal); 2640 return internalTime.getTime( cal); 2641 } 2642 2643 2649 protected Timestamp nationalGetTimestamp( Calendar cal) throws StandardException 2650 { 2651 return getTimestamp( cal, getString(), getLocaleFinder()); 2653 } 2654 2655 protected void setLocaleFinder(LocaleFinder localeFinder) 2656 { 2657 this.localeFinder = localeFinder; 2658 } 2659 2660 2661 private Locale getLocale() throws StandardException 2662 { 2663 return getLocaleFinder().getCurrentLocale(); 2664 } 2665 2666 protected LocaleFinder getLocaleFinder() 2667 { 2668 if (localeFinder == null) 2672 { 2673 DatabaseContext dc = (DatabaseContext) ContextService.getContext(DatabaseContext.CONTEXT_ID); 2674 if( dc != null) 2675 localeFinder = dc.getDatabase(); 2676 } 2677 2678 return localeFinder; 2679 } 2680 2681 protected DateFormat getDateFormat() throws StandardException { 2682 return getLocaleFinder().getDateFormat(); 2683 } 2684 protected DateFormat getTimeFormat() throws StandardException { 2685 return getLocaleFinder().getTimeFormat(); 2686 } 2687 protected DateFormat getTimestampFormat() throws StandardException { 2688 return getLocaleFinder().getTimestampFormat(); 2689 } 2690 2691 protected DateFormat getDateFormat( Calendar cal) throws StandardException { 2692 return setDateFormatCalendar( getLocaleFinder().getDateFormat(), cal); 2693 } 2694 protected DateFormat getTimeFormat( Calendar cal) throws StandardException { 2695 return setDateFormatCalendar( getLocaleFinder().getTimeFormat(), cal); 2696 } 2697 protected DateFormat getTimestampFormat( Calendar cal) throws StandardException { 2698 return setDateFormatCalendar( getLocaleFinder().getTimestampFormat(), cal); 2699 } 2700 2701 private DateFormat setDateFormatCalendar( DateFormat df, Calendar cal) 2702 { 2703 if( cal != null && df.getTimeZone() != cal.getTimeZone()) 2704 { 2705 df = (DateFormat ) df.clone(); 2708 df.setCalendar( cal); 2709 } 2710 return df; 2711 } 2712 2713 2716 2717 private String value; 2720 2721 private char[] rawData; 2729 private int rawLength = -1; 2730 2731 private CollationKey cKey; 2733 2734 2737 InputStream stream; 2738 2739 2740 private int[] intArray; 2741 private int intLength; 2742 2743 2744 private LocaleFinder localeFinder; 2745 2746 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLChar.class); 2747 2748 public int estimateMemoryUsage() 2749 { 2750 int sz = BASE_MEMORY_USAGE + ClassSize.estimateMemoryUsage( value); 2751 if( null != rawData) 2752 sz += 2*rawData.length; 2753 if( null != intArray) 2755 sz += intArray.length*ClassSize.getIntSize(); 2756 return sz; 2757 } 2759 protected void copyState(SQLChar other) { 2760 2761 this.value = other.value; 2762 this.rawData = other.rawData; 2763 this.rawLength = other.rawLength; 2764 this.cKey = other.cKey; 2765 this.stream = other.stream; 2766 this.intArray = intArray; 2767 this.intLength = intLength; 2768 this.localeFinder = localeFinder; 2769 } 2770 2771} 2772 | Popular Tags |