| 1 | /* ============================================================= |
| 2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
| 3 | * ============================================================= |
| 4 | * |
| 5 | * (C) Copyright 2004-2006, by Volker Berlin. |
| 6 | * |
| 7 | * Project Info: http://www.smallsql.de/ |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU Lesser General Public License as published by |
| 11 | * the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 | * License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 22 | * USA. |
| 23 | * |
| 24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 25 | * in the United States and other countries.] |
| 26 | * |
| 27 | * --------------- |
| 28 | * ExpressionFunctionTimestampDiff.java |
| 29 | * --------------- |
| 30 | * Author: Volker Berlin |
| 31 | * |
| 32 | * Created on 19.06.2004 |
| 33 | */ |
| 34 | package smallsql.database; |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * @author Volker Berlin |
| 39 | */ |
| 40 | public class ExpressionFunctionTimestampDiff extends ExpressionFunction { |
| 41 | |
| 42 | final private int interval; |
| 43 | |
| 44 | static final int mapIntervalType(int intervalType){ |
| 45 | switch(intervalType){ |
| 46 | case SQLTokenizer.MILLISECOND: |
| 47 | return SQLTokenizer.SQL_TSI_FRAC_SECOND; |
| 48 | case SQLTokenizer.SECOND: |
| 49 | return SQLTokenizer.SQL_TSI_SECOND; |
| 50 | case SQLTokenizer.MINUTE: |
| 51 | return SQLTokenizer.SQL_TSI_MINUTE; |
| 52 | case SQLTokenizer.HOUR: |
| 53 | return SQLTokenizer.SQL_TSI_HOUR; |
| 54 | case SQLTokenizer.D: |
| 55 | case SQLTokenizer.DAY: |
| 56 | return SQLTokenizer.SQL_TSI_DAY; |
| 57 | case SQLTokenizer.WEEK: |
| 58 | return SQLTokenizer.SQL_TSI_WEEK; |
| 59 | case SQLTokenizer.MONTH: |
| 60 | return SQLTokenizer.SQL_TSI_MONTH; |
| 61 | case SQLTokenizer.QUARTER: |
| 62 | return SQLTokenizer.SQL_TSI_QUARTER; |
| 63 | case SQLTokenizer.YEAR: |
| 64 | return SQLTokenizer.SQL_TSI_YEAR; |
| 65 | default: |
| 66 | return intervalType; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | ExpressionFunctionTimestampDiff(int intervalType, Expression p1, Expression p2){ |
| 71 | interval = mapIntervalType( intervalType ); |
| 72 | setParams( new Expression[]{p1,p2}); |
| 73 | } |
| 74 | |
| 75 | int getFunction() { |
| 76 | return SQLTokenizer.TIMESTAMPDIFF; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | boolean isNull() throws Exception { |
| 81 | return param1.isNull() || param2.isNull(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | boolean getBoolean() throws Exception { |
| 86 | return getInt() != 0; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | int getInt() throws Exception { |
| 91 | if(isNull()) return 0; |
| 92 | switch(interval){ |
| 93 | case SQLTokenizer.SQL_TSI_FRAC_SECOND: |
| 94 | return (int)(param2.getLong() - param1.getLong()); |
| 95 | case SQLTokenizer.SQL_TSI_SECOND: |
| 96 | return (int)(param2.getLong() /1000 - param1.getLong() /1000); |
| 97 | case SQLTokenizer.SQL_TSI_MINUTE: |
| 98 | return (int)(param2.getLong() /60000 - param1.getLong() /60000); |
| 99 | case SQLTokenizer.SQL_TSI_HOUR: |
| 100 | return (int)(param2.getLong() /3600000 - param1.getLong() /3600000); |
| 101 | case SQLTokenizer.SQL_TSI_DAY: |
| 102 | return (int)(param2.getLong() /86400000 - param1.getLong() /86400000); |
| 103 | case SQLTokenizer.SQL_TSI_WEEK:{ |
| 104 | long day2 = param2.getLong() /86400000; |
| 105 | long day1 = param1.getLong() /86400000; |
| 106 | // the 1. Jan 1970 is a Thursday --> 3 |
| 107 | return (int)((day2 + 3) / 7 - (day1 + 3) / 7); |
| 108 | }case SQLTokenizer.SQL_TSI_MONTH:{ |
| 109 | DateTime.Details details2 = new DateTime.Details(param2.getLong()); |
| 110 | DateTime.Details details1 = new DateTime.Details(param1.getLong()); |
| 111 | return (details2.year * 12 + details2.month) - (details1.year * 12 + details1.month); |
| 112 | } |
| 113 | case SQLTokenizer.SQL_TSI_QUARTER:{ |
| 114 | DateTime.Details details2 = new DateTime.Details(param2.getLong()); |
| 115 | DateTime.Details details1 = new DateTime.Details(param1.getLong()); |
| 116 | return (details2.year * 4 + details2.month / 3) - (details1.year * 4 + details1.month / 3); |
| 117 | } |
| 118 | case SQLTokenizer.SQL_TSI_YEAR:{ |
| 119 | DateTime.Details details2 = new DateTime.Details(param2.getLong()); |
| 120 | DateTime.Details details1 = new DateTime.Details(param1.getLong()); |
| 121 | return details2.year - details1.year; |
| 122 | } |
| 123 | default: throw new Error(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | long getLong() throws Exception { |
| 129 | return getInt(); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | float getFloat() throws Exception { |
| 134 | return getInt(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | double getDouble() throws Exception { |
| 139 | return getInt(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | long getMoney() throws Exception { |
| 144 | return getInt() * 10000L; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | MutableNumeric getNumeric() throws Exception { |
| 149 | if(isNull()) return null; |
| 150 | return new MutableNumeric(getInt()); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | Object getObject() throws Exception { |
| 155 | if(isNull()) return null; |
| 156 | return Utils.getInteger(getInt()); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | String getString() throws Exception { |
| 161 | if(isNull()) return null; |
| 162 | return String.valueOf(getInt()); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | int getDataType() { |
| 167 | return SQLTokenizer.INT; |
| 168 | } |
| 169 | |
| 170 | } |