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 java.sql.ParameterMetaData;
028    import java.sql.SQLException;
029    import java.sql.Types;
030    
031    import com.ninja_squad.dbsetup.DbSetup;
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>VARCHAR, CHAR, LONGNVARCHAR, LONGVARCHAR, NCHAR, NVARCHAR :
056         *       {@link Binders#stringBinder()}</li>
057         *   <li>DATE : {@link Binders#dateBinder()}</li>
058         *   <li>TIME : {@link Binders#timeBinder()}</li>
059         *   <li>TIMESTAMP : {@link Binders#timestampBinder()}</li>
060         *   <li>INTEGER, BIGINT, SMALLINT, TINYINT : {@link Binders#integerBinder()}</li>
061         *   <li>DECIMAL, DOUBLE, FLOAT, NUMERIC, REAL : {@link Binders#decimalBinder()}</li>
062         *   <li>other : {@link Binders#defaultBinder()}</li>
063         */
064        @Override
065        public Binder getBinder(ParameterMetaData metadata, int param) throws SQLException {
066            int sqlType = metadata.getParameterType(param);
067            if (sqlType == Types.DATE) {
068                return Binders.dateBinder();
069            }
070            if (sqlType == Types.TIME) {
071                return Binders.timeBinder();
072            }
073            if (sqlType == Types.TIMESTAMP) {
074                return Binders.timestampBinder();
075            }
076            if (sqlType == Types.BIGINT
077                || sqlType == Types.INTEGER
078                || sqlType == Types.SMALLINT
079                || sqlType == Types.TINYINT) {
080                return Binders.integerBinder();
081            }
082            if (sqlType == Types.DECIMAL
083                || sqlType == Types.DOUBLE
084                || sqlType == Types.FLOAT
085                || sqlType == Types.NUMERIC
086                || sqlType == Types.REAL) {
087                return Binders.decimalBinder();
088            }
089            if (sqlType == Types.VARCHAR
090                || sqlType == Types.CHAR
091                || sqlType == Types.LONGNVARCHAR
092                || sqlType == Types.LONGVARCHAR
093                || sqlType == Types.NCHAR
094                || sqlType == Types.NVARCHAR) {
095                return Binders.stringBinder();
096            }
097            return Binders.defaultBinder();
098        }
099    
100        @Override
101        public String toString() {
102            return "DefaultBinderConfiguration";
103        }
104    }