forked from azuqua/cassanknex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.js
273 lines (233 loc) · 6.91 KB
/
Client.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Created by austin on 3/29/15.
*/
'use strict';
var _ = require("lodash")
, inherits = require("inherits")
, EventEmitter = require('events').EventEmitter
, componentBuilderMethods = require("./componentDeclarations/componentBuilderMethods")
, componentMethods = require("./componentDeclarations/componentCompilerMethods")
, components = require("./componentDeclarations/components");
var builderMethods = []
, compilerMethods = [];
_.each(components, function (component) {
if (componentBuilderMethods[component]) {
var bm_diff = _.difference(Object.keys(componentBuilderMethods[component]), builderMethods);
builderMethods = builderMethods.concat(bm_diff);
}
if (componentMethods[component]) {
var cm_diff = _.difference(Object.keys(componentMethods[component]), compilerMethods);
compilerMethods = compilerMethods.concat(cm_diff);
}
});
var keyspaceBuilder = null
, keyspaceCompiler = require("./schema/keyspaceCompiler")
, columnFamilyBuilder = null
, columnFamilyCompiler = require("./schema/columnFamilyCompiler")
, queryBuilder = null
, queryCompiler = require("./query/queryCompiler");
function Client(config) {
var self = this;
this._debug = config.debug ? config.debug : false;
this._awsKeyspace = config.awsKeyspace ? config.awsKeyspace : false;
if (config.connection) {
this._connectionSettings = config.connection;
}
this._dialect = "cql";
this._exec = _.has(config, "exec") ? config.exec : {};
this._execPrepare = (_.has(this._exec, "prepare") ? this._exec.prepare : true);
this._keyspace = null;
this._columnFamily = null;
this._methodStack = [];
this._queryPhases = [];
this._cql = null;
this._bindings = [];
this._statements = [];
this._single = {};
this._grouped = {};
this._executing = false;
this._isCompiling = false;
this._compileMethod = null;
}
inherits(Client, EventEmitter);
// compiler methods live at the top level
_.each(keyspaceCompiler, function (v, k) {
Client.prototype[k] = function () {
return keyspaceCompiler[k].apply(this, arguments);
};
});
_.each(columnFamilyCompiler, function (v, k) {
Client.prototype[k] = function () {
return columnFamilyCompiler[k].apply(this, arguments);
};
});
_.each(queryCompiler, function (v, k) {
Client.prototype[k] = function () {
return queryCompiler[k].apply(this, arguments);
};
});
_.each(builderMethods, function (method) {
Client.prototype[method] = function () {
var _arguments = arguments
, self = this;
if (this._component === components.keyspace) { // compiling a keyspace schema query statement
if (!keyspaceBuilder)
self._initKeyspaceBuilder();
return (function () {
keyspaceBuilder[method].apply(self, _arguments);
if (self.debug()) {
self._checkCompile();
}
return self;
})();
}
else if (this._component === components.columnFamily) { // compiling a column family schema query statement
if (!columnFamilyBuilder)
self._initColumnFamilyBuilder();
return (function () {
columnFamilyBuilder[method].apply(self, _arguments);
if (self.debug()) {
self._checkCompile();
}
return self;
})();
}
else {// assume we're going to compile a basic query
if (!queryBuilder)
self._initQueryBuilder();
return (function () {
queryBuilder[method].apply(self, _arguments);
if (self.debug()) {
self._checkCompile();
}
return self;
})();
}
};
});
// compile and return the cql statement
Client.prototype.cql = function () {
this._setExecuting(true);
this._checkCompile();
this._debugLog();
return this._cql;
};
// get the current keyspace in use
Client.prototype.keyspace = function () {
return this._keyspace;
};
// get the current debug value
Client.prototype.debug = function () {
return this._debug;
};
// get the current aws keyspaces value
Client.prototype.awsKeyspace = function () {
return this._awsKeyspace;
};
// get the current executing value
Client.prototype.executing = function () {
return this._executing;
};
// external set the keyspace (for users)
Client.prototype.use = function (keyspace) {
this._setKeyspace(keyspace);
};
// internal set the keyspace (for clarity)
Client.prototype._setKeyspace = function (keyspace) {
this._keyspace = keyspace;
};
// internal set executing (for clarity)
Client.prototype._setExecuting = function (executing) {
this._executing = executing;
};
// get the current columnFamily in use
Client.prototype.columnFamily = function () {
return this._columnFamily;
};
// get the current columnFamily in use
Client.prototype._setColumnFamily = function (columnFamily) {
this._columnFamily = columnFamily;
};
// get the current compiled bindings array
Client.prototype.bindings = function () {
return this._bindings;
};
// set the component
Client.prototype.component = function (component) {
this._component = component;
};
// set the compiling method and flag
Client.prototype.compiling = function (method) {
if (method) {
this._isCompiling = true;
this._compileMethod = method;
}
else {
this._isCompiling = false;
this._compileMethod = null;
}
};
// get the compiling options first set by a call to an interface (top level) method
Client.prototype.getCompiling = function (method, params) {
var compiling;
if (!_.has(this._grouped, "compiling")) {
compiling = {
grouping: "compiling",
type: method,
value: {}
};
_.each(params || {}, function (v, k) {
compiling.value[k] = v;
});
this._statements.push(compiling);
this.compiling(method);
}
else
compiling = this._grouped.compiling[0];
return compiling;
};
// set the method
Client.prototype.methodStack = function (method) {
if (this.debug()) {
this._methodStack.push(method);
}
};
Client.prototype.query = function (query) {
if (this.debug()) {
this._queryPhases.push(query.cql);
}
this._cql = query.cql;
};
Client.prototype._wrapMethod = function (component, method, fn, _arguments) {
if (component)
this.component(component);
if (method)
this.methodStack(method);
return fn.apply(this, _.toArray(_arguments));
};
Client.prototype._initKeyspaceBuilder = function () {
keyspaceBuilder = require("./schema/keyspaceBuilder");
};
Client.prototype._initColumnFamilyBuilder = function () {
columnFamilyBuilder = require("./schema/columnFamilyBuilder");
};
Client.prototype._initQueryBuilder = function () {
queryBuilder = require("./query/queryBuilder");
};
Client.prototype._group = function () {
this._grouped = _.groupBy(this._statements, "grouping");
};
Client.prototype._checkCompile = function () {
if (this._isCompiling) {
this._bindings.length = 0;
this._group();
this[this._compileMethod]();
}
};
// print debug of object
Client.prototype._debugLog = function () {
if (this.debug()) {
console.log(this);
}
};
module.exports = Client;