001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/datastore/schema/content/SQLFunctionCall.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstraße 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.io.datastore.schema.content;
044:
045: import java.util.HashSet;
046: import java.util.List;
047: import java.util.Set;
048:
049: import org.deegree.io.datastore.schema.MappedSimplePropertyType;
050:
051: /**
052: * Content class for {@link MappedSimplePropertyType}s that describes a call to a function provided
053: * by an SQL database.
054: *
055: * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
056: * @author last edited by: $Author: apoth $
057: *
058: * @version $Revision: 9342 $, $Date: 2007-12-27 04:32:57 -0800 (Thu, 27 Dec 2007) $
059: */
060: public class SQLFunctionCall extends FunctionCall {
061:
062: private String callString;
063:
064: private int typeCode;
065:
066: private int[] usedVarNumbers;
067:
068: /**
069: * Initializes a newly created <code>SQLFunctionCall</code> with the given call string and
070: * {@link FunctionParam}s.
071: *
072: * @param callString
073: * call string with placeholders ($1,$2,...,$n)
074: * @param typeCode
075: * SQL type code of the return value
076: * @param params
077: * parameters to be used for the placeholders
078: */
079: public SQLFunctionCall(String callString, int typeCode,
080: List<FunctionParam> params) {
081: super (params);
082: this .callString = callString;
083: this .typeCode = typeCode;
084: this .usedVarNumbers = extractUsedVars(callString);
085: }
086:
087: /**
088: * Initializes a newly created <code>SQLFunctionCall</code> with the given call string and
089: * {@link FunctionParam}s.
090: *
091: * @param callString
092: * call string with placeholders ($1,$2,...,$n)
093: * @param typeCode
094: * SQL type code of the return value
095: * @param params
096: * parameters to be used for the placeholders
097: */
098: public SQLFunctionCall(String callString, int typeCode,
099: FunctionParam... params) {
100: super (params);
101: this .callString = callString;
102: this .typeCode = typeCode;
103: this .usedVarNumbers = extractUsedVars(callString);
104: }
105:
106: /**
107: * Extracts all variable numbers ('$i') used in the given call string.
108: *
109: * @param callString
110: * @return all variable numbers used in the given call string
111: */
112: private int[] extractUsedVars(String callString) {
113:
114: Set<Integer> usedVars = new HashSet<Integer>();
115:
116: int foundAt = callString.indexOf('$');
117: while (foundAt != -1) {
118: foundAt++;
119: String varNumberString = "";
120: while (foundAt < callString.length()) {
121: char numberChar = callString.charAt(foundAt++);
122: if (numberChar >= '0' && numberChar <= '9') {
123: varNumberString += numberChar;
124: } else {
125: break;
126: }
127: }
128:
129: assert varNumberString.length() != 0;
130:
131: try {
132: int varNo = Integer.parseInt(varNumberString);
133: assert varNo != 0;
134: usedVars.add(varNo);
135: } catch (NumberFormatException e) {
136: assert false;
137: }
138:
139: // find next '$' symbol
140: foundAt = callString.indexOf('$', foundAt);
141: }
142:
143: int[] usedVarInts = new int[usedVars.size()];
144: int i = 0;
145: for (Integer pos : usedVars) {
146: usedVarInts[i++] = pos;
147: }
148:
149: return usedVarInts;
150: }
151:
152: /**
153: * Returns true, because the result of an SQL function call is (in general) suitable as a sort
154: * criterion.
155: *
156: * @return true, because the result of an SQL function call is (in general) suitable as a sort
157: * criterion
158: */
159: public boolean isSortable() {
160: return true;
161: }
162:
163: /**
164: * Returns the call string with placeholders ($1,$2,...,$n) for the {@link FunctionParam}s.
165: *
166: * @return the call string with placeholders ($1,$2,...,$n)
167: */
168: public String getCall() {
169: return this .callString;
170: }
171:
172: /**
173: * Returns the SQL type code of the function call's return value.
174: *
175: * @return the SQL type code of the function call's return value.
176: */
177: public int getTypeCode() {
178: return this .typeCode;
179: }
180:
181: /**
182: * Returns all variable numbers used in the call string.
183: *
184: * @return all variable numbers used in the call string
185: */
186: public int[] getUsedVars() {
187: return this .usedVarNumbers;
188: }
189:
190: @Override
191: public String toString() {
192: String s = this .callString;
193: int[] usedVars = this .getUsedVars();
194: List<FunctionParam> params = this .getParams();
195: for (int j = 0; j < usedVars.length; j++) {
196: int varNo = usedVars[j];
197: String varString = "\\$" + varNo;
198: FunctionParam param = params.get(varNo - 1);
199: if (param instanceof FieldContent) {
200: String replace = ((FieldContent) param).getField()
201: .getTable()
202: + "."
203: + ((FieldContent) param).getField().getField();
204: s = s.replaceAll(varString, replace);
205: } else if (param instanceof ConstantContent) {
206: String replace = ((ConstantContent) param).getValue();
207: s = s.replaceAll(varString, replace);
208: } else if (param instanceof SpecialContent) {
209: // appendSpecialContentValue( query, (SpecialContent) param );
210: // s = s.replaceFirst( varString, "?" );
211: } else {
212: assert false;
213: }
214: }
215: return s;
216: }
217: }
|