-
Notifications
You must be signed in to change notification settings - Fork 10
/
formatter.js
102 lines (92 loc) · 2.15 KB
/
formatter.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
/**
* Created by austin on 3/31/15.
*/
"use strict";
var _ = require("lodash");
/**
* Creates a CQL 'map' statement value from a given Javascript object
*
* @param obj
* @returns {string}
*/
module.exports.toMapString = function (obj) {
var out = "{"
, self = this
, values = [];
_.each(obj, function (v, k) {
if (typeof v === "object")
values.push(" '" + k + "' : '" + self.toMapString(v) + " ");
else
values.push(" '" + k + "' : '" + v + "' ");
});
out += values.join(",");
out += "}";
return out;
};
/**
* Turns an array of values into a commas delineated string of '?''s
* while simultaneous pushing the values into the clients _bindings array.
* Note that the _bindings array will ONLY be populated upon a queries execution,
* or if client.debug() returns true.
*
* @param values
* @param client
* @returns {string}
*/
module.exports.parameterize = function (values, client) {
values = _.isArray(values) ? values : [values];
if (client.debug() || client.executing()) {
_.each(values, function (value) {
client._bindings.push(value);
});
}
return _.map(values, this.parameter, this).join(", ");
};
/**
* Same as `parameterize` except that it inserts the entire value param into the bindings array
*
* @param value
* @param client
* @returns {string}
*/
module.exports.parameterizeArray = function (value, client) {
if (client.debug() || client.executing()) {
client._bindings.push(value);
}
return this.parameter();
};
/**
* Returns the CQL string value used to parametize a binding in the CQL statement
*
* @returns {string}
*/
module.exports.parameter = function () {
return "?";
};
/**
* Wrap a string w/ quotes
*
* @param string
* @returns {string}
*/
module.exports.wrapQuotes = function (string) {
return "\"" + string + "\"";
};
/**
* Wrap a string w/ single quotes
*
* @param string
* @returns {string}
*/
module.exports.wrapSingleQuotes = function (string) {
return "'" + string + "'";
};
/**
* Wrap a string w/ square brackets
*
* @param string
* @returns {string}
*/
module.exports.wrapSquareBrackets = function (string) {
return "[" + string + "]";
};