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.cpdsadapter;
018
019import java.util.Arrays;
020
021import org.apache.commons.dbcp2.PStmtKey;
022
023/**
024 * A key uniquely identifying a {@link java.sql.PreparedStatement PreparedStatement}.
025 * @since 2.0
026 */
027public class PStmtKeyCPDS extends PStmtKey {
028    private final Integer _resultSetHoldability;
029    private final int _columnIndexes[];
030    private final String _columnNames[];
031
032    public PStmtKeyCPDS(final String sql) {
033        super(sql);
034        _resultSetHoldability = null;
035        _columnIndexes = null;
036        _columnNames = null;
037    }
038
039    public PStmtKeyCPDS(final String sql, final int autoGeneratedKeys) {
040        super(sql, null, autoGeneratedKeys);
041        _resultSetHoldability = null;
042        _columnIndexes = null;
043        _columnNames = null;
044    }
045
046    public PStmtKeyCPDS(final String sql, final int resultSetType, final int resultSetConcurrency) {
047        super(sql, resultSetType, resultSetConcurrency);
048        _resultSetHoldability = null;
049        _columnIndexes = null;
050        _columnNames = null;
051    }
052
053    public PStmtKeyCPDS(final String sql, final int resultSetType, final int resultSetConcurrency,
054            final int resultSetHoldability) {
055        super(sql, resultSetType, resultSetConcurrency);
056        _resultSetHoldability = Integer.valueOf(resultSetHoldability);
057        _columnIndexes = null;
058        _columnNames = null;
059    }
060
061    public PStmtKeyCPDS(final String sql, final int columnIndexes[]) {
062        super(sql);
063        _columnIndexes = Arrays.copyOf(columnIndexes, columnIndexes.length);
064        _resultSetHoldability = null;
065        _columnNames = null;
066    }
067
068    public PStmtKeyCPDS(final String sql, final String columnNames[]) {
069        super(sql);
070        _columnNames = Arrays.copyOf(columnNames, columnNames.length);
071        _resultSetHoldability = null;
072        _columnIndexes = null;
073    }
074
075
076    @Override
077    public boolean equals(final Object obj) {
078        if (this == obj) {
079            return true;
080        }
081        if (!super.equals(obj)) {
082            return false;
083        }
084        if (getClass() != obj.getClass()) {
085            return false;
086        }
087        final PStmtKeyCPDS other = (PStmtKeyCPDS) obj;
088        if (!Arrays.equals(_columnIndexes, other._columnIndexes)) {
089            return false;
090        }
091        if (!Arrays.equals(_columnNames, other._columnNames)) {
092            return false;
093        }
094        if (_resultSetHoldability == null) {
095            if (other._resultSetHoldability != null) {
096                return false;
097            }
098        } else if (!_resultSetHoldability.equals(other._resultSetHoldability)) {
099            return false;
100        }
101        return true;
102    }
103
104
105    @Override
106    public int hashCode() {
107        final int prime = 31;
108        int result = super.hashCode();
109        result = prime * result + Arrays.hashCode(_columnIndexes);
110        result = prime * result + Arrays.hashCode(_columnNames);
111        result = prime * result + (_resultSetHoldability == null ? 0 : _resultSetHoldability.hashCode());
112        return result;
113    }
114
115
116    @Override
117    public String toString() {
118        final StringBuffer buf = new StringBuffer();
119        buf.append("PStmtKey: sql=");
120        buf.append(getSql());
121        buf.append(", catalog=");
122        buf.append(getCatalog());
123        buf.append(", resultSetType=");
124        buf.append(getResultSetType());
125        buf.append(", resultSetConcurrency=");
126        buf.append(getResultSetConcurrency());
127        buf.append(", statementType=");
128        buf.append(getStmtType());
129        buf.append(", resultSetHoldability=");
130        buf.append(_resultSetHoldability);
131        buf.append(", columnIndexes=");
132        buf.append(Arrays.toString(_columnIndexes));
133        buf.append(", columnNames=");
134        buf.append(Arrays.toString(_columnNames));
135        return buf.toString();
136    }
137}