📄 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 net.jumperz.ext.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", "net.jumperz.ext.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", "net.jumperz.ext.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", "net.jumperz.ext.org.hsqldb.Library.log10" }, { "MOD", "net.jumperz.ext.org.hsqldb.Library.mod" }, { "PI", "net.jumperz.ext.org.hsqldb.Library.pi" }, { "POWER", "java.lang.Math.pow" }, { "RADIANS", "java.lang.Math.toRadians" }, { "RAND", "java.lang.Math.random" }, { "ROUND", "net.jumperz.ext.org.hsqldb.Library.round" }, { "SIGN", "net.jumperz.ext.org.hsqldb.Library.sign" }, { "SIN", "java.lang.Math.sin" }, { "SQRT", "java.lang.Math.sqrt" }, { "TAN", "java.lang.Math.tan" }, { "TRUNCATE", "net.jumperz.ext.org.hsqldb.Library.truncate" }, { "BITAND", "net.jumperz.ext.org.hsqldb.Library.bitand" }, { "BITOR", "net.jumperz.ext.org.hsqldb.Library.bitor" }, { "ROUNDMAGIC", "net.jumperz.ext.org.hsqldb.Library.roundMagic" } };// fredt@users 20010701 - patch 418023 by deforest@users// the definition for SUBSTR was added static final String sString[][] = { { "ASCII", "net.jumperz.ext.org.hsqldb.Library.ascii" }, { "CHAR", "net.jumperz.ext.org.hsqldb.Library.character" }, { "CONCAT", "net.jumperz.ext.org.hsqldb.Library.concat" }, { "DIFFERENCE", "net.jumperz.ext.org.hsqldb.Library.difference" }, { "HEXTORAW", "net.jumperz.ext.org.hsqldb.Library.hexToRaw" }, { "INSERT", "net.jumperz.ext.org.hsqldb.Library.insert" }, { "LCASE", "net.jumperz.ext.org.hsqldb.Library.lcase" }, { "LEFT", "net.jumperz.ext.org.hsqldb.Library.left" }, { "LENGTH", "net.jumperz.ext.org.hsqldb.Library.length" }, { "LOCATE", "net.jumperz.ext.org.hsqldb.Library.locate" }, { "LTRIM", "net.jumperz.ext.org.hsqldb.Library.ltrim" }, { "RAWTOHEX", "net.jumperz.ext.org.hsqldb.Library.rawToHex" }, { "REPEAT", "net.jumperz.ext.org.hsqldb.Library.repeat" }, { "REPLACE", "net.jumperz.ext.org.hsqldb.Library.replace" }, { "RIGHT", "net.jumperz.ext.org.hsqldb.Library.right" }, { "RTRIM", "net.jumperz.ext.org.hsqldb.Library.rtrim" }, { "SOUNDEX", "net.jumperz.ext.org.hsqldb.Library.soundex" }, { "SPACE", "net.jumperz.ext.org.hsqldb.Library.space" }, { "SUBSTR", "net.jumperz.ext.org.hsqldb.Library.substring" }, { "SUBSTRING", "net.jumperz.ext.org.hsqldb.Library.substring" }, { "UCASE", "net.jumperz.ext.org.hsqldb.Library.ucase" }, { "LOWER", "net.jumperz.ext.org.hsqldb.Library.lcase" }, { "UPPER", "net.jumperz.ext.org.hsqldb.Library.ucase" } }; static final String sTimeDate[][] = { { "CURDATE", "net.jumperz.ext.org.hsqldb.Library.curdate" }, { "CURTIME", "net.jumperz.ext.org.hsqldb.Library.curtime" }, { "DAYNAME", "net.jumperz.ext.org.hsqldb.Library.dayname" }, { "DAYOFMONTH", "net.jumperz.ext.org.hsqldb.Library.dayofmonth" }, { "DAYOFWEEK", "net.jumperz.ext.org.hsqldb.Library.dayofweek" }, { "DAYOFYEAR", "net.jumperz.ext.org.hsqldb.Library.dayofyear" }, { "HOUR", "net.jumperz.ext.org.hsqldb.Library.hour" }, { "MINUTE", "net.jumperz.ext.org.hsqldb.Library.minute" }, { "MONTH", "net.jumperz.ext.org.hsqldb.Library.month" }, { "MONTHNAME", "net.jumperz.ext.org.hsqldb.Library.monthname" }, { "NOW", "net.jumperz.ext.org.hsqldb.Library.now" }, { "QUARTER", "net.jumperz.ext.org.hsqldb.Library.quarter" }, { "SECOND", "net.jumperz.ext.org.hsqldb.Library.second" }, { "WEEK", "net.jumperz.ext.org.hsqldb.Library.week" }, { "YEAR", "net.jumperz.ext.org.hsqldb.Library.year" } }; static final String sSystem[][] = { { "DATABASE", "net.jumperz.ext.org.hsqldb.Library.database" }, { "USER", "net.jumperz.ext.org.hsqldb.Library.user" }, { "IDENTITY", "net.jumperz.ext.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 * *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -