View Javadoc
1   /*
2    * Copyright 2004-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.sqlmap.engine.mapping.sql.simple;
17  
18  import com.ibatis.common.beans.Probe;
19  import com.ibatis.common.beans.ProbeFactory;
20  import com.ibatis.sqlmap.client.SqlMapException;
21  import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
22  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
23  import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
24  import com.ibatis.sqlmap.engine.mapping.sql.Sql;
25  import com.ibatis.sqlmap.engine.scope.StatementScope;
26  
27  import java.util.StringTokenizer;
28  
29  /**
30   * The Class SimpleDynamicSql.
31   */
32  public class SimpleDynamicSql implements Sql {
33  
34    /** The Constant PROBE. */
35    private static final Probe PROBE = ProbeFactory.getProbe();
36  
37    /** The Constant ELEMENT_TOKEN. */
38    private static final String ELEMENT_TOKEN = "$";
39  
40    /** The sql statement. */
41    private String sqlStatement;
42  
43    /** The delegate. */
44    private SqlMapExecutorDelegate delegate;
45  
46    /**
47     * Instantiates a new simple dynamic sql.
48     *
49     * @param delegate
50     *          the delegate
51     * @param sqlStatement
52     *          the sql statement
53     */
54    public SimpleDynamicSql(SqlMapExecutorDelegate delegate, String sqlStatement) {
55      this.delegate = delegate;
56      this.sqlStatement = sqlStatement;
57    }
58  
59    @Override
60    public String getSql(StatementScope statementScope, Object parameterObject) {
61      return processDynamicElements(sqlStatement, parameterObject);
62    }
63  
64    @Override
65    public ParameterMap getParameterMap(StatementScope statementScope, Object parameterObject) {
66      return statementScope.getParameterMap();
67    }
68  
69    @Override
70    public ResultMap getResultMap(StatementScope statementScope, Object parameterObject) {
71      return statementScope.getResultMap();
72    }
73  
74    @Override
75    public void cleanup(StatementScope statementScope) {
76    }
77  
78    /**
79     * Checks if is simple dynamic sql.
80     *
81     * @param sql
82     *          the sql
83     *
84     * @return true, if is simple dynamic sql
85     */
86    public static boolean isSimpleDynamicSql(String sql) {
87      return sql != null && sql.indexOf(ELEMENT_TOKEN) > -1;
88    }
89  
90    /**
91     * Process dynamic elements.
92     *
93     * @param sql
94     *          the sql
95     * @param parameterObject
96     *          the parameter object
97     *
98     * @return the string
99     */
100   private String processDynamicElements(String sql, Object parameterObject) {
101     StringTokenizer parser = new StringTokenizer(sql, ELEMENT_TOKEN, true);
102     StringBuilder newSql = new StringBuilder();
103 
104     String token = null;
105     String lastToken = null;
106     while (parser.hasMoreTokens()) {
107       token = parser.nextToken();
108 
109       if (ELEMENT_TOKEN.equals(lastToken)) {
110         if (ELEMENT_TOKEN.equals(token)) {
111           newSql.append(ELEMENT_TOKEN);
112         } else {
113 
114           Object value = null;
115           if (parameterObject != null) {
116             if (delegate.getTypeHandlerFactory().hasTypeHandler(parameterObject.getClass())) {
117               value = parameterObject;
118             } else {
119               value = PROBE.getObject(parameterObject, token);
120             }
121           }
122           if (value != null) {
123             newSql.append(String.valueOf(value));
124           }
125 
126           token = parser.nextToken();
127           if (!ELEMENT_TOKEN.equals(token)) {
128             throw new SqlMapException("Unterminated dynamic element in sql (" + sql + ").");
129           }
130         }
131         token = null;
132       } else if (!ELEMENT_TOKEN.equals(token)) {
133         newSql.append(token);
134       }
135 
136       lastToken = token;
137     }
138 
139     return newSql.toString();
140   }
141 
142 }