001/*
002 * ============================================================================
003 *  Copyright © 2002-2022 by Thomas Thrien.
004 *  All Rights Reserved.
005 * ============================================================================
006 *  Licensed to the public under the agreements of the GNU Lesser General Public
007 *  License, version 3.0 (the "License"). You may obtain a copy of the License at
008 *
009 *       http://www.gnu.org/licenses/lgpl.html
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 *  License for the specific language governing permissions and limitations
015 *  under the License.
016 */
017
018package org.tquadrat.foundation.sql.internal;
019
020import static java.sql.ParameterMetaData.parameterNullableUnknown;
021import static org.apiguardian.api.API.Status.INTERNAL;
022
023import java.sql.JDBCType;
024import java.sql.SQLException;
025import java.sql.SQLType;
026import java.util.Collection;
027
028import org.apiguardian.api.API;
029import org.tquadrat.foundation.annotation.ClassVersion;
030import org.tquadrat.foundation.sql.ParameterMetaData;
031
032/**
033 *  The base class for a variant of
034 *  {@link java.sql.ParameterMetaData}
035 *  that is used by
036 *  {@link org.tquadrat.foundation.sql.EnhancedPreparedStatement}.
037 *
038 *  @version $Id: ParameterMetaDataBase.java 1020 2022-02-27 21:26:03Z tquadrat $
039 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
040 *  @UMLGraph.link
041 *  @since 0.1.0
042 */
043@ClassVersion( sourceVersion = "$Id: ParameterMetaDataBase.java 1020 2022-02-27 21:26:03Z tquadrat $" )
044@API( status = INTERNAL, since = "0.1.0" )
045public abstract sealed class ParameterMetaDataBase implements ParameterMetaData
046    permits EnhancedPreparedStatementBase.ParameterMetaDataImpl
047{
048        /*------------*\
049    ====** Attributes **=======================================================
050        \*------------*/
051    /**
052     *  The wrapped metadata instance.
053     */
054    private final java.sql.ParameterMetaData m_MetaData;
055
056        /*--------------*\
057    ====** Constructors **=====================================================
058        \*--------------*/
059    /**
060     *  Creates a new instance of {@code ParameterMetaDataBase}.
061     *
062     *  @param metaData The wrapped metadata instance.
063     */
064    protected ParameterMetaDataBase( final java.sql.ParameterMetaData metaData )
065    {
066        m_MetaData = metaData;
067    }   //  ParameterMetaDataBase()
068
069        /*---------*\
070    ====** Methods **==========================================================
071        \*---------*/
072    /**
073     *  {@inheritDoc}
074     */
075    @Override
076    public final String getParameterClassName( final String parameterName ) throws SQLException
077    {
078        final var indexes = getParameterIndexes( parameterName );
079        final var retValue = m_MetaData.getParameterClassName( indexes [0] );
080
081        //---* Done *----------------------------------------------------------
082        return retValue;
083    }   //  getParameterClassName()
084
085    /**
086     *  {@inheritDoc}
087     */
088    @Override
089    public final int getParameterCount() throws SQLException { return getParameterNames().size(); }
090
091    /**
092     *  {@inheritDoc}
093     */
094    @Override
095    public abstract int[] getParameterIndexes( final String parameterName ) throws SQLException;
096
097    /**
098     *  {@inheritDoc}
099     */
100    @Override
101    public abstract Collection<String> getParameterNames();
102
103    /**
104     *  {@inheritDoc}
105     */
106    @Override
107    public final SQLType getParameterType( final String parameterName ) throws SQLException
108    {
109        final var indexes = getParameterIndexes( parameterName );
110        final var retValue = JDBCType.valueOf( m_MetaData.getParameterType( indexes [0] ) );
111
112        //---* Done *----------------------------------------------------------
113        return retValue;
114    }   //  getParameterType()
115
116    /**
117     *  {@inheritDoc}
118     */
119    @Override
120    public String getParameterTypeName( final String parameterName ) throws SQLException
121    {
122        final var indexes = getParameterIndexes( parameterName );
123        final var retValue = m_MetaData.getParameterTypeName( indexes [0] );
124
125        //---* Done *----------------------------------------------------------
126        return retValue;
127    }   //  getParameterTypeName()
128
129    /**
130     *  {@inheritDoc}
131     */
132    @Override
133    public final int getPrecision( final String parameterName ) throws SQLException
134    {
135        final var indexes = getParameterIndexes( parameterName );
136        final var retValue = m_MetaData.getPrecision( indexes [0] );
137
138        //---* Done *----------------------------------------------------------
139        return retValue;
140    }   //  getPrecision()
141
142    /**
143     *  {@inheritDoc}
144     */
145    @Override
146    public final int getScale( final String parameterName ) throws SQLException
147    {
148        final var indexes = getParameterIndexes( parameterName );
149        final var retValue = m_MetaData.getScale( indexes [0] );
150
151        //---* Done *----------------------------------------------------------
152        return retValue;
153    }   //  getScale()
154
155    /**
156     *  {@inheritDoc}
157     */
158    @Override
159    public final int isNullable( final String parameterName ) throws SQLException
160    {
161        final var indexes = getParameterIndexes( parameterName );
162        var retValue = m_MetaData.isNullable( indexes [0] );
163        for( var i = 1; (retValue != parameterNullableUnknown) && (i < indexes.length); ++i )
164        {
165            if( retValue != m_MetaData.isNullable( indexes [i] ) ) retValue = parameterNullableUnknown;
166        }
167
168        //---* Done *----------------------------------------------------------
169        return retValue;
170    }   //  isNullable()
171
172    /**
173     *  {@inheritDoc}
174     */
175    @Override
176    public boolean isSigned( final String parameterName ) throws SQLException
177    {
178        final var indexes = getParameterIndexes( parameterName );
179        final var retValue = m_MetaData.isSigned( indexes [0] );
180
181        //---* Done *----------------------------------------------------------
182        return retValue;
183    }   //  isSigned()
184}
185//  class ParameterMetaDataBase
186
187/*
188 *  End of File
189 */