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 | * ExpressionFunction.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | import java.sql.*; |
36 | |
37 | |
38 | /** |
39 | * This is the base class for all functions. To add a new fuction you need<p> |
40 | * 1.) Add a new constant to the class SQLTokenizer.<p> |
41 | * 2.) Add a mapping of the function name keyword to the function constant in the class SQLTokenizer.<p> |
42 | * 3.) Extends a class from ExpressionFunction and implemets the function logic.<p> |
43 | * 4.) Add a case to the switch in SQLParser.function().<p> |
44 | */ |
45 | |
46 | abstract class ExpressionFunction extends Expression { |
47 | |
48 | Expression param1; |
49 | Expression param2; |
50 | Expression param3; |
51 | Expression param4; |
52 | |
53 | ExpressionFunction(){ |
54 | super(FUNCTION); |
55 | } |
56 | |
57 | // setzt die Funktionsnummer z.B. bei abs(5) --> SQLTokenizer.ABS |
58 | abstract int getFunction(); |
59 | |
60 | byte[] getBytes() throws Exception{ |
61 | return ExpressionValue.getBytes(getObject(), getDataType()); |
62 | } |
63 | |
64 | void setParams( Expression[] params ){ |
65 | super.setParams( params ); |
66 | if(params.length >0) param1 = params[0] ; |
67 | if(params.length >1) param2 = params[1] ; |
68 | if(params.length >2) param3 = params[2] ; |
69 | if(params.length >3) param4 = params[3] ; |
70 | } |
71 | |
72 | final void setParamAt( Expression param, int idx){ |
73 | switch(idx){ |
74 | case 0: |
75 | param1 = param; |
76 | break; |
77 | case 1: |
78 | param2 = param; |
79 | break; |
80 | case 2: |
81 | param3 = param; |
82 | break; |
83 | case 3: |
84 | param4 = param; |
85 | break; |
86 | } |
87 | super.setParamAt( param, idx ); |
88 | } |
89 | |
90 | |
91 | /** |
92 | * Is used in GroupResult. |
93 | */ |
94 | public boolean equals(Object expr){ |
95 | if(!super.equals(expr)) return false; |
96 | if(!(expr instanceof ExpressionFunction)) return false; |
97 | return ((ExpressionFunction)expr).getFunction() == getFunction(); |
98 | } |
99 | |
100 | |
101 | /** |
102 | * Create a SQLException that the current function does not support the specific data type. |
103 | * @param dataType A data type const from SQLTokenizer. |
104 | */ |
105 | SQLException createUnspportedDataType( int dataType ){ |
106 | return Utils.createSQLException("Unsupported data type '" + |
107 | SQLTokenizer.getKeyWord(dataType) + |
108 | "' for function " + |
109 | SQLTokenizer.getKeyWord(getFunction()) ); |
110 | } |
111 | |
112 | /** |
113 | * Create a SQLException that the current function can not convert the specific data type. |
114 | * @param dataType A data type const from SQLTokenizer. |
115 | */ |
116 | SQLException createUnspportedConversion( int dataType ){ |
117 | return Utils.createSQLException("Unsupported conversion to data type '" + |
118 | SQLTokenizer.getKeyWord(dataType) + |
119 | "' for function " + |
120 | SQLTokenizer.getKeyWord(getFunction()) ); |
121 | } |
122 | |
123 | |
124 | |
125 | } |