001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (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 io.jboot.db;
017
018import com.jfinal.plugin.activerecord.Model;
019import io.jboot.db.datasource.DataSourceConfig;
020import io.jboot.utils.StrUtil;
021
022import java.util.ArrayList;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026
027/**
028 * @author Michael Yang 杨福海 (fuhai999@gmail.com)
029 * @version V1.0
030 */
031public class TableInfo {
032
033    private String tableName;
034    private String primaryKey;
035    private Class<? extends Model> modelClass;
036    private String datasource;
037    private Set<String> datasourceNames;
038
039    private List<DataSourceConfigWrapper> attachedDatasources;
040
041
042    public String getTableName() {
043        return tableName;
044    }
045
046    public void setTableName(String tableName) {
047        this.tableName = tableName;
048    }
049
050    public String getPrimaryKey() {
051        return primaryKey;
052    }
053
054    public void setPrimaryKey(String primaryKey) {
055        this.primaryKey = primaryKey;
056    }
057
058    public Class<? extends Model> getModelClass() {
059        return modelClass;
060    }
061
062    public void setModelClass(Class<? extends Model> modelClass) {
063        this.modelClass = modelClass;
064    }
065
066    public String getDatasource() {
067        return datasource;
068    }
069
070    public void setDatasource(String datasource) {
071        this.datasource = datasource;
072    }
073
074    public Set<String> getDatasourceNames() {
075        if (datasourceNames == null) {
076            datasourceNames = StrUtil.isNotBlank(datasource)
077                    ? StrUtil.splitToSetByComma(datasource)
078                    : new HashSet<>();
079        }
080        return datasourceNames;
081    }
082
083
084    /**
085     * 添加数据源:让此表绑定数据源
086     *
087     * @param dataSourceConfig
088     * @param fromDesignated   是否是通过 jboot.datasource.table 或者 @table(datasource="xxx") 来指定的
089     */
090    public boolean addAttachedDatasource(DataSourceConfig dataSourceConfig, boolean fromDesignated) {
091        if (this.attachedDatasources == null) {
092            this.attachedDatasources = new ArrayList<>();
093        }
094
095        // 只能添加到一个数据源,若 fromDesignated == false 的时候,直接返回
096        if (!fromDesignated && !this.attachedDatasources.isEmpty()) {
097            return false;
098        }
099
100
101        // 若新添加的数据源,是配置了指定的表(亦或者表配置了指定的数据源),
102        // 那么需要移除那些未指定表的默认数据源
103        if (fromDesignated && !this.attachedDatasources.isEmpty()) {
104            for (DataSourceConfigWrapper dataSourceConfigWrapper : attachedDatasources) {
105                if (!dataSourceConfigWrapper.fromDesignated) {
106                    dataSourceConfigWrapper.dataSourceConfig.removeTableInfo(this);
107                }
108            }
109            attachedDatasources.removeIf(dataSourceConfigWrapper -> !dataSourceConfigWrapper.fromDesignated);
110        }
111
112
113        // 一张表只能绑定一个数据源,jfinal 的 TableMapping 决定的,默认情况下使用该数据源去进行增删改查
114        if (attachedDatasources.isEmpty()) {
115            this.attachedDatasources.add(new DataSourceConfigWrapper(dataSourceConfig, fromDesignated));
116        }
117
118        return true;
119    }
120
121
122    public static class DataSourceConfigWrapper {
123        private final DataSourceConfig dataSourceConfig;
124        private final boolean fromDesignated;
125
126        public DataSourceConfigWrapper(DataSourceConfig dataSourceConfig, boolean fromDesignated) {
127            this.dataSourceConfig = dataSourceConfig;
128            this.fromDesignated = fromDesignated;
129        }
130    }
131
132}