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.table.TableDef; 021import com.mybatisflex.core.util.StringUtil; 022 023import java.io.Serializable; 024import java.util.Objects; 025 026/** 027 * 查询列,描述的是一张表的字段 028 */ 029public class QueryTable implements Serializable { 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.name = tableDef.getTableName(); 040 this.schema = tableDef.getSchema(); 041 } 042 043 public QueryTable(String name) { 044 this.name = name; 045 } 046 047 public QueryTable(String schema, String name) { 048 this.schema = schema; 049 this.name = name; 050 } 051 052 public QueryTable(String schema, String table, String alias) { 053 this.schema = schema; 054 this.name = table; 055 this.alias = 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 075 public QueryTable as(String alias) { 076 this.alias = alias; 077 return this; 078 } 079 080 boolean isSameTable(QueryTable table) { 081 if (table == null) { 082 return false; 083 } 084 if (StringUtil.isNotBlank(alias) && StringUtil.isNotBlank(table.alias)) { 085 if (Objects.equals(alias, table.alias)) { 086 return false; 087 } 088 } 089 return Objects.equals(name, table.name); 090 } 091 092 093 Object[] getValueArray() { 094 return FlexConsts.EMPTY_ARRAY; 095 } 096 097 public String toSql(IDialect dialect) { 098 String sql; 099 if (StringUtil.isNotBlank(schema)) { 100 sql = dialect.wrap(dialect.getRealSchema(schema)) + "." + dialect.wrap(dialect.getRealTable(name)) + WrapperUtil.buildAsAlias(alias, dialect); 101 } else { 102 sql = dialect.wrap(dialect.getRealTable(name)) + WrapperUtil.buildAsAlias(alias, dialect); 103 } 104 return sql; 105 } 106 107 108 @Override 109 public String toString() { 110 return "QueryTable{" + 111 "schema='" + schema + '\'' + 112 ", name='" + name + '\'' + 113 ", alias='" + alias + '\'' + 114 '}'; 115 } 116}