content: package com.platform.admin.service.impl;
  
  import cn.hutool.core.util.ObjectUtil;
  import com.google.common.collect.Lists;
  import com.platform.admin.entity.JobDatasource;
  import com.platform.admin.service.DatasourceQueryService;
  import com.platform.admin.service.JobDatasourceService;
  import com.platform.admin.tool.query.*;
  import com.platform.admin.tool.query.BaseQueryTool;
  import com.platform.admin.tool.query.HBaseQueryTool;
  import com.platform.admin.tool.query.MongoDBQueryTool;
  import com.platform.admin.tool.query.QueryToolFactory;
  import com.platform.admin.util.JdbcConstants;
  import org.apache.commons.lang3.StringUtils;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import javax.annotation.Resource;
  import java.io.IOException;
  import java.sql.SQLException;
  import java.util.List;
  
  /**
   * datasource query
   *
   * @author AllDataDC
   * @ClassName JdbcDatasourceQueryServiceImpl * @date 2022/7/31 20:51
   */
  @Service
  public class DatasourceQueryServiceImpl implements DatasourceQueryService {
  
      @Resource
      private JobDatasourceService jobDatasourceService;
  
      @Override
      public List<String> getDBs(Long id) throws IOException {
          //获取数据源对象
          JobDatasource datasource = jobDatasourceService.getById(id);
          return new MongoDBQueryTool(datasource).getDBNames();
      }
  
  
      @Override
      public List<String> getTables(Long id, String tableSchema) throws IOException {
          //获取数据源对象
          JobDatasource datasource = jobDatasourceService.getById(id);
          //queryTool组装
          if (ObjectUtil.isNull(datasource)) {
              return Lists.newArrayList();
          }
          if (JdbcConstants.HBASE.equals(datasource.getDatasource())) {
              return new HBaseQueryTool(datasource).getTableNames();
          } else if (JdbcConstants.MONGODB.equals(datasource.getDatasource())) {
              return new MongoDBQueryTool(datasource).getCollectionNames(datasource.getDatabaseName());
          } else {
              BaseQueryTool qTool = QueryToolFactory.getByDbType(datasource);
              if(StringUtils.isBlank(tableSchema)){
                  return qTool.getTableNames();
              }else{
                  return qTool.getTableNames(tableSchema);
              }
          }
      }
  
      @Override
      public List<String> getTableSchema(Long id) {
          //获取数据源对象
          JobDatasource datasource = jobDatasourceService.getById(id);
          //queryTool组装
          if (ObjectUtil.isNull(datasource)) {
              return Lists.newArrayList();
          }
          BaseQueryTool qTool = QueryToolFactory.getByDbType(datasource);
          return qTool.getTableSchema();
      }
  
      @Override
      public List<String> getCollectionNames(long id, String dbName) throws IOException {
          //获取数据源对象
          JobDatasource datasource = jobDatasourceService.getById(id);
          //queryTool组装
          if (ObjectUtil.isNull(datasource)) {
              return Lists.newArrayList();
          }
          return new MongoDBQueryTool(datasource).getCollectionNames(dbName);
      }
  
  
      @Override
      public List<String> getColumns(Long id, String tableName) throws IOException {
          //获取数据源对象
          JobDatasource datasource = jobDatasourceService.getById(id);
          //queryTool组装
          if (ObjectUtil.isNull(datasource)) {
              return Lists.newArrayList();
          }
          if (JdbcConstants.HBASE.equals(datasource.getDatasource())) {
              return new HBaseQueryTool(datasource).getColumns(tableName);
          } else if (JdbcConstants.MONGODB.equals(datasource.getDatasource())) {
              return new MongoDBQueryTool(datasource).getColumns(tableName);
          } else {
              BaseQueryTool queryTool = QueryToolFactory.getByDbType(datasource);
              return queryTool.getColumnNames(tableName, datasource.getDatasource());
          }
      }
  
      @Override
      public List<String> getColumnsByQuerySql(Long datasourceId, String querySql) throws SQLException {
          //获取数据源对象
          JobDatasource jdbcDatasource = jobDatasourceService.getById(datasourceId);
          //queryTool组装
          if (ObjectUtil.isNull(jdbcDatasource)) {
              return Lists.newArrayList();
          }
          BaseQueryTool queryTool = QueryToolFactory.getByDbType(jdbcDatasource);
          return queryTool.getColumnsByQuerySql(querySql);
      }
  }
  