📄 rdbmsoperation.java
字号:
return sql;
}
/**
* Add anonymous parameters, specifying only their SQL types
* as defined in the <code>java.sql.Types</code> class.
* <p>Parameter ordering is significant. This method is an alternative
* to the declareParameter() method, which should normally be preferred.
* @param types array of SQL types as defined in the
* <code>java.sql.Types</code> class
* @throws InvalidDataAccessApiUsageException if the operation is already compiled
*/
public void setTypes(int[] types) throws InvalidDataAccessApiUsageException {
if (isCompiled()) {
throw new InvalidDataAccessApiUsageException("Cannot add parameters once query is compiled");
}
if (types != null) {
for (int i = 0; i < types.length; i++) {
declareParameter(new SqlParameter(types[i]));
}
}
}
/**
* Declare a parameter. The order in which this method is called is significant.
* @param param SqlParameter to add. This will specify SQL type and (optionally)
* the parameter's name.
* @throws InvalidDataAccessApiUsageException if the operation is already compiled,
* and hence cannot be configured further
*/
public void declareParameter(SqlParameter param) throws InvalidDataAccessApiUsageException {
if (isCompiled()) {
throw new InvalidDataAccessApiUsageException("Cannot add parameters once query is compiled");
}
this.declaredParameters.add(param);
}
/**
* Return a list of the declared SqlParameter objects.
*/
protected List getDeclaredParameters() {
return declaredParameters;
}
/**
* Ensures compilation if used in a bean factory.
*/
public void afterPropertiesSet() {
compile();
}
/**
* Compile this query.
* Ignores subsequent attempts to compile.
* @throws InvalidDataAccessApiUsageException if the object hasn't
* been correctly initialized, for example if no DataSource has been provided
*/
public final void compile() throws InvalidDataAccessApiUsageException {
if (!isCompiled()) {
if (getSql() == null) {
throw new InvalidDataAccessApiUsageException("sql is required");
}
try {
this.jdbcTemplate.afterPropertiesSet();
}
catch (IllegalArgumentException ex) {
throw new InvalidDataAccessApiUsageException(ex.getMessage());
}
compileInternal();
this.compiled = true;
if (logger.isDebugEnabled()) {
logger.debug("RdbmsOperation with SQL [" + getSql() + "] compiled");
}
}
}
/**
* Subclasses must implement to perform their own compilation.
* Invoked after this class's compilation is complete.
* <p>Subclasses can assume that SQL has been supplied and that
* a DataSource has been supplied.
* @throws InvalidDataAccessApiUsageException if the subclass
* hasn't been properly configured.
*/
protected abstract void compileInternal() throws InvalidDataAccessApiUsageException;
/**
* Is this operation "compiled"? Compilation, as in JDO,
* means that the operation is fully configured, and ready to use.
* The exact meaning of compilation will vary between subclasses.
* @return whether this operation is compiled, and ready to use.
*/
public boolean isCompiled() {
return compiled;
}
/**
* Check whether this operation has been compiled already;
* lazily compile it if not already compiled.
* <p>Automatically called by <code>validateNamedParameters</code>.
* @see #validateParameters
*/
protected void checkCompiled() {
if (!isCompiled()) {
logger.debug("SQL operation not compiled before execution - invoking compile");
compile();
}
}
/**
* Validate the parameters passed to an execute method based on declared parameters.
* Subclasses should invoke this method before every <code>executeQuery()</code>
* or <code>update()</code> method.
* @param parameters parameters supplied (may be <code>null</code>)
* @throws InvalidDataAccessApiUsageException if the parameters are invalid
*/
protected void validateParameters(Object[] parameters) throws InvalidDataAccessApiUsageException {
checkCompiled();
int declaredInParameters = 0;
if (this.declaredParameters != null) {
Iterator it = this.declaredParameters.iterator();
while (it.hasNext()) {
Object param = it.next();
if (!(param instanceof ResultSetSupportingSqlParameter)) {
if (!supportsLobParameters() &&
(((SqlParameter) param).getSqlType() == Types.BLOB ||
((SqlParameter) param).getSqlType() == Types.CLOB)) {
throw new InvalidDataAccessApiUsageException(
"BLOB or CLOB parameters are not allowed for this kind of operation");
}
declaredInParameters++;
}
}
}
if (parameters != null) {
if (this.declaredParameters == null) {
throw new InvalidDataAccessApiUsageException("Didn't expect any parameters: none was declared");
}
if (parameters.length < declaredInParameters) {
throw new InvalidDataAccessApiUsageException(
parameters.length + " parameters were supplied, but " +
declaredInParameters + " in parameters were declared in class [" +
getClass().getName() + "]");
}
if (!allowsUnusedParameters() && parameters.length > this.declaredParameters.size()) {
throw new InvalidDataAccessApiUsageException(
parameters.length + " parameters were supplied, but " +
this.declaredParameters.size() + " parameters were declared " +
"in class [" + getClass().getName() + "]");
}
}
else {
// No parameters were supplied.
if (this.declaredParameters != null && !this.declaredParameters.isEmpty()) {
throw new InvalidDataAccessApiUsageException(
this.declaredParameters.size() + " parameters must be supplied");
}
}
}
/**
* Validate the named parameters passed to an execute method based on declared parameters.
* Subclasses should invoke this method before every <code>executeQuery()</code> or
* <code>update()</code> method.
* @param parameters parameter Map supplied. May be <code>null</code>.
* @throws InvalidDataAccessApiUsageException if the parameters are invalid
*/
protected void validateNamedParameters(Map parameters) throws InvalidDataAccessApiUsageException {
checkCompiled();
if (this.declaredParameters != null) {
Iterator it = this.declaredParameters.iterator();
while (it.hasNext()) {
Object param = it.next();
if (!(param instanceof ResultSetSupportingSqlParameter)) {
if (!supportsLobParameters() &&
(((SqlParameter) param).getSqlType() == Types.BLOB ||
((SqlParameter) param).getSqlType() == Types.CLOB)) {
throw new InvalidDataAccessApiUsageException(
"BLOB or CLOB parameters are not allowed for this kind of operation");
}
if (((SqlParameter) param).getName() == null) {
throw new InvalidDataAccessApiUsageException(
"All parameters must have name specified when using the methods " +
"dedicated to named parameter support");
}
if (!parameters.containsKey(((SqlParameter) param).getName())) {
throw new InvalidDataAccessApiUsageException(
"The parameter named '" + ((SqlParameter)param).getName() +
"' were not among the parameters supplied: " +
parameters.keySet());
}
}
}
}
if (parameters != null && parameters.size() > 0) {
if (this.declaredParameters == null) {
throw new InvalidDataAccessApiUsageException("Didn't expect any parameters: none was declared");
}
}
else {
// No parameters were supplied.
if (this.declaredParameters != null && !this.declaredParameters.isEmpty()) {
throw new InvalidDataAccessApiUsageException("Parameters must be supplied");
}
}
}
/**
* Return whether BLOB or CLOB parameters are supported
* for this kind of operation. Default is "true".
*/
protected boolean supportsLobParameters() {
return true;
}
/**
* Return whether this operation accepts additional parameters that are
* given but not actually used. Applies in particular to parameter Maps.
* @see StoredProcedure
*/
protected boolean allowsUnusedParameters() {
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -