package bean;

import com.chenyang.druid.DbType;
import com.chenyang.druid.util.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import util.getdata.DatabaseCache;
import util.sqlparse.visitor.common.names.NameWrapper;

public class DataBase implements Cloneable {
   private String ip_port;
   private String url;
   private String username;
   private String password;
   private String dbType;
   private List<Schema> schemaList = new ArrayList();
   private List<String> excludeSchema;
   private List<String> includeSchema;
   private String clickhouseJdbcUrl;
   private String clickhousePassword;
   private String redisMode;
   private String redisIp;
   private String redisNodes;
   private String redisPort;
   private String redisUsername;
   private String redisPassword;
   private String redisDatabase;
   private boolean cacheFlag;
   private Date updateTime;
   private String servername;
   private String isIgnorecase;
   public DatabaseCache simpleCache;
   public String isMatchCase;
   public boolean isRedis = false;

   public DataBase clone() throws CloneNotSupportedException {
      return (DataBase)super.clone();
   }

   public static void main(String[] args) throws InterruptedException {
      new Date();
      Thread.sleep(1000L);
      new Date();
   }

   public Date getUpdateTime() {
      return this.updateTime;
   }

   public void setUpdateTime(Date updateTime) {
      this.updateTime = updateTime;
   }

   public String getIp_port() {
      return this.ip_port;
   }

   public void setIp_port(String ip_port) {
      this.ip_port = ip_port;
   }

   public String getUrl() {
      return this.url;
   }

   public void setUrl(String url) {
      this.url = url;
   }

   public String getUsername() {
      return this.username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return this.password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public String getDbType() {
      return this.dbType;
   }

   public void setDbType(String dbType) {
      this.dbType = dbType;
   }

   public List<Schema> getSchemaList() {
      return this.schemaList;
   }

   public void setSchemaList(List<Schema> schemaList) {
      this.schemaList = schemaList;
   }

   public List<String> getExcludeSchema() {
      return this.excludeSchema;
   }

   public void setExcludeSchema(List<String> excludeSchema) {
      this.excludeSchema = excludeSchema;
   }

   public boolean isCacheFlag() {
      return this.cacheFlag;
   }

   public void setCacheFlag(boolean cacheFlag) {
      this.cacheFlag = cacheFlag;
   }

   public String getServername() {
      return this.servername;
   }

   public void setServername(String servername) {
      this.servername = servername;
   }

   public String getIsIgnorecase() {
      return this.isIgnorecase;
   }

   public void setIsIgnorecase(String isIgnorecase) {
      this.isIgnorecase = isIgnorecase;
   }

   public boolean isCaseSensitive() {
      return "1".equals(this.isIgnorecase);
   }

   public void setCaseSensitive(boolean caseSensitive) {
      this.isIgnorecase = caseSensitive ? "1" : "0";
   }

   public String getClickhouseJdbcUrl() {
      return this.clickhouseJdbcUrl;
   }

   public void setClickhouseJdbcUrl(String clickhouseJdbcUrl) {
      this.clickhouseJdbcUrl = clickhouseJdbcUrl;
   }

   public List<String> getIncludeSchema() {
      return this.includeSchema;
   }

   public void setIncludeSchema(List<String> includeSchema) {
      this.includeSchema = includeSchema;
   }

   public String getRedisIp() {
      return this.redisIp;
   }

   public void setRedisIp(String redisIp) {
      this.redisIp = redisIp;
   }

   public String getRedisPort() {
      return this.redisPort;
   }

   public void setRedisPort(String redisPort) {
      this.redisPort = redisPort;
   }

   public String getRedisUsername() {
      return this.redisUsername;
   }

   public void setRedisUsername(String redisUsername) {
      this.redisUsername = redisUsername;
   }

   public String getRedisPassword() {
      return this.redisPassword;
   }

   public void setRedisPassword(String redisPassword) {
      this.redisPassword = redisPassword;
   }

   public String getRedisDatabase() {
      return this.redisDatabase;
   }

   public void setRedisDatabase(String redisDatabase) {
      this.redisDatabase = redisDatabase;
   }

   public String getRedisMode() {
      return this.redisMode;
   }

   public void setRedisMode(String redisMode) {
      this.redisMode = redisMode;
   }

   public String getRedisNodes() {
      return this.redisNodes;
   }

   public void setRedisNodes(String redisNodes) {
      this.redisNodes = redisNodes;
   }

   public String getClickhousePassword() {
      return this.clickhousePassword;
   }

   public void setClickhousePassword(String clickhousePassword) {
      this.clickhousePassword = clickhousePassword;
   }

   public boolean isRedis() {
      return this.isRedis;
   }

   public List<String> getTableNamelistKey(String schemaName) {
      return new ArrayList();
   }

   public Table getTableFromRedis(String schemaName, String tableName) {
      return null;
   }

   public View getViewFromRedis(String schemaName, String ViewName) {
      return null;
   }

   public Synonym getSynonymFromRedis(String schemaName, String name) {
      return null;
   }

   public List<String> getSchemaNamelistKey() {
      return new ArrayList();
   }

   public List<String> getViewNamelistKey(String schemaName) {
      return new ArrayList();
   }

   public List<String> getSynonymNamelistKey(String name) {
      return new ArrayList();
   }

   public boolean containsColumn(String name, boolean isSensitive) {
      if (!this.isRedis()) {
         return this.simpleCache.containsColumn(name);
      } else {
         if (!StringUtils.isEmpty(name)) {
            String[] split = name.split("\\.");
            if (split.length >= 3) {
               int length = split.length;
               String schemaName = split[length - 3];
               String tableName = split[length - 2];
               String columnName = split[length - 1];
               NameWrapper nameWrapper = NameWrapper.create(DbType.of(this.getDbType()), this.simpleCache);
               Table table = nameWrapper.getTable(schemaName, tableName);
               if (table != null) {
                  for(Column column : table.getColumnList()) {
                     if (columnName.equalsIgnoreCase(column.getColumnName())) {
                        if (!isSensitive) {
                           return true;
                        }

                        if (columnName.equals(column.getColumnName())) {
                           return true;
                        }
                     }
                  }
               }
            }
         }

         return false;
      }
   }
}
