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