Source Code Cross Referenced for CallableStatementCreatorFactory.java in  » J2EE » spring-framework-2.5 » org » springframework » jdbc » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.5 » org.springframework.jdbc.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2006 the original author or authors.
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:
017:        package org.springframework.jdbc.core;
018:
019:        import java.sql.CallableStatement;
020:        import java.sql.Connection;
021:        import java.sql.ResultSet;
022:        import java.sql.SQLException;
023:        import java.util.HashMap;
024:        import java.util.LinkedList;
025:        import java.util.List;
026:        import java.util.Map;
027:
028:        import org.springframework.dao.InvalidDataAccessApiUsageException;
029:        import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
030:
031:        /**
032:         * Helper class that can efficiently create multiple CallableStatementCreator
033:         * objects with different parameters based on a SQL statement and a single
034:         * set of parameter declarations.
035:         *
036:         * @author Rod Johnson
037:         * @author Thomas Risberg
038:         * @author Juergen Hoeller
039:         */
040:        public class CallableStatementCreatorFactory {
041:
042:            /** The SQL call string, which won't change when the parameters change. */
043:            private final String callString;
044:
045:            /** List of SqlParameter objects. May not be <code>null</code>. */
046:            private final List declaredParameters;
047:
048:            private int resultSetType = ResultSet.TYPE_FORWARD_ONLY;
049:
050:            private boolean updatableResults = false;
051:
052:            private NativeJdbcExtractor nativeJdbcExtractor;
053:
054:            /**
055:             * Create a new factory. Will need to add parameters
056:             * via the addParameter() method or have no parameters.
057:             */
058:            public CallableStatementCreatorFactory(String callString) {
059:                this .callString = callString;
060:                this .declaredParameters = new LinkedList();
061:            }
062:
063:            /**
064:             * Create a new factory with sql and the given parameters.
065:             * @param callString the SQL call string
066:             * @param declaredParameters list of SqlParameter objects
067:             */
068:            public CallableStatementCreatorFactory(String callString,
069:                    List declaredParameters) {
070:                this .callString = callString;
071:                this .declaredParameters = declaredParameters;
072:            }
073:
074:            /**
075:             * Add a new declared parameter.
076:             * Order of parameter addition is significant.
077:             */
078:            public void addParameter(SqlParameter param) {
079:                this .declaredParameters.add(param);
080:            }
081:
082:            /**
083:             * Set whether to use prepared statements that return a
084:             * specific type of ResultSet.
085:             * @param resultSetType the ResultSet type
086:             * @see java.sql.ResultSet#TYPE_FORWARD_ONLY
087:             * @see java.sql.ResultSet#TYPE_SCROLL_INSENSITIVE
088:             * @see java.sql.ResultSet#TYPE_SCROLL_SENSITIVE
089:             */
090:            public void setResultSetType(int resultSetType) {
091:                this .resultSetType = resultSetType;
092:            }
093:
094:            /**
095:             * Set whether to use prepared statements capable of returning
096:             * updatable ResultSets.
097:             */
098:            public void setUpdatableResults(boolean updatableResults) {
099:                this .updatableResults = updatableResults;
100:            }
101:
102:            /**
103:             * Specify the NativeJdbcExtractor to use for unwrapping
104:             * CallableStatements, if any.
105:             */
106:            public void setNativeJdbcExtractor(
107:                    NativeJdbcExtractor nativeJdbcExtractor) {
108:                this .nativeJdbcExtractor = nativeJdbcExtractor;
109:            }
110:
111:            /**
112:             * Return a new CallableStatementCreator instance given this parameters.
113:             * @param inParams List of parameters. May be <code>null</code>.
114:             */
115:            public CallableStatementCreator newCallableStatementCreator(
116:                    Map inParams) {
117:                return new CallableStatementCreatorImpl(
118:                        inParams != null ? inParams : new HashMap());
119:            }
120:
121:            /**
122:             * Return a new CallableStatementCreator instance given this parameter mapper.
123:             * @param inParamMapper ParameterMapper implementation that will return a Map of parameters. May not be <code>null</code>.
124:             */
125:            public CallableStatementCreator newCallableStatementCreator(
126:                    ParameterMapper inParamMapper) {
127:                return new CallableStatementCreatorImpl(inParamMapper);
128:            }
129:
130:            /**
131:             * CallableStatementCreator implementation returned by this class.
132:             */
133:            private class CallableStatementCreatorImpl implements 
134:                    CallableStatementCreator, SqlProvider, ParameterDisposer {
135:
136:                private ParameterMapper inParameterMapper;
137:
138:                private Map inParameters;
139:
140:                /**
141:                 * Create a new CallableStatementCreatorImpl.
142:                 * @param inParamMapper ParameterMapper implementation for mapping input parameters.
143:                 * May not be <code>null</code>.
144:                 */
145:                public CallableStatementCreatorImpl(
146:                        ParameterMapper inParamMapper) {
147:                    this .inParameterMapper = inParamMapper;
148:                }
149:
150:                /**
151:                 * Create a new CallableStatementCreatorImpl.
152:                 * @param inParams list of SqlParameter objects. May not be <code>null</code>.
153:                 */
154:                public CallableStatementCreatorImpl(Map inParams) {
155:                    this .inParameters = inParams;
156:                }
157:
158:                public CallableStatement createCallableStatement(Connection con)
159:                        throws SQLException {
160:                    // If we were given a ParameterMapper - we must let the mapper do its thing to create the Map.
161:                    if (this .inParameterMapper != null) {
162:                        this .inParameters = this .inParameterMapper
163:                                .createMap(con);
164:                    } else {
165:                        if (this .inParameters == null) {
166:                            throw new InvalidDataAccessApiUsageException(
167:                                    "A ParameterMapper or a Map of parameters must be provided");
168:                        }
169:                    }
170:
171:                    CallableStatement cs = null;
172:                    if (resultSetType == ResultSet.TYPE_FORWARD_ONLY
173:                            && !updatableResults) {
174:                        cs = con.prepareCall(callString);
175:                    } else {
176:                        cs = con.prepareCall(callString, resultSetType,
177:                                updatableResults ? ResultSet.CONCUR_UPDATABLE
178:                                        : ResultSet.CONCUR_READ_ONLY);
179:                    }
180:
181:                    // Determine CallabeStatement to pass to custom types.
182:                    CallableStatement csToUse = cs;
183:                    if (nativeJdbcExtractor != null) {
184:                        csToUse = nativeJdbcExtractor
185:                                .getNativeCallableStatement(cs);
186:                    }
187:
188:                    int sqlColIndx = 1;
189:                    for (int i = 0; i < declaredParameters.size(); i++) {
190:                        SqlParameter declaredParameter = (SqlParameter) declaredParameters
191:                                .get(i);
192:                        if (!declaredParameter.isResultsParameter()) {
193:                            // So, it's a call parameter - part of the call string.
194:                            // Get the value - it may still be null.
195:                            Object inValue = this .inParameters
196:                                    .get(declaredParameter.getName());
197:                            if (declaredParameter instanceof  ResultSetSupportingSqlParameter) {
198:                                // It's an output parameter: SqlReturnResultSet parameters already excluded.
199:                                // It need not (but may be) supplied by the caller.
200:                                if (declaredParameter instanceof  SqlOutParameter) {
201:                                    if (declaredParameter.getTypeName() != null) {
202:                                        cs
203:                                                .registerOutParameter(
204:                                                        sqlColIndx,
205:                                                        declaredParameter
206:                                                                .getSqlType(),
207:                                                        declaredParameter
208:                                                                .getTypeName());
209:                                    } else {
210:                                        if (declaredParameter.getScale() != null) {
211:                                            cs.registerOutParameter(sqlColIndx,
212:                                                    declaredParameter
213:                                                            .getSqlType(),
214:                                                    declaredParameter
215:                                                            .getScale()
216:                                                            .intValue());
217:                                        } else {
218:                                            cs.registerOutParameter(sqlColIndx,
219:                                                    declaredParameter
220:                                                            .getSqlType());
221:                                        }
222:                                    }
223:                                    if ((declaredParameter)
224:                                            .isInputValueProvided()
225:                                            || inValue != null) {
226:                                        StatementCreatorUtils
227:                                                .setParameterValue(csToUse,
228:                                                        sqlColIndx,
229:                                                        declaredParameter,
230:                                                        inValue);
231:                                    }
232:                                }
233:                            } else {
234:                                // It's an input parameter- must be supplied by the caller.
235:                                if (!this .inParameters
236:                                        .containsKey(declaredParameter
237:                                                .getName())) {
238:                                    throw new InvalidDataAccessApiUsageException(
239:                                            "Required input parameter '"
240:                                                    + declaredParameter
241:                                                            .getName()
242:                                                    + "' is missing");
243:                                }
244:                                StatementCreatorUtils.setParameterValue(
245:                                        csToUse, sqlColIndx, declaredParameter,
246:                                        inValue);
247:                            }
248:                            sqlColIndx++;
249:                        }
250:                    }
251:
252:                    return cs;
253:                }
254:
255:                public String getSql() {
256:                    return callString;
257:                }
258:
259:                public void cleanupParameters() {
260:                    if (this .inParameters != null) {
261:                        StatementCreatorUtils
262:                                .cleanupParameters(this .inParameters.values());
263:                    }
264:                }
265:
266:                public String toString() {
267:                    StringBuffer buf = new StringBuffer(
268:                            "CallableStatementCreatorFactory.CallableStatementCreatorImpl: sql=[");
269:                    buf.append(callString).append("]; parameters=").append(
270:                            this.inParameters);
271:                    return buf.toString();
272:                }
273:            }
274:
275:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.