| 1 65 66 67 package org.hsqldb; 68 69 import java.sql.Connection ; 70 import java.sql.Date ; 71 import java.sql.Time ; 72 import java.sql.Timestamp ; 73 import java.text.FieldPosition ; 74 import java.text.SimpleDateFormat ; 75 import java.util.Calendar ; 76 import java.util.Locale ; 77 import java.util.Random ; 78 79 import org.hsqldb.lib.HashMap; 80 import org.hsqldb.lib.IntValueHashMap; 81 import org.hsqldb.persist.HsqlDatabaseProperties; 82 import org.hsqldb.store.ValuePool; 83 84 92 98 99 113 public class Library { 114 115 static final SimpleDateFormat tocharFormat = new SimpleDateFormat (); 116 static final SimpleDateFormat daynameFormat = new SimpleDateFormat ("EEEE", 117 Locale.ENGLISH); 118 static final SimpleDateFormat monthnameFormat = 119 new SimpleDateFormat ("MMMM", Locale.ENGLISH); 120 static final StringBuffer daynameBuffer = new StringBuffer (); 121 static final StringBuffer monthnameBuffer = new StringBuffer (); 122 static final FieldPosition monthPosition = 123 new FieldPosition (SimpleDateFormat.MONTH_FIELD); 124 static final FieldPosition dayPosition = 125 new FieldPosition (SimpleDateFormat.DAY_OF_WEEK_FIELD); 126 public static final String [][] sNumeric = { 127 { 128 "ABS", "org.hsqldb.Library.abs" 129 }, { 130 "ACOS", "java.lang.Math.acos" 131 }, { 132 "ASIN", "java.lang.Math.asin" 133 }, { 134 "ATAN", "java.lang.Math.atan" 135 }, { 136 "ATAN2", "java.lang.Math.atan2" 137 }, { 138 "CEILING", "java.lang.Math.ceil" 139 }, { 140 "COS", "java.lang.Math.cos" 141 }, { 142 "COT", "org.hsqldb.Library.cot" 143 }, { 144 "DEGREES", "java.lang.Math.toDegrees" 145 }, { 146 "EXP", "java.lang.Math.exp" 147 }, { 148 "FLOOR", "java.lang.Math.floor" 149 }, { 150 "LOG", "java.lang.Math.log" 151 }, { 152 "LOG10", "org.hsqldb.Library.log10" 153 }, { 154 "MOD", "org.hsqldb.Library.mod" 155 }, { 156 "PI", "org.hsqldb.Library.pi" 157 }, { 158 "POWER", "java.lang.Math.pow" 159 }, { 160 "RADIANS", "java.lang.Math.toRadians" 161 }, { 162 "RAND", "java.lang.Math.random" 163 }, { 164 "ROUND", "org.hsqldb.Library.round" 165 }, { 166 "SIGN", "org.hsqldb.Library.sign" 167 }, { 168 "SIN", "java.lang.Math.sin" 169 }, { 170 "SQRT", "java.lang.Math.sqrt" 171 }, { 172 "TAN", "java.lang.Math.tan" 173 }, { 174 "TRUNCATE", "org.hsqldb.Library.truncate" 175 }, { 176 "BITAND", "org.hsqldb.Library.bitand" 177 }, { 178 "BITOR", "org.hsqldb.Library.bitor" 179 }, { 180 "BITXOR", "org.hsqldb.Library.bitxor" 181 }, { 182 "ROUNDMAGIC", "org.hsqldb.Library.roundMagic" 183 } 184 }; 185 186 public static final String [][] sString = { 189 { 190 "ASCII", "org.hsqldb.Library.ascii" 191 }, { 192 "BIT_LENGTH", "org.hsqldb.Library.bitLength" 193 }, { 194 "CHAR", "org.hsqldb.Library.character" 195 }, { 196 "CHAR_LENGTH", "org.hsqldb.Library.length" 197 }, { 198 "CHARACTER_LENGTH", "org.hsqldb.Library.length" 199 }, { 200 "CONCAT", "org.hsqldb.Library.concat" 201 }, { 202 "DIFFERENCE", "org.hsqldb.Library.difference" 203 }, { 204 "HEXTORAW", "org.hsqldb.Library.hexToRaw" 205 }, { 206 "INSERT", "org.hsqldb.Library.insert" 207 }, { 208 "LCASE", "org.hsqldb.Library.lcase" 209 }, { 210 "LEFT", "org.hsqldb.Library.left" 211 }, { 212 "LENGTH", "org.hsqldb.Library.length" 213 }, { 214 "LOCATE", "org.hsqldb.Library.locate" 215 }, { 216 "LTRIM", "org.hsqldb.Library.ltrim" 217 }, { 218 "OCTET_LENGTH", "org.hsqldb.Library.octetLength" 219 }, { 220 "RAWTOHEX", "org.hsqldb.Library.rawToHex" 221 }, { 222 "REPEAT", "org.hsqldb.Library.repeat" 223 }, { 224 "REPLACE", "org.hsqldb.Library.replace" 225 }, { 226 "RIGHT", "org.hsqldb.Library.right" 227 }, { 228 "RTRIM", "org.hsqldb.Library.rtrim" 229 }, { 230 "SOUNDEX", "org.hsqldb.Library.soundex" 231 }, { 232 "SPACE", "org.hsqldb.Library.space" 233 }, { 234 "SUBSTR", "org.hsqldb.Library.substring" 235 }, { 236 "SUBSTRING", "org.hsqldb.Library.substring" 237 }, { 238 "UCASE", "org.hsqldb.Library.ucase" 239 }, { 240 "LOWER", "org.hsqldb.Library.lcase" 241 }, { 242 "UPPER", "org.hsqldb.Library.ucase" 243 } 244 }; 245 public static final String [][] sTimeDate = { 246 { 247 "CURDATE", "org.hsqldb.Library.curdate" 248 }, { 249 "CURTIME", "org.hsqldb.Library.curtime" 250 }, { 251 "DATEDIFF", "org.hsqldb.Library.datediff" 252 }, { 253 "DAYNAME", "org.hsqldb.Library.dayname" 254 }, { 255 "DAY", "org.hsqldb.Library.dayofmonth" 256 }, { 257 "DAYOFMONTH", "org.hsqldb.Library.dayofmonth" 258 }, { 259 "DAYOFWEEK", "org.hsqldb.Library.dayofweek" 260 }, { 261 "DAYOFYEAR", "org.hsqldb.Library.dayofyear" 262 }, { 263 "HOUR", "org.hsqldb.Library.hour" 264 }, { 265 "MINUTE", "org.hsqldb.Library.minute" 266 }, { 267 "MONTH", "org.hsqldb.Library.month" 268 }, { 269 "MONTHNAME", "org.hsqldb.Library.monthname" 270 }, { 271 "NOW", "org.hsqldb.Library.now" 272 }, { 273 "QUARTER", "org.hsqldb.Library.quarter" 274 }, { 275 "SECOND", "org.hsqldb.Library.second" 276 }, { 277 "WEEK", "org.hsqldb.Library.week" 278 }, { 279 "YEAR", "org.hsqldb.Library.year" 280 }, { 281 "TO_CHAR", "org.hsqldb.Library.to_char" 282 } 283 }; 284 public static final String [][] sSystem = { 285 { 286 "DATABASE", "org.hsqldb.Library.database" 287 }, { 288 "USER", "org.hsqldb.Library.user" 289 }, { 290 "IDENTITY", "org.hsqldb.Library.identity" 291 } 292 }; 293 294 private Library() {} 295 296 static HashMap getAliasMap() { 297 298 HashMap h = new HashMap(83, 1); 299 300 register(h, sNumeric); 301 register(h, sString); 302 register(h, sTimeDate); 303 register(h, sSystem); 304 305 return h; 306 } 307 308 private static void register(HashMap h, String [][] s) { 309 310 for (int i = 0; i < s.length; i++) { 311 h.put(s[i][0], s[i][1]); 312 } 313 } 314 315 private static final Random rRandom = new Random (); 316 317 320 334 public static double rand(Integer seed) { 335 336 synchronized (rRandom) { 346 if (seed != null) { 347 rRandom.setSeed(seed.intValue()); 348 } 349 350 return rRandom.nextDouble(); 351 } 352 } 353 354 359 public static double abs(double d) { 360 return Math.abs(d); 361 } 362 363 private static final double LOG10_FACTOR = 0.43429448190325183; 365 366 371 public static double log10(double x) { 372 return roundMagic(Math.log(x) * LOG10_FACTOR); 373 } 374 375 402 public static double roundMagic(double d) { 403 404 if ((d < 0.0000000000001) && (d > -0.0000000000001)) { 412 return 0.0; 413 } 414 415 if ((d > 1000000000000.) || (d < -1000000000000.)) { 416 return d; 417 } 418 419 StringBuffer s = new StringBuffer (); 420 421 s.append(d); 422 423 int len = s.length(); 424 425 if (len < 16) { 426 return d; 427 } 428 429 char cx = s.charAt(len - 1); 430 char c1 = s.charAt(len - 2); 431 char c2 = s.charAt(len - 3); 432 char c3 = s.charAt(len - 4); 433 434 if ((c1 == '0') && (c2 == '0') && (c3 == '0') && (cx != '.')) { 435 s.setCharAt(len - 1, '0'); 436 } else if ((c1 == '9') && (c2 == '9') && (c3 == '9') && (cx != '.')) { 437 s.setCharAt(len - 1, '9'); 438 s.append('9'); 439 s.append('9'); 440 } 441 442 return Double.valueOf(s.toString()).doubleValue(); 443 } 444 445 451 public static double cot(double d) { 452 return 1. / Math.tan(d); 453 } 454 455 463 public static int mod(int i1, int i2) { 464 return i1 % i2; 465 } 466 467 471 public static double pi() { 472 return Math.PI; 473 } 474 475 484 public static double round(double d, int p) { 485 486 double f = Math.pow(10., p); 487 488 return Math.round(d * f) / f; 489 } 490 491 499 public static int sign(double d) { 500 501 return (d < 0) ? -1 502 : ((d > 0) ? 1 503 : 0); 504 } 505 506 517 public static double truncate(double d, int p) { 518 519 double f = Math.pow(10., p); 520 double g = d * f; 521 522 return ((d < 0) ? Math.ceil(g) 523 : Math.floor(g)) / f; 524 } 525 526 534 public static int bitand(int i, int j) { 535 return i & j; 536 } 537 538 547 public static int bitor(int i, int j) { 548 return i | j; 549 } 550 551 562 public static int bitxor(int i, int j) { 563 return i ^ j; 564 } 565 566 568 576 public static Integer ascii(String s) { 577 578 if ((s == null) || (s.length() == 0)) { 579 return null; 580 } 581 582 return ValuePool.getInt(s.charAt(0)); 583 } 584 585 599 public static String character(int code) { 600 return String.valueOf((char) code); 601 } 602 603 623 public static String concat(String s1, String s2) { 624 625 if (s1 == null) { 626 if (s2 == null) { 627 return null; 628 } 629 630 return s2; 631 } 632 633 if (s2 == null) { 634 return s1; 635 } 636 637 return s1.concat(s2); 638 } 639 640 650 651 public static int difference(String s1, String s2) { 653 654 if ((s1 == null) || (s2 == null)) { 656 return 0; 657 } 658 659 s1 = soundex(s1); 660 s2 = soundex(s2); 661 662 int e = 0; 663 664 for (int i = 0; i < 4; i++) { 665 if (s1.charAt(i) != s2.charAt(i)) { 666 e++; 667 } 668 } 669 670 return e; 671 } 672 673 689 public static String hexToRaw(String s) { 690 691 if (s == null) { 692 return null; 693 } 694 695 char raw; 696 StringBuffer to = new StringBuffer (); 697 int len = s.length(); 698 699 if (len % 4 != 0) { 700 return null; 701 } 702 703 for (int i = 0; i < len; i += 4) { 704 raw = (char) Integer.parseInt(s.substring(i, i + 4), 16); 705 706 to.append(raw); 707 } 708 709 return (to.toString()); 710 } 711 712 745 public static String insert(String s1, int start, int length, String s2) { 746 747 if (s1 == null) { 748 return s2; 749 } 750 751 if (s2 == null) { 752 return s1; 753 } 754 755 int len1 = s1.length(); 756 int len2 = s2.length(); 757 758 start--; 759 760 if (start < 0 || length <= 0 || len2 == 0 || start > len1) { 761 return s1; 762 } 763 764 if (start + length > len1) { 765 length = len1 - start; 766 } 767 768 return s1.substring(0, start) + s2 + s1.substring(start + length); 769 } 770 771 779 public static String lcase(String s) { 780 return (s == null) ? null 781 : s.toLowerCase(); 782 } 783 784 804 public static String left(String s, int count) { 805 806 if (s == null) { 807 return null; 808 } 809 810 return s.substring(0, ((count < 0) ? 0 811 : (count < s.length()) ? count 812 : s.length())); 813 } 814 815 817 824 public static Integer length(String s) { 825 return s == null ? null 826 : ValuePool.getInt(s.length()); 827 } 828 829 837 public static Integer octetLength(String s) { 838 return s == null ? null 839 : ValuePool.getInt(s.length() * 2); 840 } 841 842 850 public static Integer bitLength(String s) { 851 return s == null ? null 852 : ValuePool.getInt(s.length() * 16); 853 } 854 855 876 public static int locate(String search, String s, Integer start) { 877 878 if (s == null || search == null) { 879 return 0; 880 } 881 882 int i = (start == null) ? 0 883 : start.intValue() - 1; 884 885 return s.indexOf(search, (i < 0) ? 0 886 : i) + 1; 887 } 888 889 898 public static int position(String search, String s) { 899 return locate(search, s, null); 900 } 901 902 910 public static String ltrim(String
|