001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
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,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.dbcp2;
018
019import org.apache.commons.dbcp2.PoolingConnection.StatementType;
020
021/**
022 * A key uniquely identifying {@link java.sql.PreparedStatement PreparedStatement}s.
023 * @version $Id: PStmtKey.java 1649430 2015-01-04 21:29:32Z tn $
024 * @since 2.0
025 */
026public class PStmtKey {
027
028    /** SQL defining Prepared or Callable Statement */
029    private final String _sql;
030
031    /** Result set type */
032    private final Integer _resultSetType;
033
034    /** Result set concurrency */
035    private final Integer _resultSetConcurrency;
036
037    /** Database catalog */
038    private final String _catalog;
039
040    /** Auto generated keys */
041    private final Integer _autoGeneratedKeys;
042
043    /** Statement type */
044    private final StatementType _stmtType;
045
046
047    public PStmtKey(String sql) {
048        this(sql, null, StatementType.PREPARED_STATEMENT, null);
049    }
050
051    public PStmtKey(String sql, String catalog) {
052        this(sql, catalog, StatementType.PREPARED_STATEMENT, null);
053    }
054
055    public PStmtKey(String sql, String catalog, int autoGeneratedKeys) {
056        this(sql, catalog, StatementType.PREPARED_STATEMENT, Integer.valueOf(autoGeneratedKeys));
057    }
058
059    public PStmtKey(String sql, String catalog, StatementType stmtType, Integer autoGeneratedKeys) {
060        _sql = sql;
061        _catalog = catalog;
062        _stmtType = stmtType;
063        _autoGeneratedKeys = autoGeneratedKeys;
064        _resultSetType = null;
065        _resultSetConcurrency = null;
066    }
067
068    public  PStmtKey(String sql, int resultSetType, int resultSetConcurrency) {
069        this(sql, null, resultSetType, resultSetConcurrency, StatementType.PREPARED_STATEMENT);
070    }
071
072    public PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency) {
073        this(sql, catalog, resultSetType, resultSetConcurrency, StatementType.PREPARED_STATEMENT);
074    }
075
076    public PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency, StatementType stmtType) {
077        _sql = sql;
078        _catalog = catalog;
079        _resultSetType = Integer.valueOf(resultSetType);
080        _resultSetConcurrency = Integer.valueOf(resultSetConcurrency);
081        _stmtType = stmtType;
082        _autoGeneratedKeys = null;
083    }
084
085
086    public String getSql() {
087        return _sql;
088    }
089
090    public Integer getResultSetType() {
091        return _resultSetType;
092    }
093
094    public Integer getResultSetConcurrency() {
095        return _resultSetConcurrency;
096    }
097
098    public Integer getAutoGeneratedKeys() {
099        return _autoGeneratedKeys;
100    }
101
102    public String getCatalog() {
103        return _catalog;
104    }
105
106    public StatementType getStmtType() {
107        return _stmtType;
108    }
109
110    @Override
111    public boolean equals(Object obj) {
112        if (this == obj) {
113            return true;
114        }
115        if (obj == null) {
116            return false;
117        }
118        if (getClass() != obj.getClass()) {
119            return false;
120        }
121        PStmtKey other = (PStmtKey) obj;
122        if (_catalog == null) {
123            if (other._catalog != null) {
124                return false;
125            }
126        } else if (!_catalog.equals(other._catalog)) {
127            return false;
128        }
129        if (_resultSetConcurrency == null) {
130            if (other._resultSetConcurrency != null) {
131                return false;
132            }
133        } else if (!_resultSetConcurrency.equals(other._resultSetConcurrency)) {
134            return false;
135        }
136        if (_resultSetType == null) {
137            if (other._resultSetType != null) {
138                return false;
139            }
140        } else if (!_resultSetType.equals(other._resultSetType)) {
141            return false;
142        }
143        if (_autoGeneratedKeys == null) {
144            if (other._autoGeneratedKeys != null) {
145                return false;
146            }
147        } else if (!_autoGeneratedKeys.equals(other._autoGeneratedKeys)) {
148            return false;
149        }
150        if (_sql == null) {
151            if (other._sql != null) {
152                return false;
153            }
154        } else if (!_sql.equals(other._sql)) {
155            return false;
156        }
157        if (_stmtType != other._stmtType) {
158            return false;
159        }
160        return true;
161    }
162
163    @Override
164    public int hashCode() {
165        final int prime = 31;
166        int result = 1;
167        result = prime * result + (_catalog == null ? 0 : _catalog.hashCode());
168        result = prime * result + (_resultSetConcurrency == null ? 0 : _resultSetConcurrency.hashCode());
169        result = prime * result + (_resultSetType == null ? 0 : _resultSetType.hashCode());
170        result = prime * result + (_sql == null ? 0 : _sql.hashCode());
171        result = prime * result + (_autoGeneratedKeys == null ? 0 : _autoGeneratedKeys.hashCode());
172        result = prime * result + _stmtType.hashCode();
173        return result;
174    }
175
176    @Override
177    public String toString() {
178        StringBuffer buf = new StringBuffer();
179        buf.append("PStmtKey: sql=");
180        buf.append(_sql);
181        buf.append(", catalog=");
182        buf.append(_catalog);
183        buf.append(", resultSetType=");
184        buf.append(_resultSetType);
185        buf.append(", resultSetConcurrency=");
186        buf.append(_resultSetConcurrency);
187        buf.append(", autoGeneratedKeys=");
188        buf.append(_autoGeneratedKeys);
189        buf.append(", statmentType=");
190        buf.append(_stmtType);
191        return buf.toString();
192    }
193}