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.exception.FlexExceptions; 021import com.mybatisflex.core.table.TableDef; 022import com.mybatisflex.core.util.StringUtil; 023 024import java.util.Objects; 025 026/** 027 * 查询列,描述的是一张表的字段 028 */ 029public class QueryTable implements CloneSupport<QueryTable> { 030 031 protected int tableDefHashCode = 0; 032 protected String schema; 033 protected String name; 034 protected String alias; 035 036 public QueryTable() { 037 } 038 039 public QueryTable(TableDef tableDef) { 040 // TableDef的标识符号,0:不确定标识 041 this.tableDefHashCode = tableDef.hashCode(); 042 this.schema = tableDef.getSchema(); 043 this.name = tableDef.getTableName(); 044 } 045 046 public QueryTable(String name) { 047 String[] schemaAndTableName = StringUtil.getSchemaAndTableName(name); 048 this.schema = schemaAndTableName[0]; 049 this.name = schemaAndTableName[1]; 050 } 051 052 public QueryTable(String schema, String name) { 053 this.schema = StringUtil.tryTrim(schema); 054 this.name = StringUtil.tryTrim(name); 055 } 056 057 public QueryTable(String schema, String table, String alias) { 058 this.schema = StringUtil.tryTrim(schema); 059 this.name = StringUtil.tryTrim(table); 060 this.alias = StringUtil.tryTrim(alias); 061 } 062 063 public String getSchema() { 064 return schema; 065 } 066 067 public void setSchema(String schema) { 068 this.schema = schema; 069 } 070 071 public String getName() { 072 return name; 073 } 074 075 public void setName(String name) { 076 this.name = name; 077 } 078 079 public String getNameWithSchema() { 080 return StringUtil.isNotBlank(schema) ? schema + "." + name : name; 081 } 082 083 public String getAlias() { 084 return alias; 085 } 086 087 public void setAlias(String alias) { 088 this.alias = alias; 089 } 090 091 public QueryTable as(String alias) { 092 this.alias = alias; 093 return this; 094 } 095 096 boolean isSameTable(QueryTable table) { 097 if (table == null) { 098 return false; 099 } 100 if (StringUtil.isNotBlank(alias) && StringUtil.isNotBlank(table.alias) && (Objects.equals(alias, table.alias))) { 101 return false; 102 } 103 //比较对象都有tableDef标记,就用标记比对, 否则就用名称比对 104 if (tableDefHashCode != 0 && table.tableDefHashCode != 0) { 105 return tableDefHashCode == table.tableDefHashCode; 106 } 107 return Objects.equals(name, table.name); 108 } 109 110 Object[] getValueArray() { 111 return FlexConsts.EMPTY_ARRAY; 112 } 113 114 public String toSql(IDialect dialect) { 115 String sql; 116 if (StringUtil.isNotBlank(schema)) { 117 String table = dialect.getRealTable(name); 118 sql = dialect.wrap(dialect.getRealSchema(schema, table)) + "." + dialect.wrap(table) + WrapperUtil.buildAlias(alias, dialect); 119 } else { 120 sql = dialect.wrap(dialect.getRealTable(name)) + WrapperUtil.buildAlias(alias, dialect); 121 } 122 return sql; 123 } 124 125 @Override 126 public String toString() { 127 return "QueryTable{" + "schema='" + schema + '\'' + ", name='" + name + '\'' + ", alias='" + alias + '\'' + '}'; 128 } 129 130 @Override 131 public QueryTable clone() { 132 try { 133 return (QueryTable) super.clone(); 134 } catch (CloneNotSupportedException e) { 135 throw FlexExceptions.wrap(e); 136 } 137 } 138 139}