2017-02-28 18:27:38 +08:00
|
|
|
/**
|
|
|
|
* mysql操作类
|
|
|
|
* @authors yutent (yutent@doui.cc)
|
|
|
|
* @date 2015-11-24 11:31:55
|
|
|
|
*
|
|
|
|
*/
|
2017-12-14 16:36:39 +08:00
|
|
|
'use strict'
|
2017-02-28 18:27:38 +08:00
|
|
|
require('es.shim')
|
2017-12-14 16:36:39 +08:00
|
|
|
const mysql = require('mysql')
|
|
|
|
const Method = require('./lib/method')
|
|
|
|
|
|
|
|
if (!Promise.defer) {
|
|
|
|
Promise.defer = function() {
|
|
|
|
let obj = {}
|
|
|
|
obj.promise = new this((yes, no) => {
|
|
|
|
obj.resolve = yes
|
|
|
|
obj.reject = no
|
|
|
|
})
|
|
|
|
return obj
|
|
|
|
}
|
2017-02-28 18:27:38 +08:00
|
|
|
}
|
2017-12-14 16:36:39 +08:00
|
|
|
class Mysqli {
|
|
|
|
/**
|
|
|
|
* [constructor 构造数据库连接池]
|
|
|
|
*/
|
|
|
|
constructor(config) {
|
|
|
|
if (!Array.isArray(config)) {
|
|
|
|
config = [config]
|
2017-02-28 18:27:38 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:36:39 +08:00
|
|
|
//是否有从库
|
|
|
|
this.useSlaveDB = config.length > 1
|
|
|
|
this.pool = mysql.createPoolCluster()
|
|
|
|
|
|
|
|
config.forEach((item, i) => {
|
|
|
|
let { host, port, user, charset, passwd: password, db: database } = item
|
|
|
|
let name = i < 1 ? 'MASTER' : 'SLAVE' + i
|
|
|
|
|
|
|
|
charset = charset || 'utf8'
|
|
|
|
let collate =
|
|
|
|
charset + (charset === 'utf8mb4' ? '_unicode_ci' : '_general_ci')
|
|
|
|
|
|
|
|
this.pool.add(name, {
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
user,
|
|
|
|
charset,
|
|
|
|
collate,
|
|
|
|
password,
|
|
|
|
database
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
//对外的escape方法
|
|
|
|
static escape(val) {
|
|
|
|
return mysql.escape(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(fromSlave = false, db) {
|
|
|
|
const defer = Promise.defer()
|
|
|
|
const slave = fromSlave && this.useSlaveDB ? 'SLAVE*' : 'MASTER'
|
|
|
|
|
|
|
|
this.pool.getConnection(slave, (err, conn) => {
|
|
|
|
if (err) {
|
|
|
|
return defer.reject(`MySQL connect ${err}`)
|
|
|
|
}
|
|
|
|
if (db) {
|
|
|
|
conn.query('USE ' + db, err => {
|
|
|
|
if (err) {
|
|
|
|
return defer.reject('Select DB ' + err)
|
|
|
|
}
|
|
|
|
defer.resolve(conn)
|
2017-02-28 18:27:38 +08:00
|
|
|
})
|
2018-02-22 00:43:10 +08:00
|
|
|
} else {
|
|
|
|
defer.resolve(conn)
|
2017-12-14 16:36:39 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return new Method(defer.promise)
|
|
|
|
}
|
2017-02-28 18:27:38 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:36:39 +08:00
|
|
|
module.exports = Mysqli
|