View Javadoc
1   /*
2    * Copyright 2004-2022 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    public String getSql(StatementScope statementScope, Object parameterObject) {
60      return processDynamicElements(sqlStatement, parameterObject);
61    }
62  
63    public ParameterMap getParameterMap(StatementScope statementScope, Object parameterObject) {
64      return statementScope.getParameterMap();
65    }
66  
67    public ResultMap getResultMap(StatementScope statementScope, Object parameterObject) {
68      return statementScope.getResultMap();
69    }
70  
71    public void cleanup(StatementScope statementScope) {
72    }
73  
74    /**
75     * Checks if is simple dynamic sql.
76     *
77     * @param sql
78     *          the sql
79     *
80     * @return true, if is simple dynamic sql
81     */
82    public static boolean isSimpleDynamicSql(String sql) {
83      return sql != null && sql.indexOf(ELEMENT_TOKEN) > -1;
84    }
85  
86    /**
87     * Process dynamic elements.
88     *
89     * @param sql
90     *          the sql
91     * @param parameterObject
92     *          the parameter object
93     *
94     * @return the string
95     */
96    private String processDynamicElements(String sql, Object parameterObject) {
97      StringTokenizer parser = new StringTokenizer(sql, ELEMENT_TOKEN, true);
98      StringBuilder newSql = new StringBuilder();
99  
100     String token = null;
101     String lastToken = null;
102     while (parser.hasMoreTokens()) {
103       token = parser.nextToken();
104 
105       if (ELEMENT_TOKEN.equals(lastToken)) {
106         if (ELEMENT_TOKEN.equals(token)) {
107           newSql.append(ELEMENT_TOKEN);
108           token = null;
109         } else {
110 
111           Object value = null;
112           if (parameterObject != null) {
113             if (delegate.getTypeHandlerFactory().hasTypeHandler(parameterObject.getClass())) {
114               value = parameterObject;
115             } else {
116               value = PROBE.getObject(parameterObject, token);
117             }
118           }
119           if (value != null) {
120             newSql.append(String.valueOf(value));
121           }
122 
123           token = parser.nextToken();
124           if (!ELEMENT_TOKEN.equals(token)) {
125             throw new SqlMapException("Unterminated dynamic element in sql (" + sql + ").");
126           }
127           token = null;
128         }
129       } else {
130         if (!ELEMENT_TOKEN.equals(token)) {
131           newSql.append(token);
132         }
133       }
134 
135       lastToken = token;
136     }
137 
138     return newSql.toString();
139   }
140 
141 }