| 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 | * ExpressionFunctionCase.java | 
| 29 | * --------------- | 
| 30 | * Author: Volker Berlin | 
| 31 | * | 
| 32 | * Created on 29.06.2004 | 
| 33 | */ | 
| 34 | package smallsql.database; | 
| 35 |  | 
| 36 |  | 
| 37 | /** | 
| 38 | * @author Volker Berlin | 
| 39 | */ | 
| 40 | final class ExpressionFunctionCase extends Expression/*Function*/ { | 
| 41 |  | 
| 42 | /** | 
| 43 | * @param type | 
| 44 | */ | 
| 45 | ExpressionFunctionCase() { | 
| 46 | super(FUNCTION); | 
| 47 | } | 
| 48 |  | 
| 49 |  | 
| 50 | private final Expressions cases   = new Expressions(); | 
| 51 | private final Expressions results = new Expressions(); | 
| 52 | private Expression elseResult = ExpressionValue.NULL; | 
| 53 | private int dataType = -1; | 
| 54 |  | 
| 55 |  | 
| 56 | final void addCase(Expression condition, Expression result){ | 
| 57 | cases.add(condition); | 
| 58 | results.add(result); | 
| 59 | } | 
| 60 |  | 
| 61 |  | 
| 62 | final void setElseResult(Expression expr){ | 
| 63 | elseResult = expr; | 
| 64 | } | 
| 65 |  | 
| 66 |  | 
| 67 | /** | 
| 68 | * The structur is finish | 
| 69 | */ | 
| 70 | final void setEnd(){ | 
| 71 | Expression[] params = new Expression[cases.size()*2 + (elseResult!=null ? 1 : 0)]; | 
| 72 | int i=0; | 
| 73 | for(int p=0; p<cases.size(); p++){ | 
| 74 | params[i++] = cases  .get( p ); | 
| 75 | params[i++] = results.get( p ) ; | 
| 76 | } | 
| 77 | if(i<params.length) | 
| 78 | params[i] = elseResult; | 
| 79 | super.setParams(params); | 
| 80 | } | 
| 81 |  | 
| 82 | final void setParams( Expression[] params ){ | 
| 83 | super.setParams(params); | 
| 84 | int i = 0; | 
| 85 | for(int p=0; p<cases.size(); p++){ | 
| 86 | cases  .set( p, params[i++]); | 
| 87 | results.set( p, params[i++]); | 
| 88 | } | 
| 89 | if(i<params.length) | 
| 90 | elseResult = params[i]; | 
| 91 | } | 
| 92 |  | 
| 93 |  | 
| 94 | void setParamAt( Expression param, int idx){ | 
| 95 | super.setParamAt( param, idx ); | 
| 96 | int p = idx / 2; | 
| 97 | if(p>=cases.size()){ | 
| 98 | elseResult = param; | 
| 99 | return; | 
| 100 | } | 
| 101 | if(idx % 2 > 0){ | 
| 102 | results.set( p, param ); | 
| 103 | }else{ | 
| 104 | cases.set( p, param ); | 
| 105 | } | 
| 106 | } | 
| 107 |  | 
| 108 |  | 
| 109 | //================================ | 
| 110 | // Methods of the interface | 
| 111 | //================================ | 
| 112 |  | 
| 113 |  | 
| 114 | final int getFunction() { | 
| 115 | return SQLTokenizer.CASE; | 
| 116 | } | 
| 117 |  | 
| 118 |  | 
| 119 | final boolean isNull() throws Exception { | 
| 120 | return getResult().isNull(); | 
| 121 | } | 
| 122 |  | 
| 123 |  | 
| 124 | final boolean getBoolean() throws Exception { | 
| 125 | return getResult().getBoolean(); | 
| 126 | } | 
| 127 |  | 
| 128 |  | 
| 129 | final int getInt() throws Exception { | 
| 130 | return getResult().getInt(); | 
| 131 | } | 
| 132 |  | 
| 133 |  | 
| 134 | final long getLong() throws Exception { | 
| 135 | return getResult().getLong(); | 
| 136 | } | 
| 137 |  | 
| 138 |  | 
| 139 | final float getFloat() throws Exception { | 
| 140 | return getResult().getFloat(); | 
| 141 | } | 
| 142 |  | 
| 143 |  | 
| 144 | final double getDouble() throws Exception { | 
| 145 | return getResult().getDouble(); | 
| 146 | } | 
| 147 |  | 
| 148 |  | 
| 149 | final long getMoney() throws Exception { | 
| 150 | return getResult().getMoney(); | 
| 151 | } | 
| 152 |  | 
| 153 |  | 
| 154 | final MutableNumeric getNumeric() throws Exception { | 
| 155 | return getResult().getNumeric(); | 
| 156 | } | 
| 157 |  | 
| 158 |  | 
| 159 | final Object getObject() throws Exception { | 
| 160 | return getResult().getObject(); | 
| 161 | } | 
| 162 |  | 
| 163 |  | 
| 164 | final String getString() throws Exception { | 
| 165 | return getResult().getString(); | 
| 166 | } | 
| 167 |  | 
| 168 |  | 
| 169 | final byte[] getBytes() throws Exception{ | 
| 170 | return getResult().getBytes(); | 
| 171 | } | 
| 172 |  | 
| 173 |  | 
| 174 | final int getDataType() { | 
| 175 | if(dataType < 0){ | 
| 176 | dataType = elseResult.getDataType(); | 
| 177 | for(int i=0; i<results.size(); i++){ | 
| 178 | dataType = ExpressionArithmetic.getDataType(dataType, results.get(i).getDataType()); | 
| 179 | } | 
| 180 | } | 
| 181 | return dataType; | 
| 182 | } | 
| 183 |  | 
| 184 |  | 
| 185 | final int getPrecision(){ | 
| 186 | int precision = 0; | 
| 187 | for(int i=results.size()-1; i>=0; i--){ | 
| 188 | precision = Math.max(precision, results.get(i).getPrecision()); | 
| 189 | } | 
| 190 | return precision; | 
| 191 | } | 
| 192 |  | 
| 193 |  | 
| 194 | final int getScale(){ | 
| 195 | int precision = 0; | 
| 196 | for(int i=results.size()-1; i>=0; i--){ | 
| 197 | precision = Math.max(precision, results.get(i).getScale()); | 
| 198 | } | 
| 199 | return precision; | 
| 200 | } | 
| 201 |  | 
| 202 |  | 
| 203 | //================================ | 
| 204 | // private helper functions | 
| 205 | //================================ | 
| 206 |  | 
| 207 |  | 
| 208 | final private Expression getResult() throws Exception{ | 
| 209 | for(int i=0; i<cases.size(); i++){ | 
| 210 | if(cases.get(i).getBoolean()) return results.get(i); | 
| 211 | } | 
| 212 | return elseResult; | 
| 213 | } | 
| 214 |  | 
| 215 | } |