-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcantina-mysql.js
150 lines (131 loc) · 3.48 KB
/
cantina-mysql.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var mysql = require('mysql')
, _ = require('underscore');
module.exports = function (app) {
var conf;
// Mysql namespace.
app.mysql = {
connections: [],
_activeQueries: 0,
_completeQueries: 0
};
// Default conf.
app.conf.add({
mysql: {
user: 'root',
pool: 10
}
});
// Get conf.
conf = app.conf.get('mysql');
// Set up a simple pool of connections.
for (var i = 0; i < conf.pool; i++) {
newConnection(conf);
}
// Provide a balanced, pooled query method.
app.mysql.query = function () {
var conn, query, cb;
// Perform the query on the least busiest connection.
app.mysql.connections.sort(function (a, b) {
if (a._activeQueries === b._activeQueries) return 0;
return a._activeQueries > b._activeQueries ? 1 : -1;
});
conn = app.mysql.connections[0];
if (!conn) {
// connections in pool may have all been shut down.
// make a new one so we can continue.
newConnection(conf);
conn = app.mysql.connections[0];
}
conn._activeQueries++;
app.mysql._activeQueries++;
query = conn.query.apply(conn, arguments);
query._connection = conn;
if (!query._callback) {
query.once('end', function () {
conn._activeQueries--;
app.mysql._activeQueries--;
conn._completeQueries++;
app.mysql._completeQueries++;
});
}
else {
cb = query._callback;
query._callback = function () {
conn._activeQueries--;
app.mysql._activeQueries--;
conn._completeQueries++;
app.mysql._completeQueries++;
cb.apply(query, arguments);
};
}
return query;
};
// Create a new connection
function newConnection (config) {
var conn = mysql.createConnection(config);
conn._activeQueries = 0;
conn._completeQueries = 0;
conn.on('error', function (err) {
if (!err.fatal) {
return;
}
// Emit non-connection-related fatals on the app.
if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
err.plugin = 'mysql';
app.emit('error', err);
}
for (var idx in app.mysql.connections) {
if (app.mysql.connections[idx] === conn) {
app.mysql.connections.splice(idx, 1);
newConnection(conn.config);
break;
}
}
});
conn.connect();
app.mysql.connections.push(conn);
}
// Build select query from a nested structure.
app.mysql.build = module.exports.build;
};
// Standalone query builder.
module.exports.build = function (parts) {
var query = '';
parts = _.defaults(parts, {
select: ['*'],
from: [],
join: [],
where: [],
group: [],
order: [],
limit: null,
offset: null
});
// Cast to arrays.
['select', 'from', 'join', 'where', 'group', 'order'].forEach(function (key) {
if (!Array.isArray(parts[key])) {
parts[key] = [parts[key]];
}
});
query += 'SELECT ' + parts.select.join(', ') + '\n';
query += 'FROM ' + parts.from.join(', ') + '\n';
if (parts.join.length) {
query += parts.join.join('\n') + '\n';
}
if (parts.where.length) {
query += 'WHERE ' + parts.where.join(' AND ') + '\n';
}
if (parts.group.length) {
query += 'GROUP BY ' + parts.group.join(', ') + '\n';
}
if (parts.order.length) {
query += 'ORDER BY ' + parts.order.join(', ') + '\n';
}
if (parts.limit) {
query += 'LIMIT ' + parts.limit + '\n';
}
if (parts.offset) {
query += 'OFFSET ' + parts.offset;
}
return query;
};