connection.js
3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var Promise = require('bluebird'),
mysql = require('mysql'),
promiseCallback = require('./helper').promiseCallback;
/**
* Constructor
* @param {object} config - The configuration object
* @param {object} _connection - Mysql-Connection, only used internaly by the pool object
*/
var connection = function(config, _connection){
var self = this,
connect = function(resolve, reject) {
if (_connection) {
self.connection = _connection;
resolve();
} else {
self.connection = mysql.createConnection(self.config);
self.connection.connect(
function(err){
if (err) {
self.connection.err = err;
if (typeof reject === 'function') {
return reject(err);
} else {
setTimeout(function(){
connect();
}, 2000);
return;
}
} else {
delete self.connection.err;
if (typeof resolve === 'function') {
return resolve();
}
}
}
);
self.connection.on('error', function(err) {
console.log('db error', err);
self.connection.err = err;
if(err.code === 'PROTOCOL_CONNECTION_LOST' || err.code === 'ECONNRESET' || err.code === 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR') {
connect();
}
});
}
};
this.config = config;
return new Promise(function(resolve, reject) {
connect(
function(){
return resolve(self);
},
function(err){
return reject(err);
}
);
});
}
connection.prototype.query = function(sql, values) {
return promiseCallback.apply(this.connection, ['query', arguments]);
};
connection.prototype.queryStream = function(sql, values) {
return this.connection.query(sql, values);
};
connection.prototype.beginTransaction = function() {
return promiseCallback.apply(this.connection, ['beginTransaction', arguments]);
};
connection.prototype.commit = function() {
return promiseCallback.apply(this.connection, ['commit', arguments]);
};
connection.prototype.rollback = function() {
return promiseCallback.apply(this.connection, ['rollback', arguments]);
};
connection.prototype.changeUser = function(data) {
return promiseCallback.apply(this.connection, ['changeUser', arguments]);
};
connection.prototype.ping = function(data) {
return promiseCallback.apply(this.connection, ['ping', arguments]);
};
connection.prototype.statistics = function(data) {
return promiseCallback.apply(this.connection, ['statistics', arguments]);
};
connection.prototype.end = function(data) {
return promiseCallback.apply(this.connection, ['end', arguments]);
};
connection.prototype.destroy = function() {
this.connection.destroy();
};
connection.prototype.pause = function() {
this.connection.pause();
};
connection.prototype.resume = function() {
this.connection.resume();
};
connection.prototype.escape = function(value) {
return this.connection.escape(value);
};
connection.prototype.escapeId = function(value) {
return this.connection.escapeId(value);
};
connection.prototype.format = function(sql, values) {
return this.connection.format(sql, values);
};
module.exports = connection;