001/*
002 * Copyright 2002-2017 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.metadata;
018
019import java.sql.DatabaseMetaData;
020import java.sql.SQLException;
021import java.sql.Types;
022
023import org.springframework.jdbc.core.ColumnMapRowMapper;
024import org.springframework.jdbc.core.SqlOutParameter;
025import org.springframework.jdbc.core.SqlParameter;
026import org.springframework.lang.Nullable;
027
028/**
029 * Postgres-specific implementation for the {@link CallMetaDataProvider} interface.
030 * This class is intended for internal use by the Simple JDBC classes.
031 *
032 * @author Thomas Risberg
033 * @since 2.5
034 */
035public class PostgresCallMetaDataProvider extends GenericCallMetaDataProvider {
036
037        private static final String RETURN_VALUE_NAME = "returnValue";
038
039
040        public PostgresCallMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLException {
041                super(databaseMetaData);
042        }
043
044
045        @Override
046        public boolean isReturnResultSetSupported() {
047                return false;
048        }
049
050        @Override
051        public boolean isRefCursorSupported() {
052                return true;
053        }
054
055        @Override
056        public int getRefCursorSqlType() {
057                return Types.OTHER;
058        }
059
060        @Override
061        @Nullable
062        public String metaDataSchemaNameToUse(@Nullable String schemaName) {
063                // Use public schema if no schema specified
064                return (schemaName == null ? "public" : super.metaDataSchemaNameToUse(schemaName));
065        }
066
067        @Override
068        public SqlParameter createDefaultOutParameter(String parameterName, CallParameterMetaData meta) {
069                if (meta.getSqlType() == Types.OTHER && "refcursor".equals(meta.getTypeName())) {
070                        return new SqlOutParameter(parameterName, getRefCursorSqlType(), new ColumnMapRowMapper());
071                }
072                else {
073                        return super.createDefaultOutParameter(parameterName, meta);
074                }
075        }
076
077        @Override
078        public boolean byPassReturnParameter(String parameterName) {
079                return RETURN_VALUE_NAME.equals(parameterName);
080        }
081
082}