📄 library.java
字号:
/* Copyrights and Licenses * * This product includes Hypersonic SQL. * Originally developed by Thomas Mueller and the Hypersonic SQL Group. * * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved. * Redistribution and use in source and binary forms, with or without modification, are permitted * provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, this list of conditions * and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * - All advertising materials mentioning features or use of this software must display the * following acknowledgment: "This product includes Hypersonic SQL." * - Products derived from this software may not be called "Hypersonic SQL" nor may * "Hypersonic SQL" appear in their names without prior written permission of the * Hypersonic SQL Group. * - Redistributions of any form whatsoever must retain the following acknowledgment: "This * product includes Hypersonic SQL." * This software is provided "as is" and any expressed or implied warranties, including, but * not limited to, the implied warranties of merchantability and fitness for a particular purpose are * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any * direct, indirect, incidental, special, exemplary, or consequential damages (including, but * not limited to, procurement of substitute goods or services; loss of use, data, or profits; * or business interruption). However caused any on any theory of liability, whether in contract, * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this * software, even if advised of the possibility of such damage. * This software consists of voluntary contributions made by many individuals on behalf of the * Hypersonic SQL Group. * * * For work added by the HSQL Development Group: * * Copyright (c) 2001-2002, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer, including earlier * license statements (above) and comply with all above license conditions. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution, including earlier * license statements (above) and comply with all above license conditions. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb;import java.sql.SQLException;import java.sql.Timestamp;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Hashtable;import java.util.Random;import java.text.SimpleDateFormat;/** * Class declaration * * * @version 1.7.0 */// fredt@users 20020210 - patch 513005 by sqlbob@users (RMP) - ABS function// fredt@users 20020305 - patch 1.7.0 - change to 2D string arrays// sqlbob@users 20020420- patch 1.7.0 - added HEXTORAW and RAWTOHEX.class Library { static final String sNumeric[][] = { { "ABS", "org.hsqldb.Library.abs" }, { "ACOS", "java.lang.Math.acos" }, { "ASIN", "java.lang.Math.asin" }, { "ATAN", "java.lang.Math.atan" }, { "ATAN2", "java.lang.Math.atan2" }, { "CEILING", "java.lang.Math.ceil" }, { "COS", "java.lang.Math.cos" }, { "COT", "org.hsqldb.Library.cot" }, { "DEGREES", "java.lang.Math.toDegrees" }, { "EXP", "java.lang.Math.exp" }, { "FLOOR", "java.lang.Math.floor" }, { "LOG", "java.lang.Math.log" }, { "LOG10", "org.hsqldb.Library.log10" }, { "MOD", "org.hsqldb.Library.mod" }, { "PI", "org.hsqldb.Library.pi" }, { "POWER", "java.lang.Math.pow" }, { "RADIANS", "java.lang.Math.toRadians" }, { "RAND", "java.lang.Math.random" }, { "ROUND", "org.hsqldb.Library.round" }, { "SIGN", "org.hsqldb.Library.sign" }, { "SIN", "java.lang.Math.sin" }, { "SQRT", "java.lang.Math.sqrt" }, { "TAN", "java.lang.Math.tan" }, { "TRUNCATE", "org.hsqldb.Library.truncate" }, { "BITAND", "org.hsqldb.Library.bitand" }, { "BITOR", "org.hsqldb.Library.bitor" }, { "ROUNDMAGIC", "org.hsqldb.Library.roundMagic" } };// fredt@users 20010701 - patch 418023 by deforest@users// the definition for SUBSTR was added static final String sString[][] = { { "ASCII", "org.hsqldb.Library.ascii" }, { "CHAR", "org.hsqldb.Library.character" }, { "CONCAT", "org.hsqldb.Library.concat" }, { "DIFFERENCE", "org.hsqldb.Library.difference" }, { "HEXTORAW", "org.hsqldb.Library.hexToRaw" }, { "INSERT", "org.hsqldb.Library.insert" }, { "LCASE", "org.hsqldb.Library.lcase" }, { "LEFT", "org.hsqldb.Library.left" }, { "LENGTH", "org.hsqldb.Library.length" }, { "LOCATE", "org.hsqldb.Library.locate" }, { "LTRIM", "org.hsqldb.Library.ltrim" }, { "RAWTOHEX", "org.hsqldb.Library.rawToHex" }, { "REPEAT", "org.hsqldb.Library.repeat" }, { "REPLACE", "org.hsqldb.Library.replace" }, { "RIGHT", "org.hsqldb.Library.right" }, { "RTRIM", "org.hsqldb.Library.rtrim" }, { "SOUNDEX", "org.hsqldb.Library.soundex" }, { "SPACE", "org.hsqldb.Library.space" }, { "SUBSTR", "org.hsqldb.Library.substring" }, { "SUBSTRING", "org.hsqldb.Library.substring" }, { "UCASE", "org.hsqldb.Library.ucase" }, { "LOWER", "org.hsqldb.Library.lcase" }, { "UPPER", "org.hsqldb.Library.ucase" } }; static final String sTimeDate[][] = { { "CURDATE", "org.hsqldb.Library.curdate" }, { "CURTIME", "org.hsqldb.Library.curtime" }, { "DAYNAME", "org.hsqldb.Library.dayname" }, { "DAYOFMONTH", "org.hsqldb.Library.dayofmonth" }, { "DAYOFWEEK", "org.hsqldb.Library.dayofweek" }, { "DAYOFYEAR", "org.hsqldb.Library.dayofyear" }, { "HOUR", "org.hsqldb.Library.hour" }, { "MINUTE", "org.hsqldb.Library.minute" }, { "MONTH", "org.hsqldb.Library.month" }, { "MONTHNAME", "org.hsqldb.Library.monthname" }, { "NOW", "org.hsqldb.Library.now" }, { "QUARTER", "org.hsqldb.Library.quarter" }, { "SECOND", "org.hsqldb.Library.second" }, { "WEEK", "org.hsqldb.Library.week" }, { "YEAR", "org.hsqldb.Library.year" } }; static final String sSystem[][] = { { "DATABASE", "org.hsqldb.Library.database" }, { "USER", "org.hsqldb.Library.user" }, { "IDENTITY", "org.hsqldb.Library.identity" } }; /** * Method declaration * * * @param h */ static void register(Hashtable h) { register(h, sNumeric); register(h, sString); register(h, sTimeDate); register(h, sSystem); } /** * Method declaration * * * @param h * @param s */ private static void register(Hashtable h, String s[][]) { for (int i = 0; i < s.length; i++) { h.put(s[i][0], s[i][1]); } } private static final Random rRandom = new Random(); // NUMERIC /** * Method declaration * * * @param i * * @return */// fredt@users 20020220 - patch 489184 by xclayl@users - thread safety public static synchronized double rand(Integer i) { if (i != null) { rRandom.setSeed(i.intValue()); } return rRandom.nextDouble(); } /** * Method declaration * * * @param d * @param p * * @return */ public static double abs(double d) { return Math.abs(d); } // this magic number works for 100000000000000; but not for 0.1 and 0.01 private static final double LOG10_FACTOR = 0.43429448190325183; /** * Method declaration * * * @param x * * @return */ public static double log10(double x) { return roundMagic(Math.log(x) * LOG10_FACTOR); } /** * Method declaration * * * @param d * * @return */ public static double roundMagic(double d) { // this function rounds numbers in a good way but slow: // - special handling for numbers around 0 // - only numbers <= +/-1000000000000 // - convert to a string // - check the last 4 characters: // '000x' becomes '0000' // '999x' becomes '999999' (this is rounded automatically) if ((d < 0.0000000000001) && (d > -0.0000000000001)) { return 0.0; } if ((d > 1000000000000.) || (d < -1000000000000.)) { return d; } StringBuffer s = new StringBuffer(); s.append(d); int len = s.length(); if (len < 16) { return d; } char cx = s.charAt(len - 1); char c1 = s.charAt(len - 2); char c2 = s.charAt(len - 3); char c3 = s.charAt(len - 4); if ((c1 == '0') && (c2 == '0') && (c3 == '0') && (cx != '.')) { s.setCharAt(len - 1, '0'); } else if ((c1 == '9') && (c2 == '9') && (c3 == '9') && (cx != '.')) { s.setCharAt(len - 1, '9'); s.append('9'); s.append('9'); } return Double.valueOf(s.toString()).doubleValue(); } /** * Method declaration * * * @param d * * @return */ public static double cot(double d) { return 1. / Math.tan(d); } /** * Method declaration * * * @param i1 * @param i2 * * @return */ public static int mod(int i1, int i2) { return i1 % i2; } /** * Method declaration * * * @return */ public static double pi() { return Math.PI; } /** * Method declaration * * * @param d * @param p * * @return */ public static double round(double d, int p) { double f = Math.pow(10., p); return Math.round(d * f) / f; } /** * Method declaration * * * @param d * * @return */ public static int sign(double d) { return (d < 0) ? -1 : ((d > 0) ? 1 : 0); } /** * Method declaration * * * @param d * @param p * * @return */ public static double truncate(double d, int p) { double f = Math.pow(10., p); double g = d * f; return ((d < 0) ? Math.ceil(g) : Math.floor(g)) / f; } /** * Method declaration * * * @param i * @param j * * @return */ public static int bitand(int i, int j) { return i & j; } /** * Method declaration * * * @param i * @param j * * @return */ public static int bitor(int i, int j) { return i | j; } // STRING /** * Method declaration * * * @param s * * @return */ public static Integer ascii(String s) { if ((s == null) || (s.length() == 0)) { return null; } return new Integer(s.charAt(0)); } /** * Method declaration * * * @param code * * @return */ public static String character(int code) { return String.valueOf((char) code); } /** * Method declaration * * * @param s1 * @param s2 * * @return */ public static String concat(String s1, String s2) { if (s1 == null) { if (s2 == null) { return null; } return s2; } if (s2 == null) { return s1; } return s1 + s2; } /** * Method declaration * * * @param s1 * @param s2 * * @return */// fredt@users 20020305 - patch 460907 by fredt - soundex public static int difference(String s1, String s2) { // todo: check if this is the standard algorithm if ((s1 == null) || (s2 == null)) { return 0; } s1 = soundex(s1); s2 = soundex(s2); int e = 0; for (int i = 0; i < 4; i++) { if (s1.charAt(i) != s2.charAt(i)) { e++; } } return e; } /** * Method declaration * * * @param s * * @return */ public static String hexToRaw(String s) { char raw; StringBuffer to = new StringBuffer(); int len = s.length(); if (len % 4 != 0) { return null; } for (int i = 0; i < len; i += 4) { raw = (char) Integer.parseInt(s.substring(i, i + 4), 16); to.append(raw); } return (to.toString()); } /** * Method declaration * * * @param s1 * @param start * @param length * @param s2 * * @return */ public static String insert(String s1, int start, int length, String s2) { if (s1 == null) { return s2; } if (s2 == null) { return s1; } int len1 = s1.length(); int len2 = s2.length(); start--; if (start < 0 || length <= 0 || len2 == 0 || start > len1) { return s1; } if (start + length > len1) { length = len1 - start; } return s1.substring(0, start) + s2 + s1.substring(start + length); } /** * Method declaration * * * @param s * * @return */ public static String lcase(String s) { return (s == null) ? null : s.toLowerCase(); } /** * Method declaration * * * @param s * @param i * * @return */ public static String left(String s, int i) { if (s == null) { return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -