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 | * ExpressionFunctionAbs.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | |
36 | class ExpressionFunctionAbs extends ExpressionFunctionReturnP1 { |
37 | |
38 | int getFunction(){ return SQLTokenizer.ABS; } |
39 | |
40 | |
41 | boolean getBoolean() throws Exception{ |
42 | return getDouble() != 0; |
43 | } |
44 | |
45 | int getInt() throws Exception{ |
46 | return Math.abs( param1.getInt() ); |
47 | } |
48 | |
49 | long getLong() throws Exception{ |
50 | return Math.abs( param1.getLong() ); |
51 | } |
52 | |
53 | float getFloat() throws Exception{ |
54 | return Math.abs( param1.getFloat() ); |
55 | } |
56 | |
57 | double getDouble() throws Exception{ |
58 | return Math.abs( param1.getDouble() ); |
59 | } |
60 | |
61 | long getMoney() throws Exception{ |
62 | return Math.abs( param1.getMoney() ); |
63 | } |
64 | |
65 | MutableNumeric getNumeric() throws Exception{ |
66 | if(param1.isNull()) return null; |
67 | MutableNumeric num = param1.getNumeric(); |
68 | if(num.signum < 0) num.signum = 1; |
69 | return num; |
70 | } |
71 | |
72 | Object getObject() throws Exception{ |
73 | if(param1.isNull()) return null; |
74 | Object para1 = param1.getObject(); |
75 | switch(param1.getDataType()){ |
76 | case SQLTokenizer.FLOAT: |
77 | case SQLTokenizer.DOUBLE: |
78 | double dValue = ((Double)para1).doubleValue(); |
79 | return (dValue<0) ? new Double(-dValue) : para1; |
80 | case SQLTokenizer.REAL: |
81 | double fValue = ((Float)para1).floatValue(); |
82 | return (fValue<0) ? new Float(-fValue) : para1; |
83 | case SQLTokenizer.BIGINT: |
84 | long lValue = ((Number)para1).longValue(); |
85 | return (lValue<0) ? new Long(-lValue) : para1; |
86 | case SQLTokenizer.TINYINT: |
87 | case SQLTokenizer.SMALLINT: |
88 | case SQLTokenizer.INT: |
89 | int iValue = ((Number)para1).intValue(); |
90 | return (iValue<0) ? new Integer(-iValue) : para1; |
91 | case SQLTokenizer.NUMERIC: |
92 | case SQLTokenizer.DECIMAL: |
93 | MutableNumeric nValue = (MutableNumeric)para1; |
94 | if(nValue.signum <0) nValue.signum = 1; |
95 | return nValue; |
96 | case SQLTokenizer.MONEY: |
97 | Money mValue = (Money)para1; |
98 | if(mValue.value <0) mValue.value = -mValue.value; |
99 | return mValue; |
100 | default: throw createUnspportedDataType(param1.getDataType()); |
101 | } |
102 | } |
103 | |
104 | String getString() throws Exception{ |
105 | Object obj = getObject(); |
106 | if(obj == null) return null; |
107 | return obj.toString(); |
108 | } |
109 | |
110 | |
111 | } |