forked from martintajur/node-mysql-activerecord
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathquery_exec.js
More file actions
executable file
·252 lines (210 loc) · 8.62 KB
/
query_exec.js
File metadata and controls
executable file
·252 lines (210 loc) · 8.62 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const QueryBuilder = require('./query_builder.js');
const ERROR = require("../QueryExecError");
const WrapperPromise = require("../WrapperPromise");
// ****************************************************************************
// QueryBuilder "Query Execution" methods.
// ****************************************************************************
class QueryExec extends QueryBuilder {
constructor() {
super();
}
_exec(sql, cb) {
if (Object.prototype.toString.call(this._connection) === Object.prototype.toString.call({})) {
this._connection.query(sql, (err, results) => {
// Standardize some important properties
if (!err && results && !Array.isArray(results)) {
// Insert ID
if (results.hasOwnProperty('insertId')) {
results.insert_id = results.insertId || null;
}
// Affected Rows
if (results.hasOwnProperty('affectedRows')) {
results.affected_rows = results.affectedRows;
}
// Changed Rows
if (results.hasOwnProperty('changedRows')) {
results.changed_rows = results.changedRows;
}
}
cb(err, results);
});
} else {
throw ERROR.NO_CONN_OBJ_ERR;
}
}
query(sql, cb) {
if (!cb || typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
count(table, cb) {
if (typeof table === 'function' && typeof cb !== 'function') {
table = null;
cb = table;
}
const sql = this._count(table);
this.reset_query(sql);
const handler = (err, row) => {
if (!err) {
if (typeof callback !== "function") {
this.resolve(row[0].numrows);
return;
}
cb(err, row[0].numrows);
}
else {
if (typeof callback !== "function") {
this.reject(err);
return;
}
cb(err, row);
}
}
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this), handler).promisify();
this._exec(sql, handler);
}
get(table, cb, conn) {
// The table parameter is optional, it could be the cb...
if (typeof table === 'function' && typeof cb !== 'function') {
cb = table;
}
const sql = this._get(table);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
get_where(table, where, cb) {
if (typeof table !== 'string' && !Array.isArray(table)) {
throw ERROR.FIRST_PARAM_OF_GET_WHERE_ERR;
}
if (Object.prototype.toString.call(where) !== Object.prototype.toString.call({})) {
throw ERROR.SECOND_PARAM_OF_GET_WHERE_ERR;
}
const sql = this._get_where(table, where);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
insert(table, set, cb, ignore, suffix) {
const sql = this._insert(table, set, ignore, suffix);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
insert_ignore(table, set, on_dupe, cb) {
if (typeof on_dupe === 'function') {
cb = on_dupe;
on_dupe = null;
}
const sql = this._insert_ignore(table, set, on_dupe);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
insert_batch(table, set, ignore, on_dupe, cb) {
if (typeof ignore === 'function') {
cb = ignore;
ignore = null;
}
else if (typeof on_dupe === 'function') {
cb = on_dupe;
on_dupe = null;
}
const sql = this._insert_batch(table, set, ignore, on_dupe);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
update(table, set, where, cb) {
// The where parameter is optional, it could be the cb...
if (typeof where === 'function' && typeof cb !== 'function') {
cb = where;
where = null;
}
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && Object.keys(where).length === 0)) {
where = null;
}
const sql = this._update(table, set, where);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
// TODO: Write this complicated-ass function
update_batch(table, set, index, where, cb) {
// The where parameter is optional, it could be the cb...
if (typeof where === 'function' && typeof cb !== 'function') {
cb = where;
where = null;
}
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && Object.keys(where).length === 0)) {
where = null;
}
const sqls = this._update_batch(table, set, index, where);
var results = null;
const errors = [];
var _this=this;
// Execute each batch of (at least) 100
const handler = (resolve, reject) => {
(function next_batch() {
const sql = sqls.shift();
_this.reset_query(sql);
_this._exec(sql, (err, res) => {
if (!err) {
if (null === results) {
results = res;
} else {
results.affected_rows += res.affected_rows;
results.changed_rows += res.changed_rows;
}
} else {
errors.push(err);
}
if (sqls.length > 0) {
setTimeout(next_batch, 0);
} else {
if ((!cb || typeof cb !== 'function') && (typeof resolve === 'function' && typeof reject === 'function')) {
if (Array.isArray(errors) && errors.length > 0) return reject(new Error(errors.join("\n\n")));
return resolve(results);
} else if (cb && typeof cb === 'function') {
return cb(errors, results);
} else {
throw ERRORS.NO_VALID_RESULTS_HANDLER;
}
}
});
})();
};
if (!cb || cb !== 'function') {
return new Promise(handler);
} else {
handler();
}
}
delete(table, where, cb) {
if (typeof where === 'function' && typeof cb !== 'function') {
cb = where;
where = undefined;
}
if (typeof table === 'function' && typeof cb !== 'function') {
cb = table;
table = undefined;
where = undefined;
}
const sql = this._delete(table, where);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
empty_table(table, cb) {
const sql = this._empty_table(table, cb);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
truncate(table, cb) {
const sql = this._truncate(table, cb);
this.reset_query(sql);
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify();
this._exec(sql, cb);
}
}
module.exports = QueryExec;