001package com.randomnoun.common.db;
002
003import java.util.Arrays;
004import java.util.Objects;
005
006/** A container class for SQL with positional placeholders, and the arguments to be substituted 
007 * into those placeholders, to be used in a JdbcTemplate.
008 * 
009 * @author knoxg
010 */
011public class SqlWithArguments {
012        
013        private String sql;
014        private Object[] args;
015        private int[] argTypes;
016        
017        /** Create a new SqlWithArguments object.
018         * 
019         * @param sql the SQL, containing '?' positional placeholders
020         * @param args the arguments to be substituted into those placeholders
021         * @param argTypes the SQL datatypes of the arguments (as java.sql.Types constants)   
022         */
023        public SqlWithArguments(String sql, Object[] args, int[] argTypes) {
024                this.sql = sql;
025                this.args = args;
026                this.argTypes = argTypes;
027        }
028
029        /** Return the SQL, containing '?' positional placeholders
030         * 
031         * @return the SQL, containing '?' positional placeholders
032         */
033        public String getSql() {
034                return sql;
035        }
036
037        
038        /** Return the arguments to be substituted into the SQL placeholders
039         * 
040         * @return the arguments to be substituted into the SQL placeholders
041         */
042        public Object[] getArgs() {
043                return args;
044        }
045        
046        /** Return the SQL datatypes of the arguments (as java.sql.Types constants)
047         *  
048         * @return the SQL datatypes of the arguments (as java.sql.Types constants)
049         */
050        public int[] getArgTypes() {
051                return argTypes;
052                
053        }
054
055        // setters
056        
057        public void setSql(String sql) {
058                this.sql = sql;
059        }
060
061        public void setArgs(Object[] args) {
062                this.args = args;
063        }
064
065        public void setArgTypes(int[] argTypes) {
066                this.argTypes = argTypes;
067        }
068        
069
070    @Override
071    public int hashCode() {
072        return Objects.hash(sql, Arrays.hashCode(args), Arrays.hashCode(argTypes));
073    }
074
075    @Override
076    public boolean equals(Object o) {
077        if (this == o) { 
078                return true; 
079        } else if (o == null || getClass() != o.getClass()) { 
080                return false;
081        } else {
082                SqlWithArguments that = (SqlWithArguments) o;
083                return Objects.equals(sql, that.sql) &&
084                        Arrays.equals(args, that.args) &&
085                        Arrays.equals(argTypes, that.argTypes);
086        }
087    }
088        
089}