-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathobject-selector.js
321 lines (267 loc) · 10.9 KB
/
object-selector.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const keyMatches = {
all: 1,
none: 0,
some: -1
};
/**
* Provides utility methods to choose the correct tables and views that should be included in a statement.
* @ignore
*/
class ObjectSelector {
/**
* Gets the table/view that should be used to execute the SELECT query.
* @param {Client} client
* @param {ModelMappingInfo} info
* @param {Boolean} allPKsDefined
* @param {Array} propertiesInfo
* @param {Array} fieldsInfo
* @param {Array<Array<String>>} orderByColumns
* @return {Promise<String>} A promise that resolves to a table names.
*/
static getForSelect(client, info, allPKsDefined, propertiesInfo, fieldsInfo, orderByColumns) {
return Promise.all(
info.tables.map(t => {
if (t.isView) {
return client.metadata.getMaterializedView(info.keyspace, t.name);
}
return client.metadata.getTable(info.keyspace, t.name);
}))
.then(tables => {
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
if (table === null) {
throw new Error(`Table "${info.tables[i].name}" could not be retrieved`);
}
if (keysAreIncluded(table.partitionKeys, propertiesInfo) !== keyMatches.all) {
// Not all the partition keys are covered
continue;
}
if (allPKsDefined) {
if (keysAreIncluded(table.clusteringKeys, propertiesInfo) !== keyMatches.all) {
// All clustering keys should be included as allPKsDefined flag is set
continue;
}
}
if (propertiesInfo.length > table.partitionKeys.length) {
// Check that the Where clause is composed by partition and clustering keys
const allPropertiesArePrimaryKeys = propertiesInfo
.reduce(
(acc, p) => acc && (
contains(table.partitionKeys, c => c.name === p.columnName) ||
contains(table.clusteringKeys, c => c.name === p.columnName)
),
true);
if (!allPropertiesArePrimaryKeys) {
continue;
}
}
// All fields must be contained
const containsAllFields = fieldsInfo
.reduce((acc, p) => acc && table.columnsByName[p.columnName] !== undefined, true);
if (!containsAllFields) {
continue;
}
// CQL:
// - "ORDER BY" is currently only supported on the clustered columns of the PRIMARY KEY
// - "ORDER BY" currently only support the ordering of columns following their declared order in
// the PRIMARY KEY
//
// In the mapper, we validate that the ORDER BY columns appear in the same order as in the clustering keys
const containsAllOrderByColumns = orderByColumns
.reduce((acc, order, index) => {
if (!acc) {
return false;
}
const ck = table.clusteringKeys[index];
return ck && ck.name === order[0];
}, true);
if (!containsAllOrderByColumns) {
continue;
}
return table.name;
}
let message = `No table matches the filter (${allPKsDefined ? 'all PKs have to be specified' : 'PKs'}): [${
propertiesInfo.map(p => p.columnName)}]`;
if (fieldsInfo.length > 0) {
message += `; fields: [${fieldsInfo.map(p => p.columnName)}]`;
}
if (orderByColumns.length > 0) {
message += `; orderBy: [${orderByColumns.map(item => item[0])}]`;
}
throw new Error(message);
});
}
/** Returns the name of the first table */
static getForSelectAll(info) {
return info.tables[0].name;
}
/**
* Gets the tables that should be used to execute the INSERT query.
* @param {Client} client
* @param {ModelMappingInfo} info
* @param {Array} propertiesInfo
* @return {Promise<Array<TableMetadata>>} A promise that resolves to an Array of tables.
*/
static getForInsert(client, info, propertiesInfo) {
return Promise.all(info.tables.filter(t => !t.isView).map(t => client.metadata.getTable(info.keyspace, t.name)))
.then(tables => {
const filteredTables = tables
.filter((table, i) => {
if (table === null) {
throw new Error(`Table "${info.tables[i].name}" could not be retrieved`);
}
if (keysAreIncluded(table.partitionKeys, propertiesInfo) !== keyMatches.all) {
// Not all the partition keys are covered
return false;
}
const clusteringKeyMatches = keysAreIncluded(table.clusteringKeys, propertiesInfo);
// All clustering keys should be included or it can be inserting a static column value
if (clusteringKeyMatches === keyMatches.all) {
return true;
}
if (clusteringKeyMatches === keyMatches.some) {
return false;
}
const staticColumns = staticColumnCount(table);
return propertiesInfo.length === table.partitionKeys.length + staticColumns && staticColumns > 0;
});
if (filteredTables.length === 0) {
throw new Error(`No table matches (all PKs have to be specified) fields: [${
propertiesInfo.map(p => p.columnName)}]`);
}
return filteredTables;
});
}
/**
* Gets the tables that should be used to execute the UPDATE query.
* @param {Client} client
* @param {ModelMappingInfo} info
* @param {Array} propertiesInfo
* @param {Array} when
* @return {Promise<Array<TableMetadata>>} A promise that resolves to an Array of tables.
*/
static getForUpdate(client, info, propertiesInfo, when) {
return Promise.all(info.tables.filter(t => !t.isView).map(t => client.metadata.getTable(info.keyspace, t.name)))
.then(tables => {
const filteredTables = tables
.filter((table, i) => {
if (table === null) {
throw new Error(`Table "${info.tables[i].name}" could not be retrieved`);
}
if (keysAreIncluded(table.partitionKeys, propertiesInfo) !== keyMatches.all) {
// Not all the partition keys are covered
return false;
}
const clusteringKeyMatches = keysAreIncluded(table.clusteringKeys, propertiesInfo);
// All clustering keys should be included or it can be updating a static column value
if (clusteringKeyMatches === keyMatches.some) {
return false;
}
if (clusteringKeyMatches === keyMatches.none && !hasStaticColumn(table)) {
return false;
}
const applicableColumns = propertiesInfo
.reduce((acc, p) => acc + (table.columnsByName[p.columnName] !== undefined ? 1 : 0), 0);
if (applicableColumns <= table.partitionKeys.length + table.clusteringKeys.length) {
if (!hasStaticColumn(table) || applicableColumns <= table.partitionKeys.length) {
// UPDATE statement does not contain columns to SET
return false;
}
}
// "when" conditions should be contained in the table
return when.reduce((acc, p) => acc && table.columnsByName[p.columnName] !== undefined, true);
});
if (filteredTables.length === 0) {
let message = `No table matches (all PKs and columns to set have to be specified) fields: [${
propertiesInfo.map(p => p.columnName)}]`;
if (when.length > 0) {
message += `; condition: [${when.map(p => p.columnName)}]`;
}
throw new Error(message);
}
return filteredTables;
});
}
/**
* Gets the tables that should be used to execute the DELETE query.
* @param {Client} client
* @param {ModelMappingInfo} info
* @param {Array} propertiesInfo
* @param {Array} when
* @return {Promise<Array<TableMetadata>>} A promise that resolves to an Array of tables.
*/
static getForDelete(client, info, propertiesInfo, when) {
return Promise.all(info.tables.filter(t => !t.isView).map(t => client.metadata.getTable(info.keyspace, t.name)))
.then(tables => {
const filteredTables = tables
.filter((table, i) => {
if (table === null) {
throw new Error(`Table "${info.tables[i].name}" could not be retrieved`);
}
// All partition and clustering keys from the table should be included in the document
const keyNames = table.partitionKeys.concat(table.clusteringKeys).map(k => k.name);
const columns = propertiesInfo.map(p => p.columnName);
for (let i = 0; i < keyNames.length; i++) {
if (columns.indexOf(keyNames[i]) === -1) {
return false;
}
}
// "when" conditions should be contained in the table
return when.reduce((acc, p) => acc && table.columnsByName[p.columnName] !== undefined, true);
});
if (filteredTables.length === 0) {
let message = `No table matches (all PKs have to be specified) fields: [${
propertiesInfo.map(p => p.columnName)}]`;
if (when.length > 0) {
message += `; condition: [${when.map(p => p.columnName)}]`;
}
throw new Error(message);
}
return filteredTables;
});
}
}
function contains(arr, fn) {
return arr.filter(fn).length > 0;
}
/**
* Returns the amount of matches for a given key
* @private
* @param {Array} keys
* @param {Array} propertiesInfo
*/
function keysAreIncluded(keys, propertiesInfo) {
if (keys.length === 0) {
return keyMatches.all;
}
// Filtering by name might look slow / ineffective to using hash maps
// but we expect `keys` and `propertiesInfo` to contain only few items
const matches = propertiesInfo.reduce((acc, p) => acc + (contains(keys, k => p.columnName === k.name) ? 1 : 0), 0);
if (matches === 0) {
return keyMatches.none;
}
return matches === keys.length ? keyMatches.all : keyMatches.some;
}
function hasStaticColumn(table) {
return staticColumnCount(table) > 0;
}
function staticColumnCount(table) {
return table.columns.reduce((acc, column) => acc + (column.isStatic ? 1 : 0), 0);
}
module.exports = ObjectSelector;