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