@scx-js/scx-data 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,143 @@
1
+ import {Query} from "./Query.js";
2
+
3
+ class QueryImpl extends Query {
4
+
5
+ #where;
6
+ #groupBy;
7
+ #orderBy;
8
+ #offset;
9
+ #limit;
10
+
11
+
12
+ /**
13
+ * 创建 Query 对象
14
+ */
15
+ constructor(old) {
16
+ super();
17
+ this.#where = [];
18
+ this.#groupBy = [];
19
+ this.#orderBy = [];
20
+ this.#offset = null;
21
+ this.#limit = null;
22
+ }
23
+
24
+ where(...whereClauses) {
25
+ this.clearWhere();
26
+ this.#addWhere(...whereClauses);
27
+ return this;
28
+ }
29
+
30
+ groupBy(...groupByClauses) {
31
+ this.clearGroupBy();
32
+ this.#addGroupBy(...groupByClauses);
33
+ return this;
34
+ }
35
+
36
+
37
+ orderBy(...orderByClauses) {
38
+ this.clearOrderBy();
39
+ this.#addOrderBy(...orderByClauses);
40
+ return this;
41
+ }
42
+
43
+
44
+ offset(limitOffset) {
45
+ this.#offset = limitOffset;
46
+ return this;
47
+ }
48
+
49
+ limit(numberOfRows) {
50
+ this.#limit = numberOfRows;
51
+ return this;
52
+ }
53
+
54
+ getWhere() {
55
+ return this.#where;
56
+ }
57
+
58
+ getGroupBy() {
59
+ return this.#groupBy;
60
+ }
61
+
62
+ getOrderBy() {
63
+ return this.#orderBy;
64
+ }
65
+
66
+ getOffset() {
67
+ return this.#offset;
68
+ }
69
+
70
+ getLimit() {
71
+ return this.#limit;
72
+ }
73
+
74
+ clearWhere() {
75
+ this.#where = [];
76
+ return this;
77
+ }
78
+
79
+ clearGroupBy() {
80
+ this.#groupBy = [];
81
+ return this;
82
+ }
83
+
84
+ clearOrderBy() {
85
+ this.#orderBy = [];
86
+ return this;
87
+ }
88
+
89
+ clearOffset() {
90
+ this.#offset = null;
91
+ return this;
92
+ }
93
+
94
+ clearLimit() {
95
+ this.#limit = null;
96
+ return this;
97
+ }
98
+
99
+ #addWhere(...whereClauses) {
100
+ for (let whereClause of whereClauses) {
101
+ if (whereClause == null) {
102
+ continue;
103
+ }
104
+ if (Array.isArray(whereClause)) {
105
+ this.#addWhere(...whereClause);
106
+ continue;
107
+ }
108
+ this.#where.push(whereClause);
109
+ }
110
+ return this;
111
+ }
112
+
113
+ #addGroupBy(...groupByClauses) {
114
+ for (let groupByClause of groupByClauses) {
115
+ if (groupByClause == null) {
116
+ continue;
117
+ }
118
+ if (Array.isArray(groupByClause)) {
119
+ this.#addGroupBy(...groupByClause);
120
+ continue;
121
+ }
122
+ this.#groupBy.push(groupByClause);
123
+ }
124
+ return this;
125
+ }
126
+
127
+ #addOrderBy(...orderByClauses) {
128
+ for (let orderByClause of orderByClauses) {
129
+ if (orderByClause == null) {
130
+ continue;
131
+ }
132
+ if (Array.isArray(orderByClause)) {
133
+ this.#addOrderBy(...orderByClause);
134
+ continue;
135
+ }
136
+ this.#orderBy.push(orderByClause);
137
+ }
138
+ return this;
139
+ }
140
+
141
+ }
142
+
143
+ export {QueryImpl};
@@ -0,0 +1,89 @@
1
+ import {Query} from "./Query.js";
2
+
3
+ class QueryLike extends Query {
4
+
5
+ #query;
6
+
7
+ query() {
8
+ if (this.#query == null) {
9
+ this.#query = this.toQuery();
10
+ }
11
+ return this.#query;
12
+ }
13
+
14
+ where(...whereClauses) {
15
+ this.query().where(...whereClauses);
16
+ return this;
17
+ }
18
+
19
+ groupBy(...groupByClauses) {
20
+ this.query().groupBy(...groupByClauses);
21
+ return this;
22
+ }
23
+
24
+ orderBy(...orderByClauses) {
25
+ this.query().orderBy(...orderByClauses);
26
+ return this;
27
+ }
28
+
29
+ offset(limitOffset) {
30
+ this.query().offset(limitOffset);
31
+ return this;
32
+ }
33
+
34
+ limit(numberOfRows) {
35
+ this.query().limit(numberOfRows);
36
+ return this;
37
+ }
38
+
39
+ getWhere() {
40
+ return this.query().getWhere();
41
+ }
42
+
43
+ getGroupBy() {
44
+ return this.query().getGroupBy();
45
+ }
46
+
47
+ getOrderBy() {
48
+ return this.query().getOrderBy();
49
+ }
50
+
51
+ getOffset() {
52
+ return this.query().getOffset();
53
+ }
54
+
55
+ getLimit() {
56
+ return this.query().getLimit();
57
+ }
58
+
59
+ clearWhere() {
60
+ this.query().clearWhere();
61
+ return this;
62
+ }
63
+
64
+ clearGroupBy() {
65
+ this.query().clearGroupBy();
66
+ return this;
67
+ }
68
+
69
+ clearOrderBy() {
70
+ this.query().clearOrderBy();
71
+ return this;
72
+ }
73
+
74
+ clearOffset() {
75
+ this.query().clearOffset();
76
+ return this;
77
+ }
78
+
79
+ clearLimit() {
80
+ this.query().clearLimit();
81
+ return this;
82
+ }
83
+
84
+ toQuery() {
85
+ }
86
+
87
+ }
88
+
89
+ export {QueryLike};
@@ -0,0 +1,74 @@
1
+ class QueryOption {
2
+
3
+ #value;
4
+
5
+ constructor(value) {
6
+ this.#value = value;
7
+ }
8
+
9
+
10
+ }
11
+
12
+ const REPLACE = new QueryOption("REPLACE");
13
+ const SKIP_IF_NULL = new QueryOption("SKIP_IF_NULL");
14
+ const SKIP_IF_EMPTY_LIST = new QueryOption("SKIP_IF_EMPTY_LIST");
15
+ const USE_ORIGINAL_NAME = new QueryOption("USE_ORIGINAL_NAME");
16
+ const USE_JSON_EXTRACT = new QueryOption("USE_JSON_EXTRACT");
17
+ const USE_ORIGINAL_VALUE = new QueryOption("USE_ORIGINAL_VALUE");
18
+
19
+ function ofInfo(...queryOptions) {
20
+ let replace = false;
21
+ let skipIfNull = false;
22
+ let skipIfEmptyList = false;
23
+ let useOriginalName = false;
24
+ let useJsonExtract = false;
25
+ let useOriginalValue = false;
26
+ for (let option of queryOptions) {
27
+ if (option === REPLACE) {
28
+ replace = true;
29
+ } else if (option === SKIP_IF_NULL) {
30
+ skipIfNull = true;
31
+ } else if (option === SKIP_IF_EMPTY_LIST) {
32
+ skipIfEmptyList = true;
33
+ } else if (option === USE_ORIGINAL_NAME) {
34
+ useOriginalName = true;
35
+ } else if (option === USE_JSON_EXTRACT) {
36
+ useJsonExtract = true;
37
+ } else if (option === USE_ORIGINAL_VALUE) {
38
+ useOriginalValue = true;
39
+ }
40
+ }
41
+ return new Info(replace, skipIfNull, skipIfEmptyList, useOriginalName, useJsonExtract, useOriginalValue);
42
+ }
43
+
44
+ class Info {
45
+ replace;
46
+ skipIfNull;
47
+ skipIfEmptyList;
48
+ useOriginalName;
49
+ useJsonExtract;
50
+ useOriginalValue;
51
+
52
+
53
+ constructor(replace, skipIfNull, skipIfEmptyList, useOriginalName, useJsonExtract, useOriginalValue) {
54
+ this.replace = replace;
55
+ this.skipIfNull = skipIfNull;
56
+ this.skipIfEmptyList = skipIfEmptyList;
57
+ this.useOriginalName = useOriginalName;
58
+ this.useJsonExtract = useJsonExtract;
59
+ this.useOriginalValue = useOriginalValue;
60
+ }
61
+
62
+ }
63
+
64
+ export {
65
+ QueryOption,
66
+ REPLACE,
67
+ SKIP_IF_NULL,
68
+ SKIP_IF_EMPTY_LIST,
69
+ USE_ORIGINAL_NAME,
70
+ USE_JSON_EXTRACT,
71
+ USE_ORIGINAL_VALUE,
72
+ Info,
73
+ ofInfo,
74
+ };
package/query/Where.js ADDED
@@ -0,0 +1,59 @@
1
+ import {QueryLike} from "./QueryLike.js";
2
+ import {isBlank} from "../../scx-common/index.js";
3
+ import {QueryImpl} from "./QueryImpl.js";
4
+ import {ofInfo} from "./QueryOption.js";
5
+
6
+ class Where extends QueryLike {
7
+
8
+ #name;
9
+ #whereType;
10
+ #value1;
11
+ #value2;
12
+ #info;
13
+
14
+ constructor(name, whereType, value1, value2, ...options) {
15
+ super();
16
+ //名称不能为空
17
+ if (isBlank(name)) {
18
+ throw new Error("Where 参数错误 : 名称 不能为空 !!!");
19
+ }
20
+ //类型也不能为空
21
+ if (whereType == null) {
22
+ throw new Error("Where 参数错误 : whereType 不能为空 !!!");
23
+ }
24
+ this.#name = name;
25
+ this.#whereType = whereType;
26
+ this.#value1 = value1;
27
+ this.#value2 = value2;
28
+ this.#info = ofInfo(...options);
29
+ }
30
+
31
+ name() {
32
+ return this.#name;
33
+ }
34
+
35
+ whereType() {
36
+ return this.#whereType;
37
+ }
38
+
39
+ value1() {
40
+ return this.#value1;
41
+ }
42
+
43
+ value2() {
44
+ return this.#value2;
45
+ }
46
+
47
+ info() {
48
+ return this.#info;
49
+ }
50
+
51
+ toQuery() {
52
+ return new QueryImpl().where(this);
53
+ }
54
+
55
+ }
56
+
57
+ export {
58
+ Where,
59
+ };
@@ -0,0 +1,43 @@
1
+ import {QueryLike} from "./QueryLike.js";
2
+ import {QueryImpl} from "./QueryImpl.js";
3
+
4
+ class WhereClause extends QueryLike {
5
+
6
+ #whereClause;
7
+ #params;
8
+
9
+ constructor(whereClause, params) {
10
+ super();
11
+ this.#whereClause = whereClause;
12
+ this.#params = params;
13
+ }
14
+
15
+ /**
16
+ * 拼接
17
+ *
18
+ * @param other a
19
+ * @return WhereClause
20
+ */
21
+ concat(other) {
22
+ return new WhereClause(this.#whereClause.concat(other.#whereClause), this.#params.concat(other.params));
23
+ }
24
+
25
+ isEmpty() {
26
+ return (this.#whereClause == null || this.#whereClause.isEmpty()) && (this.#params == null || this.#params.length === 0);
27
+ }
28
+
29
+ whereClause() {
30
+ return this.#whereClause;
31
+ }
32
+
33
+ params() {
34
+ return this.#params;
35
+ }
36
+
37
+ toQuery() {
38
+ return new QueryImpl().where(this);
39
+ }
40
+
41
+ }
42
+
43
+ export {WhereClause};
@@ -0,0 +1,109 @@
1
+ /**
2
+ * 为空
3
+ */
4
+ const IS_NULL = "IS_NULL";
5
+
6
+ /**
7
+ * 不为空
8
+ */
9
+ const IS_NOT_NULL = "IS_NOT_NULL";
10
+
11
+ /**
12
+ * 等于
13
+ */
14
+ const EQUAL = "EQUAL";
15
+
16
+ /**
17
+ * 不等于
18
+ */
19
+ const NOT_EQUAL = "NOT_EQUAL";
20
+
21
+ /**
22
+ * 小于
23
+ */
24
+ const LESS_THAN = "LESS_THAN";
25
+
26
+ /**
27
+ * 小于等于
28
+ */
29
+ const LESS_THAN_OR_EQUAL = "LESS_THAN";
30
+
31
+ /**
32
+ * 大于
33
+ */
34
+ const GREATER_THAN = "GREATER_THAN";
35
+
36
+ /**
37
+ * 大于等于
38
+ */
39
+ const GREATER_THAN_OR_EQUAL = "GREATER_THAN";
40
+
41
+ /**
42
+ * Like
43
+ */
44
+ const LIKE = "LIKE";
45
+
46
+ /**
47
+ * Not Like
48
+ */
49
+ const NOT_LIKE = "NOT_LIKE";
50
+
51
+ /**
52
+ * Like 正则表达式
53
+ */
54
+ const LIKE_REGEX = "LIKE_REGEX";
55
+
56
+ /**
57
+ * Like 正则表达式
58
+ */
59
+ const NOT_LIKE_REGEX = "NOT_LIKE_REGEX";
60
+
61
+ /**
62
+ * IN
63
+ */
64
+ const IN = "IN";
65
+
66
+ /**
67
+ * NOT IN
68
+ */
69
+ const NOT_IN = "NOT_IN";
70
+
71
+ /**
72
+ * 在之间
73
+ */
74
+ const BETWEEN = "BETWEEN";
75
+
76
+ /**
77
+ * 不在之间
78
+ */
79
+ const NOT_BETWEEN = "NOT_BETWEEN";
80
+
81
+ /**
82
+ * json 包含 一般用于 数组判断
83
+ */
84
+ const JSON_CONTAINS = "JSON_CONTAINS";
85
+
86
+
87
+ const JSON_OVERLAPS = "JSON_OVERLAPS";
88
+
89
+
90
+ export {
91
+ IS_NULL,
92
+ IS_NOT_NULL,
93
+ EQUAL,
94
+ NOT_EQUAL,
95
+ LESS_THAN,
96
+ LESS_THAN_OR_EQUAL,
97
+ GREATER_THAN,
98
+ GREATER_THAN_OR_EQUAL,
99
+ LIKE,
100
+ NOT_LIKE,
101
+ LIKE_REGEX,
102
+ NOT_LIKE_REGEX,
103
+ IN,
104
+ NOT_IN,
105
+ BETWEEN,
106
+ NOT_BETWEEN,
107
+ JSON_CONTAINS,
108
+ JSON_OVERLAPS,
109
+ };
@@ -0,0 +1,50 @@
1
+ import {Query} from "../Query.js";
2
+ import {isArray} from "../../../scx-common/index.js";
3
+ import {GroupBy} from "../GroupBy.js";
4
+
5
+ class GroupBySerializer {
6
+
7
+ serialize(obj) {
8
+ if (obj instanceof String) {
9
+ return this.serializeString(obj);
10
+ }
11
+ if (obj instanceof GroupBy) {
12
+ return this.serializeGroupBy(obj);
13
+ }
14
+ if (obj instanceof Query) {
15
+ return this.serializeQuery(obj.getWhere());
16
+ }
17
+ if (isArray(obj)) {
18
+ return this.serializeAll(obj);
19
+ }
20
+ return obj;
21
+ }
22
+
23
+ serializeString(str) {
24
+ return str;
25
+ }
26
+
27
+ serializeGroupBy(w) {
28
+ return {
29
+ "@type": "GroupBy",
30
+ "name": w.name(),
31
+ "info": w.info(),
32
+ };
33
+ }
34
+
35
+ serializeQuery(w) {
36
+ return this.serializeAll(w.getGroupBy());
37
+ }
38
+
39
+
40
+ serializeAll(objs) {
41
+ const arr = [];
42
+ for (let i = 0; i < objs.length; i = i + 1) {
43
+ arr[i] = this.serialize(objs[i]);
44
+ }
45
+ return arr;
46
+ }
47
+
48
+ }
49
+
50
+ export {GroupBySerializer};
@@ -0,0 +1,54 @@
1
+ import {Query} from "../Query.js";
2
+ import {isArray} from "../../../scx-common/index.js";
3
+ import {OrderBy} from "../OrderBy.js";
4
+
5
+ class OrderBySerializer {
6
+
7
+ serialize(obj) {
8
+ if (obj instanceof String) {
9
+ return this.serializeString(obj);
10
+ }
11
+ if (obj instanceof OrderBy) {
12
+ return this.serializeOrderBy(obj);
13
+ }
14
+ if (obj instanceof Query) {
15
+ return this.serializeQuery(obj.getWhere());
16
+ }
17
+ if (isArray(obj)) {
18
+ return this.serializeAll(obj);
19
+ }
20
+ return obj;
21
+ }
22
+
23
+ serializeString(str) {
24
+ return str;
25
+ }
26
+
27
+ serializeOrderBy(w) {
28
+ return {
29
+ "@type": "OrderBy",
30
+ "name": w.name(),
31
+ "orderByType": w.orderByType(),
32
+ "info": w.info(),
33
+ };
34
+ }
35
+
36
+ serializeQuery(w) {
37
+ return this.serializeAll(w.getOrderBy());
38
+ }
39
+
40
+
41
+ serializeAll(objs) {
42
+ const arr = [];
43
+ for (let i = 0; i < objs.length; i = i + 1) {
44
+ arr[i] = this.serialize(objs[i]);
45
+ }
46
+ return arr;
47
+ }
48
+
49
+
50
+ }
51
+
52
+ export {
53
+ OrderBySerializer,
54
+ };
@@ -0,0 +1,35 @@
1
+ import {WhereSerializer} from "./WhereSerializer.js";
2
+ import {GroupBySerializer} from "./GroupBySerializer.js";
3
+ import {OrderBySerializer} from "./OrderBySerializer.js";
4
+
5
+ class QuerySerializer {
6
+
7
+ #whereSerializer;
8
+ #groupBySerializer;
9
+ #orderBySerializer;
10
+
11
+ constructor() {
12
+ this.#whereSerializer = new WhereSerializer();
13
+ this.#groupBySerializer = new GroupBySerializer();
14
+ this.#orderBySerializer = new OrderBySerializer();
15
+ }
16
+
17
+ serializeQuery(query) {
18
+ return {
19
+ "@type": "Query",
20
+ "where": this.#whereSerializer.serialize(query.getWhere()),
21
+ "groupBy": this.#groupBySerializer.serialize(query.getGroupBy()),
22
+ "orderBy": this.#orderBySerializer.serialize(query.getOrderBy()),
23
+ "offset": query.getOffset(),
24
+ "limit": query.getLimit(),
25
+ };
26
+ }
27
+
28
+ }
29
+
30
+ const QUERY_SERIALIZER = new QuerySerializer();
31
+
32
+ export {
33
+ QuerySerializer,
34
+ QUERY_SERIALIZER,
35
+ };