forked from azuqua/cassanknex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyspaceCompiler.js
140 lines (110 loc) · 3.53 KB
/
keyspaceCompiler.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
/**
* Created by austin on 3/29/15.
*/
"use strict";
var _ = require("lodash")
, formatter = require("../formatter")
, components = require("../componentDeclarations/components")
, methods = require("../componentDeclarations/componentCompilerMethods")[components.keyspace];
var component = components.keyspace;
module.exports = {
createKeyspace: function () {
arguments[1] = false;
arguments.length = 2;
return this._wrapMethod(component, "createKeyspace", _getCreateKeyspace(), arguments);
},
createKeyspaceIfNotExists: function () {
arguments[1] = true;
arguments.length = 2;
return this._wrapMethod(component, "createKeyspaceIfNotExists", _getCreateKeyspace(), arguments);
},
alterKeyspace: function () {
arguments[1] = false;
arguments.length = 2;
return this._wrapMethod(component, "alterKeyspace", _getAlterKeyspace(), arguments);
},
alterKeyspaceIfExists: function () {
arguments[1] = true;
arguments.length = 2;
return this._wrapMethod(component, "alterKeyspace", _getAlterKeyspace(), arguments);
},
dropKeyspace: function () {
arguments[1] = false;
arguments.length++;
return this._wrapMethod(component, "dropKeyspace", _getDropKeyspace(), arguments);
},
dropKeyspaceIfExists: function () {
arguments[1] = true;
arguments.length = 2;
return this._wrapMethod(component, "dropKeyspaceIfExists", _getDropKeyspace(), arguments);
},
keyspaceName: function () {
return this._keyspace;
}
};
function _getCreateKeyspace() {
return function (keyspace, ifNot) {
var compiling = this.getCompiling("createKeyspace", {keyspace: keyspace, ifNot: ifNot});
if (compiling.value.keyspace)
this.use(compiling.value.keyspace);
var createStatement = compiling.value.ifNot ? "CREATE KEYSPACE IF NOT EXISTS " : "CREATE KEYSPACE "
, cql = createStatement + formatter.wrapQuotes(this.keyspaceName());
cql += _compileStrategy(this);
cql += _compileAnd(this);
cql += ";";
this.query({
cql: cql
});
return this;
};
}
function _getAlterKeyspace() {
return function (keyspace, _if) {
var compiling = this.getCompiling("alterKeyspace", {keyspace: keyspace, if: _if});
if (compiling.value.keyspace)
this.use(compiling.value.keyspace);
var createStatement = compiling.value.if ? "ALTER KEYSPACE IF EXISTS " : "ALTER KEYSPACE "
, cql = createStatement + formatter.wrapQuotes(this.keyspaceName());
cql += _compileStrategy(this);
cql += _compileAnd(this);
cql += ";";
this.query({
cql: cql
});
return this;
};
}
function _getDropKeyspace() {
return function (keyspace, _if) {
var compiling = this.getCompiling("dropKeyspace", {keyspace: keyspace, if: _if});
if (compiling.value.keyspace)
this.use(compiling.value.keyspace);
var dropStatement = compiling.value.if ? "DROP KEYSPACE IF EXISTS " : "DROP KEYSPACE "
, cql = dropStatement + formatter.wrapQuotes(this.keyspaceName());
cql += ";";
this.query({
cql: cql
});
return this;
};
}
function _compileStrategy(client) {
var cql = "";
if (_.has(client._grouped, "strategy")) {
cql += " WITH REPLICATION = ";
_.each(client._grouped.strategy, function (strategy) {
cql += formatter.toMapString(strategy.value);
});
}
return cql;
}
function _compileAnd(client) {
var cql = "";
if (_.has(client._grouped, "and")) {
cql += " AND ";
_.each(client._grouped.and, function (and) {
cql += and.type + " = " + and.value;
});
}
return cql;
}