/*
 * Copyright (c) 2013-2025 Hutool Team and hutool.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.hutool.v7.db.meta;

import cn.hutool.v7.db.DbException;

import java.io.Serial;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 索引中的列索引信息
 *
 * @author huzhongying
 * @since 5.7.23
 */
public class ColumnIndex implements Serializable, Cloneable {
	@Serial
	private static final long serialVersionUID = 1L;

	/**
	 * 根据DatabaseMetaData#getIndexInfo获取的{@link ResultSet}构建索引列信息
	 *
	 * @param rs 结果集，通过DatabaseMetaData#getIndexInfo获取
	 * @return ColumnIndexInfo
	 */
	public static ColumnIndex of(final ResultSet rs) {
		try {
			return new ColumnIndex(
					rs.getString("COLUMN_NAME"),
					rs.getString("ASC_OR_DESC"));
		} catch (final SQLException e) {
			throw new DbException(e);
		}
	}

	/**
	 * 列名
	 */
	private String columnName;
	/**
	 * 列排序顺序，“A”: 升序，“D” : 降序，如果不支持排序顺序，可能为空
	 */
	private String ascOrDesc;

	/**
	 * 构造
	 *
	 * @param columnName 索引列名
	 * @param ascOrDesc  正序或反序，null表示无顺序表示
	 */
	public ColumnIndex(final String columnName, final String ascOrDesc) {
		this.columnName = columnName;
		this.ascOrDesc = ascOrDesc;
	}

	/**
	 * 获取索引列名
	 *
	 * @return 索引列名
	 */
	public String getColumnName() {
		return columnName;
	}

	/**
	 * 设置索引列名
	 *
	 * @param columnName 索引列名
	 * @return this
	 */
	public ColumnIndex setColumnName(final String columnName) {
		this.columnName = columnName;
		return this;
	}

	/**
	 * 获取索引列排序顺序
	 *
	 * @return 索引列排序顺序
	 */
	public String getAscOrDesc() {
		return ascOrDesc;
	}

	/**
	 * 设置索引列排序顺序
	 *
	 * @param ascOrDesc 索引列排序顺序
	 * @return this
	 */
	public ColumnIndex setAscOrDesc(final String ascOrDesc) {
		this.ascOrDesc = ascOrDesc;
		return this;
	}

	@Override
	public ColumnIndex clone() throws CloneNotSupportedException {
		return (ColumnIndex) super.clone();
	}

	@Override
	public String toString() {
		return "ColumnIndexInfo{" +
				"columnName='" + columnName + '\'' +
				", ascOrDesc='" + ascOrDesc + '\'' +
				'}';
	}
}
