import Vue from 'vue'
import mysql from 'mysql'

Vue.prototype.$mysql = mysql
Vue.prototype.$mysql_conn = null

export default async ({ Vue }) => {
  Vue.prototype.$mysqlService = {
    checkConnection (obj) {
      const con = mysql.createConnection({
        host: obj.host,
        user: obj.username,
        password: obj.password,
        port: obj.port
      })
      return new Promise((resolve, reject) => {
        con.connect(function (err) {
          if (err) reject(err)
          resolve('连接成功!')
          con.destroy()
        })
      })
    },
    disconnect () {
      if (Vue.prototype.$mysql_conn) {
        console.log('disconnection')
        Vue.prototype.$mysql_conn.destroy()
      }
    },
    openConnectionInitDatabases (obj) {
      Vue.prototype.$mysql_conn = mysql.createConnection({
        host: obj.host,
        user: obj.username,
        password: obj.password,
        port: obj.port
      })
      return new Promise((resolve, reject) => {
        Vue.prototype.$mysql_conn.connect(function (err) {
          if (err) reject(err)
          Vue.prototype.$mysql_conn.query('show databases;', function (error, results, fields) {
            if (error) reject(err)
            resolve(results, fields)
          })
        })
      })
    },
    getTablesByDatabase (label) {
      const sql = `SELECT table_name FROM information_schema.tables WHERE table_schema = '${label}';`
      return new Promise((resolve, reject) => {
        Vue.prototype.$mysql_conn.query(sql, function (error, results) {
          if (error) reject(error)
          resolve(results)
        })
      })
    },
    getTableInfoByTable (db, table) {
      const sql = `SELECT
                C.TABLE_SCHEMA AS 'dbName',
                T.TABLE_NAME AS 'tableName',
                T.TABLE_COMMENT AS 'tableComment',
                C.COLUMN_NAME AS 'columnName',
                C.COLUMN_COMMENT AS 'columnComment',
                C.ORDINAL_POSITION AS 'ordinalPosition',
                C.COLUMN_DEFAULT AS 'columnDefault',
                C.IS_NULLABLE AS 'isNullable',
                C.DATA_TYPE AS 'dataType',
                C.CHARACTER_MAXIMUM_LENGTH AS 'characterMaximumLength',
                C.NUMERIC_PRECISION AS 'numericPrecision',
                C.NUMERIC_SCALE AS 'numericScale',
                C.COLUMN_TYPE AS 'columnType',
                C.COLUMN_KEY 'columnKey',
                C.EXTRA AS 'extra'
            FROM
                information_schema.\`TABLES\` T
            LEFT JOIN information_schema.\`COLUMNS\` C ON T.TABLE_NAME = C.TABLE_NAME
            AND T.TABLE_SCHEMA = C.TABLE_SCHEMA
            WHERE
              T.TABLE_SCHEMA = '${db}' and  T.TABLE_NAME = '${table}'
            ORDER BY
                C.TABLE_NAME,
                C.ORDINAL_POSITION;`
      console.log(sql)
      return new Promise((resolve, reject) => {
        Vue.prototype.$mysql_conn.query(sql, function (error, results) {
          if (error) reject(error)
          resolve(results)
        })
      })
    }
  }
}
