001: /*
002: * Copyright 2002-2007 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.object;
018:
019: import org.springframework.jdbc.core.PreparedStatementCreator;
020: import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
021: import org.springframework.jdbc.core.PreparedStatementSetter;
022: import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
023: import org.springframework.jdbc.core.namedparam.ParsedSql;
024:
025: /**
026: * Operation object representing a SQL-based operation such as a query or update,
027: * as opposed to a stored procedure.
028: *
029: * <p>Configures a {@link org.springframework.jdbc.core.PreparedStatementCreatorFactory}
030: * based on the declared parameters.
031: *
032: * @author Rod Johnson
033: * @author Juergen Hoeller
034: */
035: public abstract class SqlOperation extends RdbmsOperation {
036:
037: /**
038: * Object enabling us to create PreparedStatementCreators efficiently,
039: * based on this class's declared parameters.
040: */
041: private PreparedStatementCreatorFactory preparedStatementFactory;
042:
043: /** Parsed representation of the SQL statement */
044: private ParsedSql cachedSql;
045:
046: /** Monitor for locking the cached representation of the parsed SQL statement */
047: private final Object parsedSqlMonitor = new Object();
048:
049: /**
050: * Overridden method to configure the PreparedStatementCreatorFactory
051: * based on our declared parameters.
052: */
053: protected final void compileInternal() {
054: this .preparedStatementFactory = new PreparedStatementCreatorFactory(
055: getSql(), getDeclaredParameters());
056: this .preparedStatementFactory
057: .setResultSetType(getResultSetType());
058: this .preparedStatementFactory
059: .setUpdatableResults(isUpdatableResults());
060: this .preparedStatementFactory
061: .setReturnGeneratedKeys(isReturnGeneratedKeys());
062: if (getGeneratedKeysColumnNames() != null) {
063: this .preparedStatementFactory
064: .setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
065: }
066: this .preparedStatementFactory
067: .setNativeJdbcExtractor(getJdbcTemplate()
068: .getNativeJdbcExtractor());
069:
070: onCompileInternal();
071: }
072:
073: /**
074: * Hook method that subclasses may override to post-process compilation.
075: * This implementation does nothing.
076: * @see #compileInternal
077: */
078: protected void onCompileInternal() {
079: }
080:
081: /**
082: * Obtain a parsed representation of this operation's SQL statement.
083: * <p>Typically used for named parameter parsing.
084: */
085: protected ParsedSql getParsedSql() {
086: synchronized (this .parsedSqlMonitor) {
087: if (this .cachedSql == null) {
088: this .cachedSql = NamedParameterUtils
089: .parseSqlStatement(getSql());
090: }
091: return this .cachedSql;
092: }
093: }
094:
095: /**
096: * Return a PreparedStatementSetter to perform an operation
097: * with the given parameters.
098: * @param params the parameter array (may be <code>null</code>)
099: */
100: protected final PreparedStatementSetter newPreparedStatementSetter(
101: Object[] params) {
102: return this .preparedStatementFactory
103: .newPreparedStatementSetter(params);
104: }
105:
106: /**
107: * Return a PreparedStatementCreator to perform an operation
108: * with the given parameters.
109: * @param params the parameter array (may be <code>null</code>)
110: */
111: protected final PreparedStatementCreator newPreparedStatementCreator(
112: Object[] params) {
113: return this .preparedStatementFactory
114: .newPreparedStatementCreator(params);
115: }
116:
117: /**
118: * Return a PreparedStatementCreator to perform an operation
119: * with the given parameters.
120: * @param sqlToUse the actual SQL statement to use (if different from
121: * the factory's, for example because of named parameter expanding)
122: * @param params the parameter array (may be <code>null</code>)
123: */
124: protected final PreparedStatementCreator newPreparedStatementCreator(
125: String sqlToUse, Object[] params) {
126: return this.preparedStatementFactory
127: .newPreparedStatementCreator(sqlToUse, params);
128: }
129:
130: }
|