001/*
002 *  Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.query;
017
018import com.mybatisflex.core.FlexConsts;
019import com.mybatisflex.core.dialect.IDialect;
020import com.mybatisflex.core.dialect.OperateType;
021import com.mybatisflex.core.exception.FlexExceptions;
022import com.mybatisflex.core.util.StringUtil;
023
024import java.util.Objects;
025
026/**
027 * 查询表。
028 *
029 * @author michael
030 * @author 王帅
031 */
032public class QueryTable implements CloneSupport<QueryTable> {
033
034
035    protected String schema;
036    protected String name;
037    protected String alias;
038
039    protected QueryTable() {
040    }
041
042    public QueryTable(String name) {
043        String[] schemaAndTableName = StringUtil.getSchemaAndTableName(name);
044        this.schema = schemaAndTableName[0];
045        this.name = schemaAndTableName[1];
046    }
047
048    public QueryTable(String schema, String name) {
049        this.schema = StringUtil.tryTrim(schema);
050        this.name = StringUtil.tryTrim(name);
051    }
052
053    public QueryTable(String schema, String table, String alias) {
054        this.schema = StringUtil.tryTrim(schema);
055        this.name = StringUtil.tryTrim(table);
056        this.alias = StringUtil.tryTrim(alias);
057    }
058
059    public String getSchema() {
060        return schema;
061    }
062
063    public void setSchema(String schema) {
064        this.schema = schema;
065    }
066
067    public String getName() {
068        return name;
069    }
070
071    public void setName(String name) {
072        this.name = name;
073    }
074
075    public String getNameWithSchema() {
076        return StringUtil.hasText(schema) ? schema + "." + name : name;
077    }
078
079    public String getAlias() {
080        return alias;
081    }
082
083    public void setAlias(String alias) {
084        this.alias = alias;
085    }
086
087    public QueryTable as(String alias) {
088        this.alias = alias;
089        return this;
090    }
091
092    boolean isSameTable(QueryTable table) {
093        if (table == null) {
094            return false;
095        }
096
097        if (this == table) {
098            return true;
099        }
100
101//        if (StringUtil.isNotBlank(alias)
102//            && StringUtil.isNotBlank(table.alias)) {
103//            return Objects.equals(alias, table.alias);
104//        }
105//
106//        return Objects.equals(name, table.name);
107
108        return Objects.equals(name, table.name)
109            && Objects.equals(alias, table.alias);
110    }
111
112    Object[] getValueArray() {
113        return FlexConsts.EMPTY_ARRAY;
114    }
115
116    public String toSql(IDialect dialect, OperateType operateType) {
117        String sql;
118        if (StringUtil.hasText(schema)) {
119            String table = dialect.getRealTable(name, operateType);
120            sql = dialect.wrap(dialect.getRealSchema(schema, table, operateType)) + "." + dialect.wrap(table) + WrapperUtil.buildAlias(alias, dialect);
121        } else {
122            sql = dialect.wrap(dialect.getRealTable(name, operateType)) + WrapperUtil.buildAlias(alias, dialect);
123        }
124        return sql;
125    }
126
127    @Override
128    public String toString() {
129        return "QueryTable{" + "schema='" + schema + '\'' + ", name='" + name + '\'' + ", alias='" + alias + '\'' + '}';
130    }
131
132    @Override
133    public QueryTable clone() {
134        try {
135            return (QueryTable) super.clone();
136        } catch (CloneNotSupportedException e) {
137            throw FlexExceptions.wrap(e);
138        }
139    }
140
141}