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.core.namedparam;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: /**
023: * Holds information about a parsed SQL statement.
024: *
025: * @author Thomas Risberg
026: * @author Juergen Hoeller
027: * @since 2.0
028: */
029: public class ParsedSql {
030:
031: private String originalSql;
032:
033: private List parameterNames = new ArrayList();
034:
035: private List parameterIndexes = new ArrayList();
036:
037: private int namedParameterCount;
038:
039: private int unnamedParameterCount;
040:
041: private int totalParameterCount;
042:
043: /**
044: * Create a new instance of the {@link ParsedSql} class.
045: * @param originalSql the SQL statement that is being (or is to be) parsed
046: */
047: ParsedSql(String originalSql) {
048: this .originalSql = originalSql;
049: }
050:
051: /**
052: * Return the SQL statement that is being parsed.
053: */
054: String getOriginalSql() {
055: return this .originalSql;
056: }
057:
058: /**
059: * Add a named parameter parsed from this SQL statement.
060: * @param parameterName the name of the parameter
061: * @param startIndex the start index in the original SQL String
062: * @param endIndex the end index in the original SQL String
063: */
064: void addNamedParameter(String parameterName, int startIndex,
065: int endIndex) {
066: this .parameterNames.add(parameterName);
067: this .parameterIndexes.add(new int[] { startIndex, endIndex });
068: }
069:
070: /**
071: * Return all of the parameters (bind variables) in the parsed SQL statement.
072: * Repeated occurences of the same parameter name are included here.
073: */
074: List getParameterNames() {
075: return this .parameterNames;
076: }
077:
078: /**
079: * Return the parameter indexes for the specified parameter.
080: * @param parameterPosition the position of the parameter
081: * (as index in the parameter names List)
082: * @return the start index and end index, combined into
083: * a int array of length 2
084: */
085: int[] getParameterIndexes(int parameterPosition) {
086: return (int[]) this .parameterIndexes.get(parameterPosition);
087: }
088:
089: /**
090: * Set the count of named parameters in the SQL statement.
091: * Each parameter name counts once; repeated occurences do not count here.
092: */
093: void setNamedParameterCount(int namedParameterCount) {
094: this .namedParameterCount = namedParameterCount;
095: }
096:
097: /**
098: * Return the count of named parameters in the SQL statement.
099: * Each parameter name counts once; repeated occurences do not count here.
100: */
101: int getNamedParameterCount() {
102: return this .namedParameterCount;
103: }
104:
105: /**
106: * Set the count of all of the unnamed parameters in the SQL statement.
107: */
108: void setUnnamedParameterCount(int unnamedParameterCount) {
109: this .unnamedParameterCount = unnamedParameterCount;
110: }
111:
112: /**
113: * Return the count of all of the unnamed parameters in the SQL statement.
114: */
115: int getUnnamedParameterCount() {
116: return this .unnamedParameterCount;
117: }
118:
119: /**
120: * Set the total count of all of the parameters in the SQL statement.
121: * Repeated occurences of the same parameter name do count here.
122: */
123: void setTotalParameterCount(int totalParameterCount) {
124: this .totalParameterCount = totalParameterCount;
125: }
126:
127: /**
128: * Return the total count of all of the parameters in the SQL statement.
129: * Repeated occurences of the same parameter name do count here.
130: */
131: int getTotalParameterCount() {
132: return this .totalParameterCount;
133: }
134:
135: /**
136: * Exposes the original SQL String.
137: */
138: public String toString() {
139: return this.originalSql;
140: }
141:
142: }
|