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 | * Expression.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | |
36 | abstract class Expression implements Cloneable{ |
37 | |
38 | static final Expression NULL = new ExpressionValue( null, SQLTokenizer.NULL ); |
39 | |
40 | final private int type; |
41 | private String name; // the name of the original column in the table |
42 | private String alias; |
43 | |
44 | /** |
45 | * A list of parameters. It is used for ExpressionFunction and ExpressionAritmethik. |
46 | * Do not modify this varaiable from extern directly because there are other references. |
47 | * Use the methods setParams() and setParamAt() |
48 | * @see #setParams |
49 | * @see setParamAt |
50 | */ |
51 | private Expression[] params; |
52 | |
53 | Expression(int type){ |
54 | this.type = type; |
55 | } |
56 | |
57 | protected Object clone() throws CloneNotSupportedException{ |
58 | return super.clone(); |
59 | } |
60 | |
61 | final String getName(){ |
62 | return name; |
63 | } |
64 | |
65 | final void setName(String name){ |
66 | this.alias = this.name = name; |
67 | } |
68 | |
69 | final String getAlias(){ |
70 | return alias; |
71 | } |
72 | |
73 | final void setAlias(String alias){ |
74 | this.alias = alias; |
75 | } |
76 | |
77 | void setParams( Expression[] params ){ |
78 | this.params = params; |
79 | } |
80 | |
81 | /** |
82 | * Replace the idx parameter. You need to use this method to modify the params |
83 | * array because there there can be other references to the params. |
84 | */ |
85 | void setParamAt( Expression param, int idx){ |
86 | params[idx] = param; |
87 | } |
88 | |
89 | final Expression[] getParams(){ return params; } |
90 | |
91 | /** |
92 | * Is used in GroupResult. |
93 | */ |
94 | public boolean equals(Object expr){ |
95 | if(!(expr instanceof Expression)) return false; |
96 | if( ((Expression)expr).type == type){ |
97 | |
98 | Expression[] p1 = ((Expression)expr).params; |
99 | Expression[] p2 = params; |
100 | if(p1 != null && p2 != null){ |
101 | if(p1 == null) return false; |
102 | for(int i=0; i<p1.length; i++){ |
103 | if(!p2[i].equals(p1[i])) return false; |
104 | } |
105 | } |
106 | String name1 = ((Expression)expr).name; |
107 | String name2 = name; |
108 | if(name1 == name2) return true; |
109 | if(name1 == null) return false; |
110 | if(name1.equalsIgnoreCase(name2)) return true; |
111 | } |
112 | return false; |
113 | } |
114 | |
115 | |
116 | abstract boolean isNull() throws Exception; |
117 | |
118 | abstract boolean getBoolean() throws Exception; |
119 | |
120 | abstract int getInt() throws Exception; |
121 | |
122 | abstract long getLong() throws Exception; |
123 | |
124 | abstract float getFloat() throws Exception; |
125 | |
126 | abstract double getDouble() throws Exception; |
127 | |
128 | abstract long getMoney() throws Exception; |
129 | |
130 | abstract MutableNumeric getNumeric() throws Exception; |
131 | |
132 | abstract Object getObject() throws Exception; |
133 | |
134 | final Object getApiObject() throws Exception{ |
135 | Object obj = getObject(); |
136 | if(obj instanceof Mutable){ |
137 | return ((Mutable)obj).getImmutableObject(); |
138 | } |
139 | return obj; |
140 | } |
141 | |
142 | abstract String getString() throws Exception; |
143 | |
144 | abstract byte[] getBytes() throws Exception; |
145 | |
146 | abstract int getDataType(); |
147 | |
148 | final int getType(){return type;} |
149 | |
150 | /*======================================================================= |
151 | |
152 | Methods for ResultSetMetaData |
153 | |
154 | =======================================================================*/ |
155 | |
156 | String getTableName(){ |
157 | return null; |
158 | } |
159 | |
160 | |
161 | int getPrecision(){ |
162 | return SSResultSetMetaData.getDataTypePrecision( getDataType(), -1 ); |
163 | } |
164 | |
165 | |
166 | |
167 | int getScale(){ |
168 | return getScale(getDataType()); |
169 | } |
170 | |
171 | |
172 | final static int getScale(int dataType){ |
173 | switch(dataType){ |
174 | case SQLTokenizer.MONEY: |
175 | case SQLTokenizer.SMALLMONEY: |
176 | return 4; |
177 | case SQLTokenizer.TIMESTAMP: |
178 | return 9; //nanos |
179 | case SQLTokenizer.NUMERIC: |
180 | case SQLTokenizer.DECIMAL: |
181 | return 38; |
182 | default: return 0; |
183 | } |
184 | } |
185 | |
186 | |
187 | int getDisplaySize(){ |
188 | return SSResultSetMetaData.getDisplaySize(getDataType(), getPrecision(), getScale()); |
189 | } |
190 | |
191 | boolean isDefinitelyWritable(){ |
192 | return false; |
193 | } |
194 | |
195 | boolean isAutoIncrement(){ |
196 | return false; |
197 | } |
198 | |
199 | boolean isCaseSensitive(){ |
200 | return false; //TODO sollte wenn es von ExpressionName kommt durchgereicht werden |
201 | } |
202 | |
203 | boolean isNullable(){ |
204 | return true; //TODO könnte differenzierter sein |
205 | } |
206 | |
207 | |
208 | static final int VALUE = 1; |
209 | static final int NAME = 2; |
210 | static final int FUNCTION = 3; |
211 | static final int GROUP_BY = 11; |
212 | static final int COUNT = 12; |
213 | static final int SUM = 13; |
214 | static final int FIRST = 14; |
215 | static final int LAST = 15; |
216 | static final int MIN = 16; |
217 | static final int MAX = 17; |
218 | static final int GROUP_BEGIN= GROUP_BY; |
219 | |
220 | } |