KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > sql > dynamic > DynamicSql


1 /*
2  * Copyright 2004 Clinton Begin
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  * http://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.dynamic;
17
18 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
19 import com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap;
20 import com.ibatis.sqlmap.engine.mapping.parameter.InlineParameterMapParser;
21 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
22 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
23 import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
24 import com.ibatis.sqlmap.engine.mapping.sql.Sql;
25 import com.ibatis.sqlmap.engine.mapping.sql.SqlChild;
26 import com.ibatis.sqlmap.engine.mapping.sql.SqlText;
27 import com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements.*;
28 import com.ibatis.sqlmap.engine.mapping.sql.simple.SimpleDynamicSql;
29 import com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement;
30 import com.ibatis.sqlmap.engine.scope.RequestScope;
31
32 import java.io.PrintWriter JavaDoc;
33 import java.io.StringWriter JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 public class DynamicSql implements Sql, DynamicParent {
39
40   private static final InlineParameterMapParser PARAM_PARSER = new InlineParameterMapParser();
41
42   private List JavaDoc children = new ArrayList JavaDoc();
43   private SqlMapExecutorDelegate delegate;
44
45   public DynamicSql(SqlMapExecutorDelegate delegate) {
46     this.delegate = delegate;
47   }
48
49   public String JavaDoc getSql(RequestScope request, Object JavaDoc parameterObject) {
50     String JavaDoc sql = request.getDynamicSql();
51     if (sql == null) {
52       process(request, parameterObject);
53       sql = request.getDynamicSql();
54     }
55     return sql;
56   }
57
58   public ParameterMap getParameterMap(RequestScope request, Object JavaDoc parameterObject) {
59     ParameterMap map = request.getDynamicParameterMap();
60     if (map == null) {
61       process(request, parameterObject);
62       map = request.getDynamicParameterMap();
63     }
64     return map;
65   }
66
67   public ResultMap getResultMap(RequestScope request, Object JavaDoc parameterObject) {
68     return request.getResultMap();
69   }
70
71   public void cleanup(RequestScope request) {
72     request.setDynamicSql(null);
73     request.setDynamicParameterMap(null);
74   }
75
76   private void process(RequestScope request, Object JavaDoc parameterObject) {
77     SqlTagContext ctx = new SqlTagContext();
78     List JavaDoc localChildren = children;
79     processBodyChildren(request, ctx, parameterObject, localChildren.iterator());
80
81     BasicParameterMap map = new BasicParameterMap(delegate);
82     map.setId(request.getStatement().getId() + "-InlineParameterMap");
83     map.setParameterClass(((GeneralStatement) request.getStatement()).getParameterClass());
84     map.setParameterMappingList(ctx.getParameterMappings());
85
86     String JavaDoc dynSql = ctx.getBodyText();
87
88     // Processes $substitutions$ after DynamicSql
89
if (SimpleDynamicSql.isSimpleDynamicSql(dynSql)) {
90       dynSql = new SimpleDynamicSql(delegate, dynSql).getSql(request, parameterObject);
91     }
92
93     request.setDynamicSql(dynSql);
94     request.setDynamicParameterMap(map);
95   }
96
97   private void processBodyChildren(RequestScope request, SqlTagContext ctx, Object JavaDoc parameterObject, Iterator JavaDoc localChildren) {
98     PrintWriter JavaDoc out = ctx.getWriter();
99     processBodyChildren(request, ctx, parameterObject, localChildren, out);
100   }
101
102   private void processBodyChildren(RequestScope request, SqlTagContext ctx, Object JavaDoc parameterObject, Iterator JavaDoc localChildren, PrintWriter JavaDoc out) {
103     while (localChildren.hasNext()) {
104       SqlChild child = (SqlChild) localChildren.next();
105       if (child instanceof SqlText) {
106         SqlText sqlText = (SqlText) child;
107         String JavaDoc sqlStatement = sqlText.getText();
108         if (sqlText.isWhiteSpace()) {
109           out.print(sqlStatement);
110         } else if (!sqlText.isPostParseRequired()) {
111
112           // BODY OUT
113
out.print(sqlStatement);
114
115           ParameterMapping[] mappings = sqlText.getParameterMappings();
116           if (mappings != null) {
117             for (int i = 0, n = mappings.length; i < n; i++) {
118               ctx.addParameterMapping(mappings[i]);
119             }
120           }
121         } else {
122
123           IterateContext itCtx = ctx.peekIterateContext();
124
125           if(null != itCtx && itCtx.isAllowNext()){
126             itCtx.next();
127             itCtx.setAllowNext(false);
128             if(!itCtx.hasNext()) {
129               itCtx.setFinal(true);
130             }
131           }
132
133           if(itCtx!=null) {
134             StringBuffer JavaDoc sqlStatementBuffer = new StringBuffer JavaDoc(sqlStatement);
135             iteratePropertyReplace(sqlStatementBuffer, itCtx);
136             sqlStatement = sqlStatementBuffer.toString();
137           }
138
139           sqlText = PARAM_PARSER.parseInlineParameterMap(delegate.getTypeHandlerFactory(), sqlStatement);
140
141           ParameterMapping[] mappings = sqlText.getParameterMappings();
142           out.print(sqlText.getText());
143           if (mappings != null) {
144              for (int i = 0, n = mappings.length; i < n; i++) {
145                ctx.addParameterMapping(mappings[i]);
146              }
147           }
148         }
149       } else if (child instanceof SqlTag) {
150         SqlTag tag = (SqlTag) child;
151         SqlTagHandler handler = tag.getHandler();
152         int response = SqlTagHandler.INCLUDE_BODY;
153         do {
154           StringWriter JavaDoc sw = new StringWriter JavaDoc();
155           PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
156           
157           response = handler.doStartFragment(ctx, tag, parameterObject);
158           if (response != SqlTagHandler.SKIP_BODY) {
159
160             processBodyChildren(request, ctx, parameterObject, tag.getChildren(), pw);
161             pw.flush();
162             pw.close();
163             StringBuffer JavaDoc body = sw.getBuffer();
164             response = handler.doEndFragment(ctx, tag, parameterObject, body);
165             handler.doPrepend(ctx, tag, parameterObject, body);
166             
167             if (response != SqlTagHandler.SKIP_BODY) {
168               if (body.length() > 0) {
169                 out.print(body.toString());
170               }
171             }
172
173           }
174         } while (response == SqlTagHandler.REPEAT_BODY);
175
176         ctx.popRemoveFirstPrependMarker(tag);
177
178         if(ctx.peekIterateContext()!= null && ctx.peekIterateContext().getTag() == tag) {
179           ctx.popIterateContext();
180         }
181
182       }
183     }
184   }
185
186   /**
187   * @param bodyContent
188   * @param iterate
189   */

190   protected void iteratePropertyReplace(StringBuffer JavaDoc bodyContent, IterateContext iterate) {
191     if(iterate!=null) {
192       String JavaDoc find = iterate.getProperty() + "[]";
193       String JavaDoc replace = iterate.getProperty() + "[" + iterate.getIndex() + "]";
194       replace(bodyContent, find, replace);
195     }
196   }
197
198   protected static void replace(StringBuffer JavaDoc buffer, String JavaDoc find, String JavaDoc replace) {
199     int pos = buffer.toString().indexOf(find);
200     int len = find.length();
201     while (pos > -1) {
202       buffer.replace(pos, pos + len, replace);
203       pos = buffer.toString().indexOf(find);
204     }
205   }
206   public void addChild(SqlChild child) {
207     children.add(child);
208   }
209
210 }
211
Popular Tags