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