001/*
002 * Copyright 2002-2012 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.HashMap;
020import java.util.Map;
021
022import org.springframework.util.Assert;
023
024/**
025 * Abstract base class for {@link SqlParameterSource} implementations.
026 * Provides registration of SQL types per parameter.
027 *
028 * @author Juergen Hoeller
029 * @since 2.0
030 */
031public abstract class AbstractSqlParameterSource implements SqlParameterSource {
032
033        private final Map<String, Integer> sqlTypes = new HashMap<String, Integer>();
034
035        private final Map<String, String> typeNames = new HashMap<String, String>();
036
037
038        /**
039         * Register a SQL type for the given parameter.
040         * @param paramName the name of the parameter
041         * @param sqlType the SQL type of the parameter
042         */
043        public void registerSqlType(String paramName, int sqlType) {
044                Assert.notNull(paramName, "Parameter name must not be null");
045                this.sqlTypes.put(paramName, sqlType);
046        }
047
048        /**
049         * Register a SQL type for the given parameter.
050         * @param paramName the name of the parameter
051         * @param typeName the type name of the parameter
052         */
053        public void registerTypeName(String paramName, String typeName) {
054                Assert.notNull(paramName, "Parameter name must not be null");
055                this.typeNames.put(paramName, typeName);
056        }
057
058        /**
059         * Return the SQL type for the given parameter, if registered.
060         * @param paramName the name of the parameter
061         * @return the SQL type of the parameter,
062         * or {@code TYPE_UNKNOWN} if not registered
063         */
064        @Override
065        public int getSqlType(String paramName) {
066                Assert.notNull(paramName, "Parameter name must not be null");
067                Integer sqlType = this.sqlTypes.get(paramName);
068                if (sqlType != null) {
069                        return sqlType;
070                }
071                return TYPE_UNKNOWN;
072        }
073
074        /**
075         * Return the type name for the given parameter, if registered.
076         * @param paramName the name of the parameter
077         * @return the type name of the parameter,
078         * or {@code null} if not registered
079         */
080        @Override
081        public String getTypeName(String paramName) {
082                Assert.notNull(paramName, "Parameter name must not be null");
083                return this.typeNames.get(paramName);
084        }
085
086}