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 | * ExpressionFunctionTimestampAdd.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 ExpressionFunctionTimestampAdd extends ExpressionFunction { |
41 | |
42 | final private int interval; |
43 | |
44 | |
45 | ExpressionFunctionTimestampAdd(int intervalType, Expression p1, Expression p2){ |
46 | interval = ExpressionFunctionTimestampDiff.mapIntervalType( intervalType ); |
47 | setParams( new Expression[]{p1,p2}); |
48 | } |
49 | |
50 | |
51 | int getFunction() { |
52 | return SQLTokenizer.TIMESTAMPADD; |
53 | } |
54 | |
55 | |
56 | boolean isNull() throws Exception { |
57 | return param1.isNull() || param2.isNull(); |
58 | } |
59 | |
60 | |
61 | boolean getBoolean() throws Exception { |
62 | return getLong() != 0; |
63 | } |
64 | |
65 | |
66 | int getInt() throws Exception { |
67 | return (int)getLong(); |
68 | } |
69 | |
70 | |
71 | long getLong() throws Exception { |
72 | if(isNull()) return 0; |
73 | switch(interval){ |
74 | case SQLTokenizer.SQL_TSI_FRAC_SECOND: |
75 | return param2.getLong() + param1.getLong(); |
76 | case SQLTokenizer.SQL_TSI_SECOND: |
77 | return param2.getLong() + param1.getLong() * 1000; |
78 | case SQLTokenizer.SQL_TSI_MINUTE: |
79 | return param2.getLong() + param1.getLong() * 60000; |
80 | case SQLTokenizer.SQL_TSI_HOUR: |
81 | return param2.getLong() + param1.getLong() * 3600000; |
82 | case SQLTokenizer.SQL_TSI_DAY: |
83 | return param2.getLong() + param1.getLong() * 86400000; |
84 | case SQLTokenizer.SQL_TSI_WEEK:{ |
85 | return param2.getLong() + param1.getLong() * 604800000; |
86 | }case SQLTokenizer.SQL_TSI_MONTH:{ |
87 | DateTime.Details details2 = new DateTime.Details(param2.getLong()); |
88 | details2.month += param1.getLong(); |
89 | return DateTime.calcMillis(details2); |
90 | } |
91 | case SQLTokenizer.SQL_TSI_QUARTER:{ |
92 | DateTime.Details details2 = new DateTime.Details(param2.getLong()); |
93 | details2.month += param1.getLong() * 3; |
94 | return DateTime.calcMillis(details2); |
95 | } |
96 | case SQLTokenizer.SQL_TSI_YEAR:{ |
97 | DateTime.Details details2 = new DateTime.Details(param2.getLong()); |
98 | details2.year += param1.getLong(); |
99 | return DateTime.calcMillis(details2); |
100 | } |
101 | default: throw new Error(); |
102 | } |
103 | // TODO Auto-generated method stub |
104 | } |
105 | |
106 | |
107 | float getFloat() throws Exception { |
108 | return getLong(); |
109 | } |
110 | |
111 | |
112 | double getDouble() throws Exception { |
113 | return getLong(); |
114 | } |
115 | |
116 | |
117 | long getMoney() throws Exception { |
118 | return getLong() * 10000; |
119 | } |
120 | |
121 | |
122 | MutableNumeric getNumeric() throws Exception { |
123 | if(isNull()) return null; |
124 | return new MutableNumeric(getLong()); |
125 | } |
126 | |
127 | |
128 | Object getObject() throws Exception { |
129 | if(isNull()) return null; |
130 | return new DateTime( getLong(), SQLTokenizer.TIMESTAMP ); |
131 | } |
132 | |
133 | |
134 | String getString() throws Exception { |
135 | if(isNull()) return null; |
136 | return new DateTime( getLong(), SQLTokenizer.TIMESTAMP ).toString(); |
137 | } |
138 | |
139 | |
140 | int getDataType() { |
141 | return SQLTokenizer.TIMESTAMP; |
142 | } |
143 | |
144 | } |