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