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    protected String schema;
035    protected String name;
036    protected String alias;
037
038    protected QueryTable() {
039    }
040
041    public QueryTable(String name) {
042        String[] schemaAndTableName = StringUtil.getSchemaAndTableName(name);
043        this.schema = schemaAndTableName[0];
044        this.name = schemaAndTableName[1];
045    }
046
047    public QueryTable(String schema, String name) {
048        this.schema = StringUtil.tryTrim(schema);
049        this.name = StringUtil.tryTrim(name);
050    }
051
052    public QueryTable(String schema, String table, String alias) {
053        this.schema = StringUtil.tryTrim(schema);
054        this.name = StringUtil.tryTrim(table);
055        this.alias = StringUtil.tryTrim(alias);
056    }
057
058    public String getSchema() {
059        return schema;
060    }
061
062    public void setSchema(String schema) {
063        this.schema = schema;
064    }
065
066    public String getName() {
067        return name;
068    }
069
070    public void setName(String name) {
071        this.name = name;
072    }
073
074    public String getNameWithSchema() {
075        return StringUtil.isNotBlank(schema) ? schema + "." + name : name;
076    }
077
078    public String getAlias() {
079        return alias;
080    }
081
082    public void setAlias(String alias) {
083        this.alias = alias;
084    }
085
086    public QueryTable as(String alias) {
087        this.alias = alias;
088        return this;
089    }
090
091    boolean isSameTable(QueryTable table) {
092        if (table == null) {
093            return false;
094        }
095        if (this == table) {
096            return true;
097        }
098        if (StringUtil.isNotBlank(alias)
099            && StringUtil.isNotBlank(table.alias)) {
100            return Objects.equals(alias, table.alias);
101        }
102        return Objects.equals(name, table.name);
103    }
104
105    Object[] getValueArray() {
106        return FlexConsts.EMPTY_ARRAY;
107    }
108
109    public String toSql(IDialect dialect, OperateType operateType) {
110        String sql;
111        if (StringUtil.isNotBlank(schema)) {
112            String table = dialect.getRealTable(name, operateType);
113            sql = dialect.wrap(dialect.getRealSchema(schema, table, operateType)) + "." + dialect.wrap(table) + WrapperUtil.buildAlias(alias, dialect);
114        } else {
115            sql = dialect.wrap(dialect.getRealTable(name, operateType)) + WrapperUtil.buildAlias(alias, dialect);
116        }
117        return sql;
118    }
119
120    @Override
121    public String toString() {
122        return "QueryTable{" + "schema='" + schema + '\'' + ", name='" + name + '\'' + ", alias='" + alias + '\'' + '}';
123    }
124
125    @Override
126    public QueryTable clone() {
127        try {
128            return (QueryTable) super.clone();
129        } catch (CloneNotSupportedException e) {
130            throw FlexExceptions.wrap(e);
131        }
132    }
133
134}