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 s) { 911 912 if (s == null) { 913 return s; 914 } 915 916 int len = s.length(), 917 i = 0; 918 919 while (i < len && s.charAt(i) <= ' ') { 920 i++; 921 } 922 923 return (i == 0) ? s 924 : s.substring(i); 925 } 926 927 937 public static String rawToHex(String s) { 938 939 if (s == null) { 940 return null; 941 } 942 943 char[] from = s.toCharArray(); 944 String hex; 945 StringBuffer to = new StringBuffer (4 * s.length()); 946 947 for (int i = 0; i < from.length; i++) { 948 hex = Integer.toHexString(from[i] & 0xffff); 949 950 for (int j = hex.length(); j < 4; j++) { 951 to.append('0'); 952 } 953 954 to.append(hex); 955 } 956 957 return (to.toString()); 958 } 959 960 968 public static String repeat(String s, Integer count) { 969 970 if (s == null || count == null || count.intValue() < 0) { 971 return null; 972 } 973 974 int i = count.intValue(); 975 StringBuffer b = new StringBuffer (s.length() * i); 976 977 while (i-- > 0) { 978 b.append(s); 979 } 980 981 return b.toString(); 982 } 983 984 986 995 public static String replace(String s, String replace, String with) { 996 997 if (s == null || replace == null) { 998 return s; 999 } 1000 1001 if (with == null) { 1002 with = ""; 1003 } 1004 1005 StringBuffer b = new StringBuffer (); 1006 int start = 0; 1007 int lenreplace = replace.length(); 1008 1009 while (true) { 1010 int i = s.indexOf(replace, start); 1011 1012 if (i == -1) { 1013 b.append(s.substring(start)); 1014 1015 break; 1016 } 1017 1018 b.append(s.substring(start, i)); 1019 b.append(with); 1020 1021 start = i + lenreplace; 1022 } 1023 1024 return b.toString(); 1025 } 1026 1027 1046 public static String right(String s, int count) { 1047 1048 if (s == null) { 1049 return null; 1050 } 1051 1052 count = s.length() - count; 1053 1054 return s.substring((count < 0) ? 0 1055 : (count < s.length()) ? count 1056 : s.length()); 1057 } 1058 1059 1061 1068 public static String rtrim(String s) { 1069 1070 if (s == null) { 1071 return s; 1072 } 1073 1074 int endindex = s.length() - 1; 1075 int i = endindex; 1076 1077 for (; i >= 0 && s.charAt(i) == ' '; i--) {} 1078 1079 return i == endindex ? s 1080 : s.substring(0, i + 1); 1081 } 1082 1083 1108 public static String trim(String s, String trimstr, boolean leading, 1109 boolean trailing) { 1110 1111 if (s == null) { 1112 return s; 1113 } 1114 1115 int trim = trimstr.charAt(0); 1116 int endindex = s.length(); 1117 1118 if (trailing) { 1119 for (--endindex; endindex >= 0 && s.charAt(endindex) == trim; 1120 endindex--) {} 1121 1122 endindex++; 1123 } 1124 1125 if (endindex == 0) { 1126 return ""; 1127 } 1128 1129 int startindex = 0; 1130 1131 if (leading) { 1132 while (startindex < endindex && s.charAt(startindex) == trim) { 1133 startindex++; 1134 } 1135 } 1136 1137 if (startindex == 0 && endindex == s.length()) { 1138 return s; 1139 } else { 1140 return s.substring(startindex, endindex); 1141 } 1142 } 1143 1144 1146 1160 public static String soundex(String s) { 1161 1162 if (s == null) { 1163 return s; 1164 } 1165 1166 s = s.toUpperCase(Locale.ENGLISH); 1167 1168 int len = s.length(); 1169 char[] b = new char[] { 1170 '0', '0', '0', '0' 1171 }; 1172 char lastdigit = '0'; 1173 1174 for (int i = 0, j = 0; i < len && j < 4; i++) { 1175 char c = s.charAt(i); 1176 char newdigit; 1177 1178 if ("AEIOUY".indexOf(c) != -1) { 1179 newdigit = '7'; 1180 } else if (c == 'H' || c == 'W') { 1181 newdigit = '8'; 1182 } else if ("BFPV".indexOf(c) != -1) { 1183 newdigit = '1'; 1184 } else if ("CGJKQSXZ".indexOf(c) != -1) { 1185 newdigit = '2'; 1186 } else if (c == 'D' || c == 'T') { 1187 newdigit = '3'; 1188 } else if (c == 'L') { 1189 newdigit = '4'; 1190 } else if (c == 'M' || c == 'N') { 1191 newdigit = '5'; 1192 } else if (c == 'R') { 1193 newdigit = '6'; 1194 } else { 1195 continue; 1196 } 1197 1198 if (j == 0) { 1199 b[j++] = c; 1200 lastdigit = newdigit; 1201 } else if (newdigit <= '6') { 1202 if (newdigit != lastdigit) { 1203 b[j++] = newdigit; 1204 lastdigit = newdigit; 1205 } 1206 } else if (newdigit == '7') { 1207 lastdigit = newdigit; 1208 } 1209 } 1210 1211 return new String (b, 0, 4); 1212 } 1213 1214 1221 public static String space(int count) { 1222 1223 if (count < 0) { 1224 return null; 1225 } 1226 1227 char[] c = new char[count]; 1228 1229 while (count > 0) { 1230 c[--count] = ' '; 1231 } 1232 1233 return new String (c); 1234 } 1235 1236 1271 1272 public static String substring(final String s, int start, 1275 final Integer length) { 1276 1277 if (s == null) { 1278 return null; 1279 } 1280 1281 int sl = s.length(); 1282 int ol = (length == null) ? sl 1283 : length.intValue(); 1284 1285 if (ol < 1) { 1286 return null; 1287 } 1288 1289 if (start < 0) { 1290 start = sl + start; 1291 } else if (start > 0) { 1292 start--; 1293 } 1294 1295 if (start < 0 || start >= sl) { 1296 return null; 1297 } else if (start > sl - ol) { 1298 ol = sl - start; 1299 } 1300 1301 return s.substring(start, start + ol); 1302 } 1303 1304 1311 public static String ucase(String s) { 1312 return (s == null) ? null 1313 : s.toUpperCase(); 1314 } 1315 1316 1318 1325 public static Date curdate(Connection c) { 1326 return null; 1327 } 1328 1329 1336 public static Time curtime(Connection c) { 1337 return null; 1338 } 1339 1340 1348 public static String dayname(Date d) { 1349 1350 if (d == null) { 1351 return null; 1352 } 1353 1354 synchronized (daynameBuffer) { 1355 daynameBuffer.setLength(0); 1356 1357 return daynameFormat.format(d, daynameBuffer, 1358 dayPosition).toString(); 1359 } 1360 } 1361 1362 1369 public static Integer dayofmonth(Date d) { 1370 1371 if (d == null) { 1372 return null; 1373 } 1374 1375 return ValuePool.getInt(HsqlDateTime.getDateTimePart(d, 1376 Calendar.DAY_OF_MONTH)); 1377 } 1378 1379 1386 public static Integer dayofweek(Date d) { 1387 1388 if (d == null) { 1389 return null; 1390 } 1391 1392 return ValuePool.getInt(HsqlDateTime.getDateTimePart(d, 1393 Calendar.DAY_OF_WEEK)); 1394 } 1395 1396 1403 public static Integer dayofyear(Date d) { 1404 1405 if (d == null) { 1406 return null; 1407 } 1408 1409 return ValuePool.getInt(HsqlDateTime.getDateTimePart(d, 1410 Calendar.DAY_OF_YEAR)); 1411 } 1412 1413 1420 1421 public static Integer hour(Time t) { 1423 1424 if (t == null) { 1425 return null; 1426 } 1427 1428 return ValuePool.getInt(HsqlDateTime.getDateTimePart(t, 1429 Calendar.HOUR_OF_DAY)); 1430 } 1431 1432 1439 public static Integer minute(Time t) { 1440 1441 if (t == null) { 1442 return null; 1443 } 1444 1445 return ValuePool.getInt(HsqlDateTime.getDateTimePart(t, 1446 Calendar.MINUTE)); 1447 } 1448 1449 1459 public static Integer month(Date d) { 1460 1461 if (d == null) { 1462 return null; 1463 } 1464 1465 return ValuePool.getInt( 1466 HsqlDateTime.getDateTimePart(d, Calendar.MONTH) + 1); 1467 } 1468 1469 1478 public static String monthname(Date d) { 1479 1480 if (d == null) { 1481 return null; 1482 } 1483 1484 synchronized (monthnameBuffer) { 1485 monthnameBuffer.setLength(0); 1486 1487 return monthnameFormat.format(d, monthnameBuffer, 1488 monthPosition).toString(); 1489 } 1490 } 1491 1492 1499 public static Timestamp now(Connection c) { 1500 return null; 1501 } 1502 1503 1511 public static Integer quarter(Date d) { 1512 1513 if (d == null) { 1514 return null; 1515 } 1516 1517 return ValuePool.getInt( 1518 (HsqlDateTime.getDateTimePart(d, Calendar.MONTH) / 3) + 1); 1519 } 1520 1521 1529 public static Integer second(Time d) { 1530 1531 if (d == null) { 1532 return null; 1533 } 1534 1535 return ValuePool.getInt(HsqlDateTime.getDateTimePart(d, 1536 Calendar.SECOND)); 1537 } 1538 1539 1547 public static Integer week(Date d) { 1548 1549 if (d == null) { 1550 return null; 1551 } 1552 1553 return ValuePool.getInt(HsqlDateTime.getDateTimePart(d, 1554 Calendar.WEEK_OF_YEAR)); 1555 } 1556 1557 1565 public static Integer year(Date d) { 1566 1567 if (d == null) { 1568 return null; 1569 } 1570 1571 return ValuePool.getInt(HsqlDateTime.getDateTimePart(d, 1572 Calendar.YEAR)); 1573 } 1574 1575 1578 public static String to_char(java.util.Date d, String format) { 1579 1580 if (d == null || format == null) { 1581 return null; 1582 } 1583 1584 synchronized (tocharFormat) { 1585 tocharFormat.applyPattern(HsqlDateTime.toJavaDatePattern(format)); 1586 1587 return tocharFormat.format(d); 1588 } 1589 } 1590 1591 1593 1612 public static Long datediff(String datepart, Timestamp d1, 1613 Timestamp d2) throws HsqlException { 1614 1615 if (d1 == null || d2 == null) { 1617 return null; 1618 } 1619 1620 if ("yy".equalsIgnoreCase(datepart) 1621 || "year".equalsIgnoreCase(datepart)) { 1622 return ValuePool.getLong(getElapsed(Calendar.YEAR, d1, d2)); 1623 } else if ("mm".equalsIgnoreCase(datepart) 1624 || "month".equalsIgnoreCase(datepart)) { 1625 return ValuePool.getLong(getElapsed(Calendar.MONTH, d1, d2)); 1626 } else if ("dd".equalsIgnoreCase(datepart) 1627 || "day".equalsIgnoreCase(datepart)) { 1628 return ValuePool.getLong(getElapsed(Calendar.DATE, d1, d2)); 1629 } else if ("hh".equalsIgnoreCase(datepart) 1630 || "hour".equalsIgnoreCase(datepart)) { 1631 return ValuePool.getLong(getElapsed(Calendar.HOUR, d1, d2)); 1632 } else if ("mi".equalsIgnoreCase(datepart) 1633 || "minute".equalsIgnoreCase(datepart)) { 1634 return ValuePool.getLong(getElapsed(Calendar.MINUTE, d1, d2)); 1635 } else if ("ss".equalsIgnoreCase(datepart) 1636 || "second".equalsIgnoreCase(datepart)) { 1637 return ValuePool.getLong(getElapsed(Calendar.SECOND, d1, d2)); 1638 } else if ("ms".equalsIgnoreCase(datepart) 1639 || "millisecond".equalsIgnoreCase(datepart)) { 1640 return ValuePool.getLong(getElapsed(Calendar.MILLISECOND, d1, 1641 d2)); 1642 } else { 1643 throw Trace.error(Trace.INVALID_CONVERSION); 1644 } 1645 } 1646 1647 1659 private static long getElapsed(int field, java.util.Date d1, 1660 java.util.Date d2) { 1661 1662 if (field == Calendar.MILLISECOND) { 1664 return d2.getTime() - d1.getTime(); 1665 } 1666 1667 Calendar g1 = Calendar.getInstance(), 1669 g2 = Calendar.getInstance(); 1670 1671 g1.setTime(d1); 1672 g2.setTime(d2); 1673 g1.set(Calendar.MILLISECOND, 0); 1674 g2.set(Calendar.MILLISECOND, 0); 1675 1676 if (field == Calendar.SECOND) { 1677 return (g2.getTime().getTime() - g1.getTime().getTime()) / 1000; 1678 } 1679 1680 g1.set(Calendar.SECOND, 0); 1681 g2.set(Calendar.SECOND, 0); 1682 1683 if (field == Calendar.MINUTE) { 1684 return (g2.getTime().getTime() - g1.getTime().getTime()) 1685 / (1000 * 60); 1686 } 1687 1688 g1.set(Calendar.MINUTE, 0); 1689 g2.set(Calendar.MINUTE, 0); 1690 1691 if (field == Calendar.HOUR) { 1692 return (g2.getTime().getTime() - g1.getTime().getTime()) 1693 / (1000 * 60 * 60); 1694 } 1696 long elapsed = 0; 1698 short sign = 1; 1699 1700 if (g2.before(g1)) { 1701 sign = -1; 1702 1703 Calendar tmp = g1; 1704 1705 g1 = g2; 1706 g2 = tmp; 1707 } 1709 g1.set(Calendar.HOUR_OF_DAY, 0); 1710 g2.set(Calendar.HOUR_OF_DAY, 0); 1711 1712 if (field == Calendar.MONTH || field == Calendar.YEAR) { 1713 g1.set(Calendar.DATE, 1); 1714 g2.set(Calendar.DATE, 1); 1715 } 1716 1717 if (field == Calendar.YEAR) { 1718 g1.set(Calendar.MONTH, 1); 1719 g2.set(Calendar.MONTH, 1); 1720 } 1722 while (g1.before(g2)) { 1724 g1.add(field, 1); 1725 1726 elapsed++; 1727 } 1728 1729 return sign * elapsed; 1730 } 1732 1737 1738 1745 public static String database(Connection conn) throws HsqlException { 1746 return null; 1747 } 1748 1749 1757 public static String user(Connection conn) throws HsqlException { 1758 return null; 1759 } 1760 1761 1770 public static int identity() throws HsqlException { 1771 return 0; 1772 } 1773 1774 1776 1784 public static boolean getAutoCommit(Connection conn) { 1785 return false; 1786 } 1787 1788 1794 public static String getDatabaseFullProductVersion() { 1795 return HsqlDatabaseProperties.THIS_FULL_VERSION; 1796 } 1797 1798 1804 public static String getDatabaseProductName() { 1805 return HsqlDatabaseProperties.PRODUCT_NAME; 1806 } 1807 1808 1814 public static String getDatabaseProductVersion() { 1815 return HsqlDatabaseProperties.THIS_VERSION; 1816 } 1817 1818 1824 public static int getDatabaseMajorVersion() { 1825 return HsqlDatabaseProperties.MAJOR; 1826 } 1827 1828 1834 public static int getDatabaseMinorVersion() { 1835 return HsqlDatabaseProperties.MINOR; 1836 } 1837 1838 1849 public static boolean isReadOnlyConnection(Connection conn) { 1850 return false; 1851 } 1852 1853 1861 public static boolean isReadOnlyDatabase(Connection c) { 1862 return false; 1863 } 1864 1865 1875 public static boolean isReadOnlyDatabaseFiles(Connection c) { 1876 return false; 1877 } 1878 1879 static final int abs = 0; 1880 static final int ascii = 1; 1881 static final int bitand = 2; 1882 static final int bitLength = 3; 1883 static final int bitor = 4; 1884 static final int bitxor = 5; 1885 static final int character = 6; 1886 static final int concat = 7; 1887 static final int cot = 8; 1888 static final int curdate = 9; 1889 static final int curtime = 10; 1890 static final int database = 11; 1891 static final int datediff = 12; 1892 static final int day = 13; 1893 static final int dayname = 14; 1894 static final int dayofmonth = 15; 1895 static final int dayofweek = 16; 1896 static final int dayofyear = 17; 1897 static final int difference = 18; 1898 static final int getAutoCommit = 19; 1899 static final int getDatabaseFullProductVersion = 20; 1900 static final int getDatabaseMajorVersion = 21; 1901 static final int getDatabaseMinorVersion = 22; 1902 static final int getDatabaseProductName = 23; 1903 static final int getDatabaseProductVersion = 24; 1904 static final int hexToRaw = 25; 1905 static final int hour = 26; 1906 static final int identity = 27; 1907 static final int insert = 28; 1908 static final int isReadOnlyConnection = 29; 1909 static final int isReadOnlyDatabase = 30; 1910 static final int isReadOnlyDatabaseFiles = 31; 1911 static final int lcase = 32; 1912 static final int left = 33; 1913 static final int length = 34; 1914 static final int locate = 35; 1915 static final int log10 = 36; 1916 static final int ltrim = 37; 1917 static final int minute = 38; 1918 static final int mod = 39; 1919 static final int month = 40; 1920 static final int monthname = 41; 1921 static final int now = 42; 1922 static final int octetLength = 43; 1923 static final int pi = 44; 1924 static final int position = 45; 1925 static final int quarter = 46; 1926 static final int rand = 47; 1927 static final int rawToHex = 48; 1928 static final int repeat = 49; 1929 static final int replace = 50; 1930 static final int right = 51; 1931 static final int round = 52; 1932 static final int roundMagic = 53; 1933 static final int rtrim = 54; 1934 static final int second = 55; 1935 static final int sign = 56; 1936 static final int soundex = 57; 1937 static final int space = 58; 1938 static final int substring = 59; 1939 static final int to_char = 60; 1940 static final int trim = 61; 1941 static final int truncate = 62; 1942 static final int ucase = 63; 1943 static final int user = 64; 1944 static final int week = 65; 1945 static final int year = 66; 1946 1947 1948 1949 private static final IntValueHashMap functionMap = 1951 new IntValueHashMap(67); 1952 static final Double piValue = new Double (Library.pi()); 1953 1954 static { 1955 functionMap.put("abs", abs); 1956 functionMap.put("ascii", ascii); 1957 functionMap.put("bitand", bitand); 1958 functionMap.put("bitlength", bitLength); 1959 functionMap.put("bitor", bitor); 1960 functionMap.put("bitxor", bitor); 1961 functionMap.put("character", character); 1962 functionMap.put("concat", concat); 1963 functionMap.put("cot", cot); 1964 functionMap.put("curdate", curdate); 1965 functionMap.put("curtime", curtime); 1966 functionMap.put("database", database); 1967 functionMap.put("datediff", datediff); 1968 functionMap.put("dayname", dayname); 1969 functionMap.put("day", day); 1970 functionMap.put("dayofmonth", dayofmonth); 1971 functionMap.put("dayofweek", dayofweek); 1972 functionMap.put("dayofyear", dayofyear); 1973 functionMap.put("difference", difference); 1974 functionMap.put("getAutoCommit", getAutoCommit); 1975 functionMap.put("getDatabaseFullProductVersion", 1976 getDatabaseFullProductVersion); 1977 functionMap.put("getDatabaseMajorVersion", getDatabaseMajorVersion); 1978 functionMap.put("getDatabaseMinorVersion", getDatabaseMinorVersion); 1979 functionMap.put("getDatabaseProductName", getDatabaseProductName); 1980 functionMap.put("getDatabaseProductVersion", 1981 getDatabaseProductVersion); 1982 functionMap.put("hexToRaw", hexToRaw); 1983 functionMap.put("hour", hour); 1984 functionMap.put("identity", identity); 1985 functionMap.put("insert", insert); 1986 functionMap.put("isReadOnlyConnection", isReadOnlyConnection); 1987 functionMap.put("isReadOnlyDatabase", isReadOnlyDatabase); 1988 functionMap.put("isReadOnlyDatabaseFiles", isReadOnlyDatabaseFiles); 1989 functionMap.put("lcase", lcase); 1990 functionMap.put("left", left); 1991 functionMap.put("length", length); 1992 functionMap.put("locate", locate); 1993 functionMap.put("log10", log10); 1994 functionMap.put("ltrim", ltrim); 1995 functionMap.put("minute", minute); 1996 functionMap.put("mod", mod); 1997 functionMap.put("month", month); 1998 functionMap.put("monthname", monthname); 1999 functionMap.put("now", now); 2000 functionMap.put("octetLength", octetLength); 2001 functionMap.put("pi", pi); 2002 functionMap.put("position", position); 2003 functionMap.put("quarter", quarter); 2004 functionMap.put("rand", rand); 2005 functionMap.put("rawToHex", rawToHex); 2006 functionMap.put("repeat", repeat); 2007 functionMap.put("replace", replace); 2008 functionMap.put("right", right); 2009 functionMap.put("round", round); 2010 functionMap.put("roundMagic", roundMagic); 2011 functionMap.put("rtrim", rtrim); 2012 functionMap.put("second", second); 2013 functionMap.put("sign", sign); 2014 functionMap.put("soundex", soundex); 2015 functionMap.put("space", space); 2016 functionMap.put("substring", substring); 2017 functionMap.put("to_char", to_char); 2018 functionMap.put("trim", trim); 2019 functionMap.put("truncate", truncate); 2020 functionMap.put("ucase", ucase); 2021 functionMap.put("user", user); 2022 functionMap.put("week", week); 2023 functionMap.put("year", year); 2024 } 2025 2026 static Object invoke(int fID, Object [] params) throws HsqlException { 2027 2028 try { 2029 switch (fID) { 2030 2031 case abs : { 2032 return new Double ( 2033 Library.abs(((Number ) params[0]).doubleValue())); 2034 } 2035 case ascii : { 2036 return ascii((String ) params[0]); 2037 } 2038 case bitand : { 2039 return ValuePool.getInt( 2040 bitand(((Number ) params[0]).intValue(), 2041 ((Number ) params[1]).intValue())); 2042 } 2043 case bitLength : { 2044 return bitLength((String ) params[0]); 2045 } 2046 case bitor : { 2047 return ValuePool.getInt( 2048 bitor(((Number ) params[0]).intValue(), 2049 ((Number ) params[1]).intValue())); 2050 } 2051 case bitxor : { 2052 return ValuePool.getInt( 2053 bitxor(((Number ) params[0]).intValue(), 2054 ((Number ) params[1]).intValue())); 2055 } 2056 case character : { 2057 return character(((Number ) params[0]).intValue()); 2058 } 2059 case concat : { 2060 return concat((String ) params[0], (String ) params[1]); 2061 } 2062 case cot : { 2063 return new Double ( 2064 cot(((Number ) params[0]).doubleValue())); 2065 } 2066 case curdate : { 2067 return null; 2068 } 2069 case curtime : { 2070 return null; 2071 } 2072 case database : { 2073 return null; 2074 } 2075 case datediff : { 2076 return datediff((String ) params[0], 2077 (Timestamp ) params[1], 2078 (Timestamp ) params[2]); 2079 } 2080 case dayname : { 2081 return dayname((Date ) params[0]); 2082 } 2083 case dayofmonth : 2084 case day : { 2085 return dayofmonth((Date ) params[0]); 2086 } 2087 case dayofweek : { 2088 return dayofweek((Date ) params[0]); 2089 } 2090 case dayofyear : { 2091 return dayofyear((Date ) params[0]); 2092 } 2093 case difference : { 2094 return ValuePool.getInt(difference((String ) params[0], 2095 (String ) params[1])); 2096 } 2097 case getAutoCommit : { 2098 return null; 2099 } 2100 case getDatabaseFullProductVersion : { 2101 return getDatabaseFullProductVersion(); 2102 } 2103 case getDatabaseMajorVersion : { 2104 return ValuePool.getInt(getDatabaseMajorVersion()); 2105 } 2106 case getDatabaseMinorVersion : { 2107 return ValuePool.getInt(getDatabaseMinorVersion()); 2108 } 2109 case getDatabaseProductName : { 2110 return getDatabaseProductName(); 2111 } 2112 case getDatabaseProductVersion : { 2113 return getDatabaseProductVersion(); 2114 } 2115 case hexToRaw : { 2116 return hexToRaw((String ) params[0]); 2117 } 2118 case hour : { 2119 return hour((Time ) params[0]); 2120 } 2121 case identity : { 2122 return null; 2123 } 2124 case insert : { 2125 return insert((String ) params[0], 2126 ((Number ) params[1]).intValue(), 2127 ((Number ) params[2]).intValue(), 2128 (String ) params[3]); 2129 } 2130 case isReadOnlyConnection : { 2131 return null; 2132 } 2133 case isReadOnlyDatabase : { 2134 return null; 2135 } 2136 case lcase : { 2137 return lcase((String ) params[0]); 2138 } 2139 case left : { 2140 return left((String ) params[0], 2141 ((Number ) params[1]).intValue()); 2142 } 2143 case length : { 2144 return length((String ) params[0]); 2145 } 2146 case locate : { 2147 return ValuePool.getInt(locate((String ) params[0], 2148 (String ) params[1], 2149 (Integer ) params[2])); 2150 } 2151 case log10 : { 2152 return new Double ( 2153 log10(((Number ) params[0]).doubleValue())); 2154 } 2155 case ltrim : { 2156 return ltrim((String ) params[0]); 2157 } 2158 case minute : { 2159 return minute((Time ) params[0]); 2160 } 2161 case mod : { 2162 return ValuePool.getInt( 2163 mod( 2164 ((Number ) params[0]).intValue(), 2165 ((Number ) params[1]).intValue())); 2166 } 2167 case month : { 2168 return month((Date ) params[0]); 2169 } 2170 case monthname : { 2171 return ValuePool.getString(monthname((Date ) params[0])); 2172 } 2173 case now : { 2174 return null; 2175 } 2176 case octetLength : { 2177 return octetLength((String ) params[0]); 2178 } 2179 case position : { 2180 return ValuePool.getInt(position((String ) params[0], 2181 (String ) params[1])); 2182 } 2183 case pi : { 2184 return piValue; 2185 } 2186 case quarter : { 2187 return quarter((Date ) params[0]); 2188 } 2189 case rand : { 2190 return new Double (rand((Integer ) params[0])); 2191 } 2192 case rawToHex : { 2193 return rawToHex((String ) params[0]); 2194 } 2195 case repeat : { 2196 return repeat((String ) params[0], (Integer ) params[1]); 2197 } 2198 case replace : { 2199 return replace((String ) params[0], (String ) params[1], 2200 (String ) params[2]); 2201 } 2202 case right : { 2203 return right((String ) params[0], 2204 ((Number ) params[1]).intValue()); 2205 } 2206 case round : { 2207 return new Double ( 2208 round(((Number ) params[0]).doubleValue(), 2209 ((Number ) params[1]).intValue())); 2210 } 2211 case roundMagic : { 2212 return new Double ( 2213 roundMagic(((Number ) params[0]).doubleValue())); 2214 } 2215 case rtrim : { 2216 return rtrim((String ) params[0]); 2217 } 2218 case second : { 2219 return second((Time ) params[0]); 2220 } 2221 case sign : { 2222 return ValuePool.getInt( 2223 sign(((Number ) params[0]).doubleValue())); 2224 } 2225 case soundex : { 2226 return soundex((String ) params[0]); 2227 } 2228 case space : { 2229 return space(((Number ) params[0]).intValue()); 2230 } 2231 case substring : { 2232 return substring((String ) params[0], 2233 ((Number ) params[1]).intValue(), 2234 (Integer ) params[2]); 2235 } 2236 case trim : { 2237 return trim((String ) params[0], (String ) params[1], 2238 ((Boolean ) params[2]).booleanValue(), 2239 ((Boolean ) params[3]).booleanValue()); 2240 } 2241 case truncate : { 2242 return new Double ( 2243 truncate( 2244 ((Number ) params[0]).doubleValue(), 2245 ((Number ) params[1]).intValue())); 2246 } 2247 case ucase : { 2248 return ucase((String ) params[0]); 2249 } 2250 case user : { 2251 return null; 2252 } 2253 case week : { 2254 return week((Date ) params[0]); 2255 } 2256 case year : { 2257 return year((Date ) params[0]); 2258 } 2259 case to_char : { 2260 return to_char((java.util.Date ) params[0], 2261 (String ) params[1]); 2262 } 2263 case isReadOnlyDatabaseFiles : { 2264 return null; 2265 } 2266 default : { 2267 2268 Trace.doAssert(false); 2270 2271 return null; 2272 } 2273 } 2274 } catch (Exception e) { 2275 throw Trace.error(Trace.FUNCTION_CALL_ERROR, e.toString()); 2276 } 2277 } 2278 2279 static final String prefix = "org.hsqldb.Library."; 2280 static final int prefixLength = prefix.length(); 2281 2282 static int functionID(String fname) { 2283 2284 return fname.startsWith(prefix) 2285 ? functionMap.get(fname.substring(prefixLength), -1) 2286 : -1; 2287 } 2288} 2289 | Popular Tags |