001    /*
002     * The MIT License
003     *
004     * Copyright (c) 2012, Ninja Squad
005     *
006     * Permission is hereby granted, free of charge, to any person obtaining a copy
007     * of this software and associated documentation files (the "Software"), to deal
008     * in the Software without restriction, including without limitation the rights
009     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010     * copies of the Software, and to permit persons to whom the Software is
011     * furnished to do so, subject to the following conditions:
012     *
013     * The above copyright notice and this permission notice shall be included in
014     * all copies or substantial portions of the Software.
015     *
016     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022     * THE SOFTWARE.
023     */
024    
025    package com.ninja_squad.dbsetup.bind;
026    
027    import com.ninja_squad.dbsetup.DbSetup;
028    
029    import java.sql.ParameterMetaData;
030    import java.sql.SQLException;
031    import java.sql.Types;
032    
033    /**
034     * Default implementation of {@link BinderConfiguration}, used by default by {@link DbSetup}.
035     * @author JB Nizet
036     */
037    public class DefaultBinderConfiguration implements BinderConfiguration {
038    
039        /**
040         * A shareable, reusable instance of this class.
041         */
042        public static final DefaultBinderConfiguration INSTANCE = new DefaultBinderConfiguration();
043    
044        /**
045         * Constructor. Protected because it doesn't make much sense to instantiate this class,
046         * but extending it can be useful.
047         */
048        protected DefaultBinderConfiguration() {
049        }
050    
051        /**
052         * Uses the parameter type of the given parameter and returns the following Binders depending on the type
053         * got from the metadata.
054         * <ul>
055         *   <li>null metadata (i.e. metadata not used or not returned): {@link Binders#defaultBinder()}</li>
056         *   <li>VARCHAR, CHAR, LONGNVARCHAR, LONGVARCHAR, NCHAR, NVARCHAR :
057         *       {@link Binders#stringBinder()}</li>
058         *   <li>DATE : {@link Binders#dateBinder()}</li>
059         *   <li>TIME : {@link Binders#timeBinder()}</li>
060         *   <li>TIMESTAMP : {@link Binders#timestampBinder()}</li>
061         *   <li>INTEGER, BIGINT, SMALLINT, TINYINT : {@link Binders#integerBinder()}</li>
062         *   <li>DECIMAL, DOUBLE, FLOAT, NUMERIC, REAL : {@link Binders#decimalBinder()}</li>
063         *   <li>other : {@link Binders#defaultBinder()}</li>
064         * </ul>
065         *
066         * If the parameter type can't be obtained from the metadata, the default binder is returned.
067         */
068        @Override
069        public Binder getBinder(ParameterMetaData metadata, int param) throws SQLException {
070            if (metadata == null) {
071                return Binders.defaultBinder();
072            }
073            try {
074                int sqlType = metadata.getParameterType(param);
075                if (sqlType == Types.DATE) {
076                    return Binders.dateBinder();
077                }
078                if (sqlType == Types.TIME) {
079                    return Binders.timeBinder();
080                }
081                if (sqlType == Types.TIMESTAMP) {
082                    return Binders.timestampBinder();
083                }
084                if (sqlType == Types.BIGINT
085                    || sqlType == Types.INTEGER
086                    || sqlType == Types.SMALLINT
087                    || sqlType == Types.TINYINT) {
088                    return Binders.integerBinder();
089                }
090                if (sqlType == Types.DECIMAL
091                    || sqlType == Types.DOUBLE
092                    || sqlType == Types.FLOAT
093                    || sqlType == Types.NUMERIC
094                    || sqlType == Types.REAL) {
095                    return Binders.decimalBinder();
096                }
097                if (sqlType == Types.VARCHAR
098                    || sqlType == Types.CHAR
099                    || sqlType == Types.LONGNVARCHAR
100                    || sqlType == Types.LONGVARCHAR
101                    || sqlType == Types.NCHAR
102                    || sqlType == Types.NVARCHAR) {
103                    return Binders.stringBinder();
104                }
105                return Binders.defaultBinder();
106            }
107            catch (SQLException e) {
108                // the database can't return types from parameters. Fall back to default binder.
109                return Binders.defaultBinder();
110            }
111        }
112    
113        @Override
114        public String toString() {
115            return "DefaultBinderConfiguration";
116        }
117    }