-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectQuery.ts
More file actions
537 lines (494 loc) · 16.2 KB
/
ObjectQuery.ts
File metadata and controls
537 lines (494 loc) · 16.2 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import {Clause} from "./Clause";
import {_compareObject, _isObject, JsonObject, nbLogger} from "./Head";
/*
* オブジェクトストレージ クエリクラス
*/
export class ObjectQuery {
_clause: Clause;
_limit: number;
_skip: number;
_sort: string[];
_deleteMark: boolean;
_countQuery: boolean;
_projection: JsonObject;
/**
* @class ObjectQuery
* @classdesc オブジェクトクエリ
* @description オブジェクトクエリを生成する
* @example
* var query = new Nebula.ObjectQuery();
*/
constructor() {
this._clause = null;
this._limit = -1;
this._skip = 0;
this._sort = [];
this._deleteMark = false;
this._countQuery = false;
this._projection = null;
}
/**
* @function
* @memberOf ObjectQuery
* @description 設定されているClauseインスタンスを取得する
* @return {Clause} clause
*/
getClause(): Clause {
return this._clause;
}
/**
* @function
* @memberOf ObjectQuery
* @description 設定されている検索上限数を取得する
* @return {number} 検索上限数
*/
getLimit(): number {
return this._limit;
}
/**
* @function
* @memberOf ObjectQuery
* @description 設定されているスキップカウントを取得する
* @return {number} スキップカウント
*/
getSkipCount(): number {
return this._skip;
}
/**
* @function
* @memberOf ObjectQuery
* @description 設定されているソート順序を取得する。
* ソート順序は以下のようなキー文字列の配列。降順の場合はキーの先頭が "-" となる。
* <pre>
* ["key1", "-key2"]
* </pre>
* @return {string[]}
* @since 6.5.0
*/
getSort(): string[] {
return this._sort;
}
/**
* @function
* @memberOf ObjectQuery
* @description 設定されているソート順序を取得する。
* <p>
* Deprecated である。{@link ObjectQuery#getSort} を使用すること。
* <p>
* ソート順序は以下のように {キー名: 昇順フラグ} の配列。
* <pre>
* [{"key1": true}, {"key2": false}]
* </pre>
* @return {array} ソート順序
* @deprecated since 6.5.0
*/
getSortOrder(): Array<{[index:string]: boolean}> {
const result: Array<{[index:string]: boolean}> = [];
for (let i = 0; i < this._sort.length; i++) {
const key = this._sort[i];
const e: {[index:string]: boolean} = {};
if (key.match(/^-/)) {
e[key.substr(1)] = false;
} else {
e[key] = true;
}
result.push(e);
}
return result;
}
/**
* @function
* @memberOf ObjectQuery
* @description deleteフラグを取得する
* @private
*/
_getDeleteMark(): boolean {
return this._deleteMark;
}
/**
* @function
* @memberOf ObjectQuery
* @description 設定されているプロジェクションを取得する
* @return {Object} プロジェクション
*/
getProjection(): JsonObject {
return this._projection;
}
/**
* @function
* @memberOf ObjectQuery
* @description deleteフラグをセットする
* @private
*/
_setDeleteMark(mark: boolean) {
if (typeof mark === "boolean") {
nbLogger("ObjectQuery._setDeleteMark(), mark=" + mark + ", cur=" + this._deleteMark);
this._deleteMark = mark;
} else {
throw new Error("deleteMark must be boolean");
}
}
/**
* @function
* @memberOf ObjectQuery
* @description 内部データをクエリパラメータ Object に変換する
* @private
*/
_toParam() {
const json = this._toParamJson();
if (json.where) {
json.where = JSON.stringify(json.where);
}
if (json.projection) {
json.projection = JSON.stringify(json.projection);
}
return json;
}
/**
* @function
* @memberOf ObjectQuery
* @description 内部データを JSON Object に変換する
* @private
*/
_toParamJson() {
const json: any = {};
if (this._clause) {
json.where = this._clause.json();
}
if (this._sort.length > 0) {
json.order = this._sort.join(",");
}
json.skip = this._skip;
json.limit = this._limit;
if (this._countQuery) {
json.count = 1;
}
if (this._deleteMark) {
json.deleteMark = 1;
}
if (this._projection) {
json.projection = this._projection;
}
return json;
}
/**
* @function
* @memberOf ObjectQuery
* @description QUERY情報(JSON)をNebula.ObjectQueryインスタンスに変換する
* @private
*/
static _toObjectQuery(queryJson: any): ObjectQuery {
const query = new ObjectQuery();
nbLogger("ObjectQuery._toObjectQuery(), queryJson=" + JSON.stringify(queryJson));
if (queryJson["limit"] != null) {
query.setLimit(queryJson["limit"]);
}
if (queryJson["skip"] != null) {
query.setSkipCount(queryJson["skip"]);
}
const sort: any = queryJson["sort"];
if (sort != null) {
for (const sortKey of Object.keys(sort)) { // TODO: Object ではなく Array のはずでは?
query.setSortOrder(sortKey, sort[sortKey]);
}
}
const clause: any = queryJson["clause"];
if (clause != null && Object.keys(clause).length !== 0) {
query.setClause(new Clause(clause));
}
if (queryJson["deleteMark"] != null) {
query._setDeleteMark(queryJson["deleteMark"]);
}
if (queryJson["countQuery"] != null) {
query._setCountQuery(queryJson["countQuery"]);
}
return query;
}
/**
* @function
* @memberOf ObjectQuery
* @description NebulaObjectQueryインスタンスを比較する
* @private
*/
_equals(that: ObjectQuery): boolean {
if (this._limit !== that.getLimit()) {
return false;
}
if (this._skip !== that.getSkipCount()) {
return false;
}
if (this._deleteMark !== that._getDeleteMark()) {
return false;
}
if (this._countQuery !== that._isCountQuery()) {
return false;
}
if (this._projection) {
if (that.getProjection()) {
if (!_compareObject(this._projection, that.getProjection())) {
return false;
}
}
else {
return false;
}
} else {
if (that.getProjection()) {
return false;
}
}
if (this._clause) {
if (that.getClause()) {
if (!_compareObject(this._clause.json(), that.getClause().json())) {
return false;
}
} else {
return false;
}
} else {
if (that.getClause()) {
return false;
}
}
return _compareObject(this._sort, that.getSortOrder());
}
/**
* @memberOf ObjectQuery
* @description
* 検索条件を設定する
* @example
* var clause = Nebula.Clause.lessThan("score", 50);
* var query = new Nebula.ObjectQuery();
* query.setClause(clause);
* @param {Clause} clause Nebula.Clauseインスタンス
* <p>null を指定した場合は、すでに設定済みの検索条件をクリアする。
* <p>検索条件がすでに設定されていた場合、検索条件は上書きされる。
* デフォルトでは、null が設定されている。
* @return {ObjectQuery} this
*/
setClause(clause: Clause): ObjectQuery {
if (clause === null || clause instanceof Clause) {
this._clause = clause;
} else {
throw new Error("clause must be instanceof Nebula.Clause or null");
}
return this;
}
/**
* @memberOf ObjectQuery
* @description
* 検索上限数を設定する
* @example
* var clause = Nebula.Clause.lessThan("score", 50);
* var query = new Nebula.ObjectQuery()
* .setClause(clause)
* .setLimit(10);
* @param {number} limit 検索上限数。
* <ul>
* <li>-1 以上の値を指定できる。
* <li>-1を設定した場合は「制限なし」となる。
* ただし、サーバ側のコンフィグレーションによっては、上限値が制限されている場合がある。この場合、クエリを実行するとサーバからエラーが返る。
* <li>デフォルトでは、-1 が設定されている。
* <li>範囲外の値が設定された場合は、例外をスローする。
* </ul>
* @return {ObjectQuery} this
*/
setLimit(limit: number): ObjectQuery {
if (limit >= -1) {
this._limit = limit;
} else {
throw new Error("Invalid limit range");
}
return this;
}
/**
* @function
* @memberOf ObjectQuery
* @description スキップカウントを設定する。
* <p>
* スキップカウントは検索結果の先頭からのスキップ数を表す。
* @example
* // この例では検索結果の5件目から10件を取得する。
* var clause = Nebula.Clause.lessThan("score", 50);
* var query = new Nebula.ObjectQuery()
* .setClause(clause)
* .setLimit(10)
* .setSkipCount(5);
* @param {number} skip スキップカウント
* <p>
* skip は、0以上の値が設定可能である。
* 範囲外の値が設定された場合は、例外をスローする。
* デフォルトでは、0 が設定されている。
* @return {ObjectQuery} this
*/
setSkipCount(skip: number): ObjectQuery {
if (skip >= 0) {
this._skip = skip;
} else {
throw new Error("Invalid skip range");
}
return this;
}
/**
* @function
* @memberOf ObjectQuery
* @description ソート順序を設定する。
* @example
* var query = new Nebula.ObjectQuery()
* .setSort(["key1", "-key2"]);
* @param {string[]} sort ソートキーの配列。
* 先に指定したものが高優先となる。
* <p>それぞれデフォルトは昇順。降順の場合は先頭に "-" を付与する。
* @return {ObjectQuery} this
* @since 6.5.0
*/
setSort(sort: string[]): ObjectQuery {
this._sort = sort;
return this;
}
/**
* @function
* @memberOf ObjectQuery
* @description ソート順序を設定する。
* <p>
* 本メソッドはDeprecated である。{@link ObjectQuery#setSort} を使用すること。
* <p>
* 複数のキーを設定する場合は、本メソッドを複数呼び出す。
* 先に設定したキーのほうが優先順位が高い。
* <p>
* ソート順序をクリアするには {@link ObjectQuery#clearSortOrder} を使用する。
* @example
* // 以下の例では、score降順 -> name昇順で検索する
* var query = new Nebula.ObjectQuery()
* .setSortOrder("score", false)
* .setSortOrder("name", true);
* @param {string} key ソート対象のキー
* @param {boolean} isAsc 昇順でソートするかどうかを示す。昇順はtrue, 降順はfalse。
* @return {ObjectQuery} this
* @deprecated since 6.5.0
*/
setSortOrder(key: string, isAsc: boolean): ObjectQuery {
if (!key) {
throw new Error("No key");
}
if (typeof isAsc !== "boolean") {
throw new Error("isAsc must be boolean");
}
if (isAsc) {
this._sort.push(key);
} else {
this._sort.push("-" + key);
}
return this;
}
/**
* @function
* @memberOf ObjectQuery
* @description 設定したソート順序をクリアする。<p>
* Deprecated である。{@link ObjectQuery#setSort} を使用すること。
* @example
* var clause = Nebula.Clause.lessThan("score", 50);
* var query = new Nebula.ObjectQuery()
* .setClause(clause)
* .setLimit(10)
* .setSkipCount(5)
* .setSortOrder("name", true);
* ....
* query.clearSortOrder("name");
* @param {string} key クリアするソート対象のキー。省略時は全キーをクリアする。
* @return {ObjectQuery} this
* @deprecated since 6.5.0
*/
clearSortOrder(key: string = null): ObjectQuery {
if (!key) {
// clear all
this._sort = [];
//throw new Error("No key");
} else {
for (let i = 0; i < this._sort.length; i++) {
const k = this._sort[i];
if (k === key || k === "-" + key) {
this._sort.splice(i, 1);
break;
}
}
}
return this;
}
/**
* @function
* @memberOf ObjectQuery
* @description
* クエリ件数取得フラグを設定する.
* <br>
* クエリ件数取得フラグは、Nebula.ObjectBucket.query() を実行したときに
* クエリに合致した件数を取得するかどうかを表し、trueの場合は取得することを示す。
* <br>
* クエリ件数取得フラグのデフォルト値は、falseである。
* @example
* var bucket = ....;
* var clause = Nebula.Clause.lessThan("score", 50);
* var conditions = new Nebula.ObjectQuery();
* conditions.setClause(clause);
* conditions.setLimit(10);
* conditions._setCountQuery(true);
* ....
* conditions.query(conditions)
* .then(function(result) {
* // result.count に全件数、result.objects にオブジェクト配列が返る
* })
* .catch(function(err) {...})
* @param {Boolean} countQuery クエリ件数取得フラグ
* <br/>true または false を指定する。
* @private
*/
_setCountQuery(countQuery: boolean): ObjectQuery {
if (typeof countQuery === "boolean") {
this._countQuery = countQuery;
} else {
throw new Error("countQuery must be boolean");
}
return this;
}
/**
* @function
* @memberOf ObjectQuery
* @description
* クエリ件数取得フラグを取得する.
* <br>
* クエリ件数取得フラグのデフォルト値は、falseである。
* @return
* {Boolean} クエリ件数取得フラグ(trueまたはfalse)を返す
* @private
*/
_isCountQuery(): boolean {
return this._countQuery;
}
/**
* @function
* @memberOf ObjectQuery
* @description
* プロジェクションを指定する。
* @example
* var clause = Nebula.Clause.lessThan("score", 50);
* var projection = {"score":1};
* var query = new Nebula.ObjectQuery();
* query.setClause(clause);
* query.setProjection(projection);
* @param {Object} projectionJson
* <p>null を指定した場合は、すでに設定済みの値をクリアする。
* <p>設定済み状態で、本メソッドを呼び出した場合は上書きされる。
* デフォルトでは、null が設定されている。
* @return {ObjectQuery} this
*/
setProjection(projectionJson: JsonObject): ObjectQuery {
if (projectionJson && _isObject(projectionJson)) {
this._projection = projectionJson;
} else if (projectionJson === null) {
this._projection = null;
} else {
throw new Error("projection must be object or null");
}
return this;
}
}