001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.mapping.sql.simple;
017:
018: import com.ibatis.common.beans.Probe;
019: import com.ibatis.common.beans.ProbeFactory;
020: import com.ibatis.sqlmap.client.SqlMapException;
021: import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
022: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
023: import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
024: import com.ibatis.sqlmap.engine.mapping.sql.Sql;
025: import com.ibatis.sqlmap.engine.scope.RequestScope;
026:
027: import java.util.StringTokenizer;
028:
029: public class SimpleDynamicSql implements Sql {
030:
031: private static final Probe PROBE = ProbeFactory.getProbe();
032:
033: private static final String ELEMENT_TOKEN = "$";
034:
035: private String sqlStatement;
036:
037: private SqlMapExecutorDelegate delegate;
038:
039: public SimpleDynamicSql(SqlMapExecutorDelegate delegate,
040: String sqlStatement) {
041: this .delegate = delegate;
042: this .sqlStatement = sqlStatement;
043: }
044:
045: public String getSql(RequestScope request, Object parameterObject) {
046: return processDynamicElements(sqlStatement, parameterObject);
047: }
048:
049: public ParameterMap getParameterMap(RequestScope request,
050: Object parameterObject) {
051: return request.getParameterMap();
052: }
053:
054: public ResultMap getResultMap(RequestScope request,
055: Object parameterObject) {
056: return request.getResultMap();
057: }
058:
059: public void cleanup(RequestScope request) {
060: }
061:
062: public static boolean isSimpleDynamicSql(String sql) {
063: return sql != null && sql.indexOf(ELEMENT_TOKEN) > -1;
064: }
065:
066: private String processDynamicElements(String sql,
067: Object parameterObject) {
068: StringTokenizer parser = new StringTokenizer(sql,
069: ELEMENT_TOKEN, true);
070: StringBuffer newSql = new StringBuffer();
071:
072: String token = null;
073: String lastToken = null;
074: while (parser.hasMoreTokens()) {
075: token = parser.nextToken();
076:
077: if (ELEMENT_TOKEN.equals(lastToken)) {
078: if (ELEMENT_TOKEN.equals(token)) {
079: newSql.append(ELEMENT_TOKEN);
080: token = null;
081: } else {
082:
083: Object value = null;
084: if (parameterObject != null) {
085: if (delegate.getTypeHandlerFactory()
086: .hasTypeHandler(
087: parameterObject.getClass())) {
088: value = parameterObject;
089: } else {
090: value = PROBE.getObject(parameterObject,
091: token);
092: }
093: }
094: if (value != null) {
095: newSql.append(String.valueOf(value));
096: }
097:
098: token = parser.nextToken();
099: if (!ELEMENT_TOKEN.equals(token)) {
100: throw new SqlMapException(
101: "Unterminated dynamic element in sql ("
102: + sql + ").");
103: }
104: token = null;
105: }
106: } else {
107: if (!ELEMENT_TOKEN.equals(token)) {
108: newSql.append(token);
109: }
110: }
111:
112: lastToken = token;
113: }
114:
115: return newSql.toString();
116: }
117:
118: }
|