forked from dresende/node-sql-query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove.js
More file actions
78 lines (69 loc) · 1.65 KB
/
Remove.js
File metadata and controls
78 lines (69 loc) · 1.65 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
var Where = require("./Where");
exports.RemoveQuery = RemoveQuery;
function RemoveQuery(Dialect, opts) {
var sql = {
where : [],
order : []
};
return {
from: function (table) {
sql.table = table;
return this;
},
where: function () {
for (var i = 0; i < arguments.length; i++) {
sql.where.push({
t: null,
w: arguments[i]
});
}
return this;
},
build: function () {
var query = [], tmp;
query.push("DELETE FROM");
query.push(Dialect.escapeId(sql.table));
query = query.concat(Where.build(Dialect, sql.where, opts));
// order
if (sql.order.length > 0) {
tmp = [];
for (i = 0; i < sql.order.length; i++) {
if (Array.isArray(sql.order[i].c)) {
tmp.push(Dialect.escapeId.apply(Dialect, sql.order[i].c) + " " + sql.order[i].d);
} else {
tmp.push(Dialect.escapeId(sql.order[i].c) + " " + sql.order[i].d);
}
}
if (tmp.length > 0) {
query.push("ORDER BY " + tmp.join(", "));
}
}
// limit
if (sql.hasOwnProperty("limit")) {
if (sql.hasOwnProperty("offset")) {
query.push("LIMIT " + sql.limit + " OFFSET " + sql.offset);
} else {
query.push("LIMIT " + sql.limit);
}
} else if (sql.hasOwnProperty("offset")) {
query.push("OFFSET " + sql.offset);
}
return query.join(" ");
},
offset: function (offset) {
sql.offset = offset;
return this;
},
limit: function (limit) {
sql.limit = limit;
return this;
},
order: function (column, dir) {
sql.order.push({
c : Array.isArray(column) ? [ get_table_alias(column[0]), column[1] ] : column,
d : (dir == "Z" ? "DESC" : "ASC")
});
return this;
}
};
}