import { Inject, Provide } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { PgDataSourceManager } from '../manager/pgDataSourceManager';
import { MySqlDataSourceManager } from '../manager/mysqlDataSourceManager';
import { OracleDataSourceManager } from '../manager/oracleDataSourceManager';
import * as oracledb from 'oracledb';
import { SqliteDataSourceManager } from '../manager/sqliteDataSourceManager';
import { DataTableEntity } from '../entity/sys/data_table';
import { CacheManager } from '@midwayjs/cache';
import { BaseService } from '@cool-midway/core';
import * as geojson from 'geojson';

@Provide()
export class DataService extends BaseService {
  @InjectEntityModel(DataTableEntity)
  dataTableEntity: Repository<DataTableEntity>;
  @Inject()
  pgDataSourceManager: PgDataSourceManager;
  @Inject()
  oracleDataSourceManager: OracleDataSourceManager;
  @Inject()
  sqliteDataSourceManager: SqliteDataSourceManager;
  @Inject()
  mySqlDataSourceManager: MySqlDataSourceManager;
  @Inject()
  cacheManager: CacheManager;

  /**
   * 获取表字段及数据列表
   * @returns
   */
  async getList(tableName, sqlWhere, orderByField) {
    const tableField = await this.getField(tableName);
    let filed = '';
    tableField.map(e => {
      if (e.type !== 3) {
        filed += '"' + e.name + '" "' + e.name.toLowerCase() + '",';
      }
    });
    let sql = '';
    if (filed !== '') {
      filed = filed.substring(0, filed.length - 1);
      sql = 'select ' + filed + ' from ' + tableName.toLowerCase();
    } else {
      sql = 'select * from ' + tableName.toLowerCase();
    }
    sql += ' where 1=1 ';
    if (sqlWhere !== '') {
      sql += ' and ' + sqlWhere;
    }
    if (orderByField !== '') {
      sql += ' order by ' + orderByField;
    }
    console.log(sql);
    const dataTableSource = await this.getTableSource(tableName);
    let result;
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'select');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'select'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }

    return result;
  }

  async getGeoList(tableName, sqlWhere, orderByField) {
    let srid = 4527;
    const sridOldResult = await this.getOldSrid(tableName);
    const sridOld = sridOldResult[0]['st_srid'];
    let type = '';
    //如果srid是4326，则进行下一步操作，如果不是则提示“联系管理员”
    if (sridOld === 4326) {
      //对经纬度进行算法分析，得到具体的六度带，然后得出2000坐标系的srid，105E----4507，111E----4508，117E----4509，123E----4510，129E---4511
      const sridNewResult = await this.getNewSrid(tableName);
      const geoResult = geojson.parse(sridNewResult, {
        GeoJSON: 'geom',
      }).features[0]['geometry'];
      type = JSON.parse(geoResult).type.toLowerCase();
      const coordinates = JSON.parse(geoResult).coordinates[0][0][0];
      switch (Math.floor(coordinates[0] / 6)) {
        case 17: // (102E~108E)
          srid = 4507;
          break;
        case 18: // (108E~114E)
          srid = 4508;
          break;
        case 19: // (114E~120E)
          srid = 4509;
          break;
        case 20: // (120E~126E)
          srid = 4510;
          break;
        case 21: // (126E~132E)
          srid = 4511;
          break;
      }
    } else {
      console.log('请联系管理员');
    }
    const tableField = await this.getField(tableName);
    let filed = '';
    tableField.map(e => {
      if (e.type !== 3) {
        filed += '"' + e.name + '" "' + e.name.toLowerCase() + '",';
      } else {
        filed +=
          ' st_asgeojson(ST_Transform(' +
          e.name +
          ',4326)) ::json' +
          ' "' +
          e.name +
          '",';
        if (type.indexOf('polygon') != -1) {
          filed +=
            ' st_Area(ST_Transform(' +
            e.name +
            ',' +
            srid +
            ')) area,ST_perimeter(ST_Transform(' +
            e.name +
            ',' +
            srid +
            ')) perimeter,';
        } else {
          if (type.indexOf('line') != -1) {
            filed +=
              ' st_Length(ST_Transform(' + e.name + ',' + srid + ')) length,';
          }
        }
      }
    });
    let sql = '';
    if (filed !== '') {
      filed = filed.substring(0, filed.length - 1);
      sql = 'select ' + filed + ' from ' + tableName.toLowerCase();
    } else {
      sql = 'select * from ' + tableName.toLowerCase();
    }
    sql += ' where 1=1 ';
    if (sqlWhere !== '') {
      sql += ' and ' + sqlWhere;
    }
    if (orderByField !== '') {
      sql += ' order by ' + orderByField;
    }
    console.log(sql);
    const dataTableSource = await this.getTableSource(tableName);
    let result;
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'select');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'select'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }

    return result;
  }

  async getField(tableName) {
    const tableField = await this.nativeQuery(
      'select a.* from config_data_field a LEFT JOIN config_data_table b ON a."tableId" =b."id" WHERE b."name"=$1 ORDER BY a."sortCode" ASC ',
      tableName
    );
    return tableField;
  }

  async getTableSource(tableName) {
    const dataTableSource = await this.nativeQuery(
      'select a.* from config_data_source a LEFT JOIN config_data_table b ON a."id" =b."sourceId" WHERE b."name"=$1 ',
      tableName
    );
    return dataTableSource;
  }

  //用普通的sql语句，看postgis文档，https://postgis.net/docs/manual-3.3/
  // async selectGeo() {
  //   const insertsql =
  //     "insert into 'points' ('geom', 'id') values (ST_geomFromText('Point(0 0)'), '1')";
  // }

  /**
   * 新增或修改
   * @param tableName 表名
   * @param keyValue 主键id
   * @param formData  表单值
   * @returns 主键id
   */
  async addOrUpdateComm(tableName, keyValue, formData) {
    let result;
    let sql = '';
    if (keyValue !== '') {
      let filed = '';
      for (const [key, value] of Object.entries(formData)) {
        const new_key = '"' + key + '"';
        filed += new_key + "= '" + value + "',";
      }
      if (filed !== '') {
        filed = filed.substring(0, filed.length - 1);
        sql = 'update ' + tableName + ' set ' + filed + ' where id=' + keyValue;
      } else {
        result = '无字段';
      }
    } else {
      let filed = '';
      let filedValue = '';
      for (const [key, value] of Object.entries(formData)) {
        filed += '"' + key + '"' + ',';
        filedValue += "'" + value + "',";
      }
      if (filed !== '') {
        filed = filed.substring(0, filed.length - 1);
        filedValue = filedValue.substring(0, filedValue.length - 1);
        sql = 'insert into ' + tableName + '( ' + filed + ')';
      } else {
        result = '无字段';
      }
      if (formData !== '') {
        sql += ' values( ' + filedValue + ')';
      }
    }

    console.log(sql);
    const dataTableSource = await this.getTableSource(tableName);
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'add');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'add'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }

    return result;
  }

  async deleteComm(tableName, keyValue) {
    let result;
    const sql = 'delete from ' + tableName + ' where id= ' + keyValue;

    console.log(sql);
    const dataTableSource = await this.getTableSource(tableName);
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'add');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'add'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }

    return result;
  }

  /**
   * mysql
   * @returns
   */
  async getMysqlConnected(dataSource, sql) {
    return new Promise((resolve, reject) => {
      this.mySqlDataSourceManager
        .getDataSource(dataSource)
        .query(sql, (err, results, fields) => {
          // console.log(results); // results contains rows returned by server
          // console.log(fields); // fields contains extra meta data about results, if available
          // console.log(err); // fields contains extra meta data about results, if available
          if (err) reject(err + sql);
          else resolve(JSON.parse(JSON.stringify(results)));
        });
    }).then(value => {
      return value;
    });
  }

  /**
   * pgsql
   * @returns
   */
  async getPgSqlConnected(dataSource, sql, sqltype) {
    return new Promise((resolve, reject) => {
      this.pgDataSourceManager
        .getDataSource(dataSource)
        .query(sql, [], (err, response) => {
          // console.log(results); // results contains rows returned by server
          // console.log(fields); // fields contains extra meta data about results, if available
          // console.log(err); // fields contains extra meta data about results, if available
          if (err) reject(err);
          else {
            if (sqltype === 'select')
              resolve(JSON.parse(JSON.stringify(response.rows)));
            else resolve(JSON.parse(JSON.stringify(response.rowCount)));
          }
        });
    }).then(value => {
      return value;
    });
  }

  /**
   * oracle
   * @returns
   */
  async getOracleConnected(dataSource, sql) {
    let connection;
    try {
      connection = await this.oracleDataSourceManager
        .getDataSource(dataSource)
        .getConnection();
      const result = await connection.execute(sql, [], {
        outFormat: oracledb.OUT_FORMAT_OBJECT,
      });
      // return result;
      return JSON.parse(JSON.stringify(result.rows));
    } catch (err) {
      console.error(err);
    } finally {
      if (connection) {
        try {
          // Put the connection back in the pool
          await connection.close();
        } catch (err) {
          console.error(err);
        }
      }
    }
  }

  /**
   * sqlite
   * @returns
   */
  async getSqliteConnected(dataSource, sql) {
    return new Promise((resolve, reject) => {
      this.sqliteDataSourceManager
        .getDataSource(dataSource)
        .all(sql, [], (err, data) => {
          if (err) reject(err);
          else resolve(JSON.parse(JSON.stringify(data)));
        });
    }).then(value => {
      return value;
    });
  }

  async invoke() {
    await this.cacheManager.set('a', 1);
    // 缓存10秒
    await this.cacheManager.set('a', 1111, {
      ttl: 10,
    });
    console.log(await this.cacheManager.get('a'));
  }

  /**
   *
   * @param tableName 表名
   * @param geom 空间字段
   * @param coordinate 坐标信息
   * @param buffer 缓冲区，默认要传0
   * @returns 字段信息
   */
  async getSpatialList(tableName, spatialType, coordinate, buffer, sqlWhere) {
    if (buffer === '') {
      buffer = '0';
    }
    let srid = 4527;
    const sridOldResult = await this.getOldSrid(tableName);
    const sridOld = sridOldResult[0]['st_srid'];
    let type = '';
    //如果srid是4326，则进行下一步操作，如果不是则提示“联系管理员”
    if (sridOld === 4326 || sridOld === 4490) {
      //对经纬度进行算法分析，得到具体的六度带，然后得出2000坐标系的srid，105E----4507，111E----4508，117E----4509，123E----4510，129E---4511
      const sridNewResult = await this.getNewSrid(tableName);
      const geoResult = geojson.parse(sridNewResult, {
        GeoJSON: 'geom',
      }).features[0]['geometry'];
      type = JSON.parse(geoResult).type.toLowerCase();
      let coordinates;
      if (type.indexOf('polygon') != -1) {
        coordinates = JSON.parse(geoResult).coordinates[0][0][0];
      } else {
        if (type.indexOf('line') != -1) {
          coordinates = JSON.parse(geoResult).coordinates[0][0];
        } else {
          coordinates = JSON.parse(geoResult).coordinates;
        }
      }
      switch (Math.floor(coordinates[0] / 6)) {
        case 17: // (102E~108E)
          srid = 4507;
          break;
        case 18: // (108E~114E)
          srid = 4508;
          break;
        case 19: // (114E~120E)
          srid = 4509;
          break;
        case 20: // (120E~126E)
          srid = 4510;
          break;
        case 21: // (126E~132E)
          srid = 4511;
          break;
      }
    } else {
      console.log('请联系管理员');
    }
    const tableField = await this.getField(tableName);
    let geom = '';
    let filed = '';
    tableField.map(e => {
      if (e.type !== 3) {
        filed += '"' + e.name + '" "' + e.name.toLowerCase() + '",';
      } else {
        filed +=
          ' st_asgeojson(ST_Transform(' +
          e.name +
          ',4326)) ::json' +
          ' "' +
          e.name +
          '",';
        if (type.indexOf('polygon') != -1) {
          filed +=
            ' st_Area(ST_Transform(' +
            e.name +
            ',' +
            srid +
            ')) area,ST_perimeter(ST_Transform(' +
            e.name +
            ',' +
            srid +
            ')) perimeter,';
        } else {
          if (type.indexOf('line') != -1) {
            filed +=
              ' st_Length(ST_Transform(' + e.name + ',' + srid + ')) length,';
          }
        }
        geom = e.name;
      }
    });
    let sql = '';
    if (filed !== '') {
      filed = filed.substring(0, filed.length - 1);
      sql = 'select ' + filed + ' from ' + tableName.toLowerCase();
    } else {
      sql = 'select st_asgeojson(' + geom + ') from ' + tableName.toLowerCase();
    }
    sql += ' where 1=1 ';
    if (coordinate !== '' && buffer !== '') {
      if (spatialType === '点') {
        sql +=
          ' and ST_DWithin(ST_Transform(' +
          geom +
          ',' +
          srid +
          "), ST_Transform(ST_SetSRID(ST_GeomFromText('POINT(" +
          coordinate +
          ")',st_srid(" +
          geom +
          ')),4326),' +
          srid +
          '), ' +
          buffer +
          ')=true';
      } else {
        if (spatialType === '面') {
          sql +=
            ' and ST_DWithin(ST_Transform(' +
            geom +
            ',' +
            srid +
            "), ST_Transform(ST_SetSRID(ST_GeomFromText('MULTIPOLYGON(((" +
            coordinate +
            ")))',st_srid(" +
            geom +
            ')),4326),' +
            srid +
            '), ' +
            buffer +
            ')=true';
        } else {
          sql +=
            ' and ST_DWithin(ST_Transform(' +
            geom +
            ',' +
            srid +
            "), ST_Transform(ST_SetSRID(ST_GeomFromText('MULTILINESTRING((" +
            coordinate +
            "))',st_srid(" +
            geom +
            ')),4326),' +
            srid +
            '), ' +
            buffer +
            ')=true';
        }
      }
    }
    if (sqlWhere != '') {
      sql = 'select * from (' + sql + ') a where ' + sqlWhere;
    }
    console.log(sql);
    const dataTableSource = await this.getTableSource(tableName);
    let result;
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'select');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'select'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }

    return result;
  }

  async getLineInterpolatePoints(
    tableName: string,
    name: object,
    num_type: string,
    number: number
  ) {
    let srid = 4527;
    const sridOldResult = await this.getOldSrid(tableName);
    const sridOld = sridOldResult[0]['st_srid'];
    //如果srid是4326，则进行下一步操作，如果不是则提示“联系管理员”
    if (sridOld === 4326 || sridOld === 4490) {
      //对经纬度进行算法分析，得到具体的六度带，然后得出2000坐标系的srid，105E----4507，111E----4508，117E----4509，123E----4510，129E---4511
      const sridNewResult = await this.getNewSrid(tableName);
      const geoResult = geojson.parse(sridNewResult, {
        GeoJSON: 'geom',
      }).features[0]['geometry'];
      const coordinates = JSON.parse(geoResult).coordinates[0][0][0];
      switch (Math.floor(coordinates[0] / 6)) {
        case 17: // (102E~108E)
          srid = 4507;
          break;
        case 18: // (108E~114E)
          srid = 4508;
          break;
        case 19: // (114E~120E)
          srid = 4509;
          break;
        case 20: // (120E~126E)
          srid = 4510;
          break;
        case 21: // (126E~132E)
          srid = 4511;
          break;
      }
    } else {
      console.log('请联系管理员');
    }
    const key = Object.entries(name)[0][0];
    const value = Object.entries(name)[0][1];
    let sql =
      'SELECT ST_AsText(ST_LineMerge(geom)) lp,st_Length(ST_Transform(geom,' +
      srid +
      ')) length from ' +
      tableName.toLowerCase() +
      ' WHERE "' +
      key +
      '"=';
    sql += " '" + value + "'";
    let t_sql = '';
    if (num_type === '段') {
      t_sql =
        'SELECT st_asgeojson(ST_SetSRID(ST_LineInterpolatePoints(a.lp, ' +
        1 / (number - 1) +
        '),4326)) ::json rs from (' +
        sql +
        ') a';
    } else {
      t_sql =
        'SELECT st_asgeojson(ST_SetSRID(ST_LineInterpolatePoints(a.lp, ' +
        number +
        '/a.length),4326)) ::json rs from (' +
        sql +
        ') a';
    }

    return this.getResult(t_sql, tableName);
  }

  async getLineDistance(
    tableName: string,
    name: object,
    point_one: string,
    point_two: string
  ) {
    let srid = 4527;
    const sridOldResult = await this.getOldSrid(tableName);
    const sridOld = sridOldResult[0]['st_srid'];
    //如果srid是4326，则进行下一步操作，如果不是则提示“联系管理员”
    if (sridOld === 4326 || sridOld === 4490) {
      //对经纬度进行算法分析，得到具体的六度带，然后得出2000坐标系的srid，105E----4507，111E----4508，117E----4509，123E----4510，129E---4511
      const sridNewResult = await this.getNewSrid(tableName);
      const geoResult = geojson.parse(sridNewResult, {
        GeoJSON: 'geom',
      }).features[0]['geometry'];
      const coordinates = JSON.parse(geoResult).coordinates[0][0][0];
      switch (Math.floor(coordinates[0] / 6)) {
        case 17: // (102E~108E)
          srid = 4507;
          break;
        case 18: // (108E~114E)
          srid = 4508;
          break;
        case 19: // (114E~120E)
          srid = 4509;
          break;
        case 20: // (120E~126E)
          srid = 4510;
          break;
        case 21: // (126E~132E)
          srid = 4511;
          break;
      }
    } else {
      console.log('请联系管理员');
    }
    const key = Object.entries(name)[0][0];
    const value = Object.entries(name)[0][1];
    let sql =
      'SELECT ST_AsText(ST_LineMerge(geom)) lp from ' +
      tableName.toLowerCase() +
      ' WHERE "' +
      key +
      '"=';
    sql += " '" + value + "'";

    const t_sql =
      'SELECT st_asgeojson(c.geom) ::json rs,st_Length(ST_Transform(c.geom,' +
      srid +
      ")) length from (SELECT ST_SetSRID(ST_LineSubstring(b.lp,b.rsp1,b.rsp2),4326) geom from (SELECT ST_LineLocatePoint(a.lp, ST_GeomFromText('POINT(" +
      point_one +
      ")')) rsp1,ST_LineLocatePoint(a.lp, ST_GeomFromText('POINT(" +
      point_two +
      ")')) rsp2,a.lp lp from (" +
      sql +
      ') a) b) c';

    return this.getResult(t_sql, tableName);
  }

  async getResult(sql, tableName) {
    console.log(sql);
    const dataTableSource = await this.getTableSource(tableName);
    let result;
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'select');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'select'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }
    return result;
  }

  async getOldSrid(tableName) {
    //通过去表里面查询任意一条数据，获取数据的经纬度和srid
    const sql =
      'select st_srid(geom) from ' +
      tableName.toLowerCase() +
      ' order by id limit 1';
    const dataTableSource = await this.getTableSource(tableName);
    let result;
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'select');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'select'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }
    return result;
  }
  async getNewSrid(tableName) {
    //通过去表里面查询任意一条数据，获取数据的经纬度和srid
    const sql =
      'select st_asgeojson(geom) geom from ' +
      tableName.toLowerCase() +
      ' order by id limit 1';
    const dataTableSource = await this.getTableSource(tableName);
    let result;
    //本数据库源指定source为default
    if (dataTableSource.length === 0) {
      result = this.getPgSqlConnected('dataSource0', sql, 'select');
    } else {
      const dataType = dataTableSource[0].type;
      switch (dataType) {
        case 0:
          result = this.getOracleConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 1:
          result = this.getPgSqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql,
            'select'
          );
          break;
        case 2:
          result = this.getMysqlConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;
        case 3:
          result = this.getSqliteConnected(
            'dataSource' + dataTableSource[0].id,
            sql
          );
          break;

        default:
          break;
      }
    }
    return result;
  }
}
