001/*
002 *  Copyright (c) 2022-2023, 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.isNotBlank(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        if (this == table) {
097            return true;
098        }
099        if (StringUtil.isNotBlank(alias)
100            && StringUtil.isNotBlank(table.alias)) {
101            return Objects.equals(alias, table.alias);
102        }
103        return Objects.equals(name, table.name);
104    }
105
106    Object[] getValueArray() {
107        return FlexConsts.EMPTY_ARRAY;
108    }
109
110    public String toSql(IDialect dialect, OperateType operateType) {
111        String sql;
112        if (StringUtil.isNotBlank(schema)) {
113            String table = dialect.getRealTable(name, operateType);
114            sql = dialect.wrap(dialect.getRealSchema(schema, table, operateType)) + "." + dialect.wrap(table) + WrapperUtil.buildAlias(alias, dialect);
115        } else {
116            sql = dialect.wrap(dialect.getRealTable(name, operateType)) + WrapperUtil.buildAlias(alias, dialect);
117        }
118        return sql;
119    }
120
121    @Override
122    public String toString() {
123        return "QueryTable{" + "schema='" + schema + '\'' + ", name='" + name + '\'' + ", alias='" + alias + '\'' + '}';
124    }
125
126    @Override
127    public QueryTable clone() {
128        try {
129            return (QueryTable) super.clone();
130        } catch (CloneNotSupportedException e) {
131            throw FlexExceptions.wrap(e);
132        }
133    }
134
135}