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 * @since 2.0
024 */
025public class PStmtKey {
026
027    /** SQL defining Prepared or Callable Statement */
028    private final String _sql;
029
030    /** Result set type */
031    private final Integer _resultSetType;
032
033    /** Result set concurrency */
034    private final Integer _resultSetConcurrency;
035
036    /** Database catalog */
037    private final String _catalog;
038
039    /** Auto generated keys */
040    private final Integer _autoGeneratedKeys;
041
042    /** Statement type */
043    private final StatementType _stmtType;
044
045
046    public PStmtKey(String sql) {
047        this(sql, null, StatementType.PREPARED_STATEMENT, null);
048    }
049
050    public PStmtKey(String sql, String catalog) {
051        this(sql, catalog, StatementType.PREPARED_STATEMENT, null);
052    }
053
054    public PStmtKey(String sql, String catalog, int autoGeneratedKeys) {
055        this(sql, catalog, StatementType.PREPARED_STATEMENT, Integer.valueOf(autoGeneratedKeys));
056    }
057
058    public PStmtKey(String sql, String catalog, StatementType stmtType, Integer autoGeneratedKeys) {
059        _sql = sql;
060        _catalog = catalog;
061        _stmtType = stmtType;
062        _autoGeneratedKeys = autoGeneratedKeys;
063        _resultSetType = null;
064        _resultSetConcurrency = null;
065    }
066
067    public  PStmtKey(String sql, int resultSetType, int resultSetConcurrency) {
068        this(sql, null, resultSetType, resultSetConcurrency, StatementType.PREPARED_STATEMENT);
069    }
070
071    public PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency) {
072        this(sql, catalog, resultSetType, resultSetConcurrency, StatementType.PREPARED_STATEMENT);
073    }
074
075    public PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency, StatementType stmtType) {
076        _sql = sql;
077        _catalog = catalog;
078        _resultSetType = Integer.valueOf(resultSetType);
079        _resultSetConcurrency = Integer.valueOf(resultSetConcurrency);
080        _stmtType = stmtType;
081        _autoGeneratedKeys = null;
082    }
083
084
085    public String getSql() {
086        return _sql;
087    }
088
089    public Integer getResultSetType() {
090        return _resultSetType;
091    }
092
093    public Integer getResultSetConcurrency() {
094        return _resultSetConcurrency;
095    }
096
097    public Integer getAutoGeneratedKeys() {
098        return _autoGeneratedKeys;
099    }
100
101    public String getCatalog() {
102        return _catalog;
103    }
104
105    public StatementType getStmtType() {
106        return _stmtType;
107    }
108
109    @Override
110    public boolean equals(Object obj) {
111        if (this == obj) {
112            return true;
113        }
114        if (obj == null) {
115            return false;
116        }
117        if (getClass() != obj.getClass()) {
118            return false;
119        }
120        PStmtKey other = (PStmtKey) obj;
121        if (_catalog == null) {
122            if (other._catalog != null) {
123                return false;
124            }
125        } else if (!_catalog.equals(other._catalog)) {
126            return false;
127        }
128        if (_resultSetConcurrency == null) {
129            if (other._resultSetConcurrency != null) {
130                return false;
131            }
132        } else if (!_resultSetConcurrency.equals(other._resultSetConcurrency)) {
133            return false;
134        }
135        if (_resultSetType == null) {
136            if (other._resultSetType != null) {
137                return false;
138            }
139        } else if (!_resultSetType.equals(other._resultSetType)) {
140            return false;
141        }
142        if (_autoGeneratedKeys == null) {
143            if (other._autoGeneratedKeys != null) {
144                return false;
145            }
146        } else if (!_autoGeneratedKeys.equals(other._autoGeneratedKeys)) {
147            return false;
148        }
149        if (_sql == null) {
150            if (other._sql != null) {
151                return false;
152            }
153        } else if (!_sql.equals(other._sql)) {
154            return false;
155        }
156        if (_stmtType != other._stmtType) {
157            return false;
158        }
159        return true;
160    }
161
162    @Override
163    public int hashCode() {
164        final int prime = 31;
165        int result = 1;
166        result = prime * result + (_catalog == null ? 0 : _catalog.hashCode());
167        result = prime * result + (_resultSetConcurrency == null ? 0 : _resultSetConcurrency.hashCode());
168        result = prime * result + (_resultSetType == null ? 0 : _resultSetType.hashCode());
169        result = prime * result + (_sql == null ? 0 : _sql.hashCode());
170        result = prime * result + (_autoGeneratedKeys == null ? 0 : _autoGeneratedKeys.hashCode());
171        result = prime * result + _stmtType.hashCode();
172        return result;
173    }
174
175    @Override
176    public String toString() {
177        StringBuffer buf = new StringBuffer();
178        buf.append("PStmtKey: sql=");
179        buf.append(_sql);
180        buf.append(", catalog=");
181        buf.append(_catalog);
182        buf.append(", resultSetType=");
183        buf.append(_resultSetType);
184        buf.append(", resultSetConcurrency=");
185        buf.append(_resultSetConcurrency);
186        buf.append(", autoGeneratedKeys=");
187        buf.append(_autoGeneratedKeys);
188        buf.append(", statmentType=");
189        buf.append(_stmtType);
190        return buf.toString();
191    }
192}