-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql.js
More file actions
82 lines (77 loc) · 2.37 KB
/
sql.js
File metadata and controls
82 lines (77 loc) · 2.37 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
/* ========================================================================
* DBH-PG: utilities for creating SQL chunks.
* ========================================================================
* Copyright 2014-2015 Sapienlab, Rodrigo González and Contributors.
* Licensed under MIT LICENSE (See LICENSE)
* ======================================================================== */
'use strict';
exports.orderBy = function orderBy(sort) {
if (!(sort instanceof Array)) {
sort = sort.orderBy || sort.sort;
}
if (!sort || sort.length === 0) {
return ' ';
}
var sql = ' ORDER BY ';
sort.forEach(function(sortRule) {
sql += sortRule.attr
+ ' '
+ (sortRule.asc !== undefined && !sortRule.asc ? 'DESC' : 'ASC')
+ ', ';
});
return sql.slice(0, -2) + ' ';
}
exports.limit = function limit(limit, offset) {
if (!limit) {
return ' ';
}
if (typeof limit === 'object') {
offset = limit.offset;
limit = limit.limit;
}
limit = parseInt(limit, 10);
offset = parseInt(offset, 10);
if (isNaN(limit) || limit < 0) {
return ' ';
} else if (isNaN(offset) || offset < 0) {
return ' LIMIT ' + limit + ' ';
} else {
return ' LIMIT ' + limit + ' OFFSET ' + offset + ' ';
}
}
exports.toNamed = function toNamed(object, separator, inSeparator) {
separator = separator || 'AND';
inSeparator = inSeparator || '=';
var buffer = '';
for (var key in object) {
if (object.hasOwnProperty(key)) {
buffer += ' '
+ key
+ inSeparator
+ '$'
+ key
+ ' '
+ separator;
}
}
return buffer.slice(0, - separator.length);
}
exports.toIndexed = function toIndexed(object, refArray, separator, inSeparator, startIndex) {
separator = separator || 'AND';
inSeparator = inSeparator || '=';
var index = startIndex || refArray.length + 1,
buffer = '';
for (var key in object) {
if (object.hasOwnProperty(key)) {
buffer += ' '
+ key
+ inSeparator
+ '$'
+ (index++)
+ ' '
+ separator;
refArray.push(object[key]);
}
}
return buffer.slice(0, - separator.length);
}