@scx-js/scx-data 0.2.1 → 0.2.3

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.
package/query/Junction.js CHANGED
@@ -5,8 +5,6 @@ import {
5
5
  GT,
6
6
  GTE,
7
7
  IN,
8
- JSON_CONTAINS,
9
- JSON_OVERLAPS,
10
8
  LIKE,
11
9
  LIKE_REGEX,
12
10
  LT,
@@ -16,10 +14,11 @@ import {
16
14
  NOT_IN,
17
15
  NOT_LIKE,
18
16
  NOT_LIKE_REGEX,
19
- } from "./WhereType.js";
20
- import {Where} from "./Where.js";
21
- import {QueryImpl} from "./QueryImpl.js";
22
- import {Not} from "./Not.js";
17
+ } from "./ConditionType.js";
18
+ import {Condition} from "./Condition.js";
19
+ // import {QueryImpl} from "./QueryImpl.js";
20
+ // import {Not} from "./Not.js";
21
+ // import {WhereClause} from "./WhereClause.js";
23
22
 
24
23
  class Junction extends QueryLike {
25
24
 
@@ -54,67 +53,67 @@ class Junction extends QueryLike {
54
53
  }
55
54
 
56
55
  eq(fieldName, value, ...options) {
57
- return this.add(new Where(fieldName, EQ, value, null, ...options));
56
+ return this.add(new Condition(fieldName, EQ, value, null, ...options));
58
57
  }
59
58
 
60
59
  ne(fieldName, value, ...options) {
61
- return this.add(new Where(fieldName, NE, value, null, ...options));
60
+ return this.add(new Condition(fieldName, NE, value, null, ...options));
62
61
  }
63
62
 
64
63
  lt(fieldName, value, ...options) {
65
- return this.add(new Where(fieldName, LT, value, null, ...options));
64
+ return this.add(new Condition(fieldName, LT, value, null, ...options));
66
65
  }
67
66
 
68
67
  lte(fieldName, value, ...options) {
69
- return this.add(new Where(fieldName, LTE, value, null, ...options));
68
+ return this.add(new Condition(fieldName, LTE, value, null, ...options));
70
69
  }
71
70
 
72
71
  gt(fieldName, value, ...options) {
73
- return this.add(new Where(fieldName, GT, value, null, ...options));
72
+ return this.add(new Condition(fieldName, GT, value, null, ...options));
74
73
  }
75
74
 
76
75
  gte(fieldName, value, ...options) {
77
- return this.add(new Where(fieldName, GTE, value, null, ...options));
76
+ return this.add(new Condition(fieldName, GTE, value, null, ...options));
78
77
  }
79
78
 
80
79
  like(fieldName, value, ...options) {
81
- return this.add(new Where(fieldName, LIKE, value, null, ...options));
80
+ return this.add(new Condition(fieldName, LIKE, value, null, ...options));
82
81
  }
83
82
 
84
83
  notLike(fieldName, value, ...options) {
85
- return this.add(new Where(fieldName, NOT_LIKE, value, null, ...options));
84
+ return this.add(new Condition(fieldName, NOT_LIKE, value, null, ...options));
86
85
  }
87
86
 
88
87
  likeRegex(fieldName, value, ...options) {
89
- return this.add(new Where(fieldName, LIKE_REGEX, value, null, ...options));
88
+ return this.add(new Condition(fieldName, LIKE_REGEX, value, null, ...options));
90
89
  }
91
90
 
92
91
  notLikeRegex(fieldName, value, ...options) {
93
- return this.add(new Where(fieldName, NOT_LIKE_REGEX, value, null, ...options));
92
+ return this.add(new Condition(fieldName, NOT_LIKE_REGEX, value, null, ...options));
94
93
  }
95
94
 
96
95
  in(fieldName, value, ...options) {
97
- return this.add(new Where(fieldName, IN, value, null, ...options));
96
+ return this.add(new Condition(fieldName, IN, value, null, ...options));
98
97
  }
99
98
 
100
99
  notIn(fieldName, value, ...options) {
101
- return this.add(new Where(fieldName, NOT_IN, value, null, ...options));
100
+ return this.add(new Condition(fieldName, NOT_IN, value, null, ...options));
102
101
  }
103
102
 
104
103
  between(fieldName, value1, value2, ...options) {
105
- return this.add(new Where(fieldName, BETWEEN, value1, value2, ...options));
104
+ return this.add(new Condition(fieldName, BETWEEN, value1, value2, ...options));
106
105
  }
107
106
 
108
107
  notBetween(fieldName, value1, value2, ...options) {
109
- return this.add(new Where(fieldName, NOT_BETWEEN, value1, value2, ...options));
108
+ return this.add(new Condition(fieldName, NOT_BETWEEN, value1, value2, ...options));
110
109
  }
111
110
 
112
111
  jsonContains(fieldName, value, ...options) {
113
- return this.add(new Where(fieldName, JSON_CONTAINS, value, null, ...options));
112
+ return this.add(new Condition(fieldName, JSON_CONTAINS, value, null, ...options));
114
113
  }
115
114
 
116
115
  jsonOverlaps(fieldName, value, ...options) {
117
- return this.add(new Where(fieldName, JSON_OVERLAPS, value, null, ...options));
116
+ return this.add(new Condition(fieldName, JSON_OVERLAPS, value, null, ...options));
118
117
  }
119
118
 
120
119
  and(...clauses) {
@@ -129,6 +128,10 @@ class Junction extends QueryLike {
129
128
  return this.add(new Not(clause));
130
129
  }
131
130
 
131
+ whereClause(expression, ...params) {
132
+ return this.add(new WhereClause(expression, params));
133
+ }
134
+
132
135
  toQuery() {
133
136
  return new QueryImpl().where(this);
134
137
  }
package/query/OrderBy.js CHANGED
@@ -1,15 +1,14 @@
1
1
  import {isBlank} from "@scx-js/scx-common";
2
2
  import {QueryImpl} from "./QueryImpl.js";
3
- import {ofInfo} from "./QueryOption.js";
4
3
  import {QueryLike} from "./QueryLike.js";
5
4
 
6
5
  class OrderBy extends QueryLike {
7
6
 
8
- #name;
7
+ #selector;
9
8
  #orderByType;
10
- #info;
9
+ #useExpression;
11
10
 
12
- constructor(name, orderByType, ...options) {
11
+ constructor(name, orderByType, useExpression) {
13
12
  super();
14
13
  if (isBlank(name)) {
15
14
  throw new Error("OrderBy 参数错误 : 名称 不能为空 !!!");
@@ -17,22 +16,22 @@ class OrderBy extends QueryLike {
17
16
  if (orderByType == null) {
18
17
  throw new Error("OrderBy 参数错误 : orderByType 不能为空 !!!");
19
18
  }
20
- this.#name = name;
19
+ this.#selector = name;
21
20
  this.#orderByType = orderByType;
22
- this.#info = ofInfo(...options);
21
+ this.#useExpression = useExpression;
23
22
  }
24
23
 
25
24
 
26
- name() {
27
- return this.#name;
25
+ selector() {
26
+ return this.#selector;
28
27
  }
29
28
 
30
29
  orderByType() {
31
30
  return this.#orderByType;
32
31
  }
33
32
 
34
- info() {
35
- return this.#info;
33
+ useExpression() {
34
+ return this.#useExpression;
36
35
  }
37
36
 
38
37
  toQuery() {
package/query/Query.js CHANGED
@@ -4,29 +4,22 @@ class Query {
4
4
  return this;
5
5
  }
6
6
 
7
- groupBy(...groupByClauses) {
7
+ orderBys(...orderBys) {
8
8
  return this;
9
9
  }
10
10
 
11
- orderBy(...orderByClauses) {
11
+ offset(offset) {
12
12
  return this;
13
13
  }
14
14
 
15
- offset(limitOffset) {
16
- return this;
17
- }
18
-
19
- limit(numberOfRows) {
15
+ limit(limit) {
20
16
  return this;
21
17
  }
22
18
 
23
19
  getWhere() {
24
20
  }
25
21
 
26
- getGroupBy() {
27
- }
28
-
29
- getOrderBy() {
22
+ getOrderBys() {
30
23
  }
31
24
 
32
25
  getOffset() {
@@ -39,11 +32,7 @@ class Query {
39
32
  return this;
40
33
  }
41
34
 
42
- clearGroupBy() {
43
- return this;
44
- }
45
-
46
- clearOrderBy() {
35
+ clearOrderBys() {
47
36
  return this;
48
37
  }
49
38
 
@@ -55,19 +44,15 @@ class Query {
55
44
  return this;
56
45
  }
57
46
 
58
- addGroupBy(...groupByClauses) {
59
- return this;
60
- }
61
-
62
- addOrderBy(...orderByClauses) {
47
+ orderBy(...orderBys) {
63
48
  return this;
64
49
  }
65
50
 
66
- removeGroupByIf(filter) {
51
+ asc(selector, ...controls) {
67
52
  return this;
68
53
  }
69
54
 
70
- removeOrderByIf(filter) {
55
+ desc(selector, ...controls) {
71
56
  return this;
72
57
  }
73
58
 
@@ -5,8 +5,6 @@ import {
5
5
  GT,
6
6
  GTE,
7
7
  IN,
8
- JSON_CONTAINS,
9
- JSON_OVERLAPS,
10
8
  LIKE,
11
9
  LIKE_REGEX,
12
10
  LT,
@@ -16,14 +14,16 @@ import {
16
14
  NOT_IN,
17
15
  NOT_LIKE,
18
16
  NOT_LIKE_REGEX,
19
- } from "./WhereType.js";
20
- import {Where} from "./Where.js";
17
+ } from "./ConditionType.js";
18
+ import {Condition} from "./Condition.js";
21
19
  import {QueryImpl} from "./QueryImpl.js";
22
20
  import {OrderBy} from "./OrderBy.js";
23
21
  import {ASC, DESC} from "./OrderByType.js";
24
22
  import {Not} from "./Not.js";
25
23
  import {And} from "./And.js";
26
24
  import {Or} from "./Or.js";
25
+ import {checkUseExpression, checkUseExpressionValue} from "./BuildControl.js";
26
+ import {ofSkipIfInfo} from "./SkipIfInfo.js";
27
27
 
28
28
  function query(oldQuery) {
29
29
  return new QueryImpl(oldQuery);
@@ -33,20 +33,16 @@ function where(whereClauses) {
33
33
  return new QueryImpl().where(whereClauses);
34
34
  }
35
35
 
36
- function groupBy(...groupByClauses) {
37
- return new QueryImpl().groupBy(...groupByClauses);
36
+ function orderBys(...orderBys) {
37
+ return new QueryImpl().orderBys(...orderBys);
38
38
  }
39
39
 
40
- function orderBy(...orderByClauses) {
41
- return new QueryImpl().orderBy(...orderByClauses);
40
+ function offset(offset) {
41
+ return new QueryImpl().offset(offset);
42
42
  }
43
43
 
44
- function offset(limitOffset) {
45
- return new QueryImpl().offset(limitOffset);
46
- }
47
-
48
- function limit(numberOfRows) {
49
- return new QueryImpl().limit(numberOfRows);
44
+ function limit(limit) {
45
+ return new QueryImpl().limit(limit);
50
46
  }
51
47
 
52
48
  function and(...clauses) {
@@ -61,87 +57,97 @@ function not(clause) {
61
57
  return new Not(clause);
62
58
  }
63
59
 
60
+ function condition(fieldName, conditionType, value, ...controls) {
61
+ let useExpression = checkUseExpression(...controls);
62
+ let useExpressionValue = checkUseExpressionValue(...controls);
63
+ let skipIfInfo = ofSkipIfInfo(...controls);
64
+ return new Condition(fieldName, conditionType, value, null, useExpression, useExpressionValue, skipIfInfo);
65
+ }
66
+
67
+ function condition2(fieldName, conditionType, value1, value2, ...controls) {
68
+ let useExpression = checkUseExpression(...controls);
69
+ let useExpressionValue = checkUseExpressionValue(...controls);
70
+ let skipIfInfo = ofSkipIfInfo(...controls);
71
+ return new Condition(fieldName, conditionType, value1, value2, useExpression, useExpressionValue, skipIfInfo);
72
+ }
73
+
74
+ function orderBy(selector, orderByType, ...controls) {
75
+ let useExpression = checkUseExpression(controls);
76
+ return new OrderBy(selector, orderByType, useExpression);
77
+ }
78
+
79
+ function whereClause(expression, ...params) {
80
+ return new WhereClause(expression, params);
81
+ }
82
+
64
83
  function asc(name, ...options) {
65
- return new OrderBy(name, ASC, ...options);
84
+ return orderBy(name, ASC, ...options);
66
85
  }
67
86
 
68
87
  function desc(name, ...options) {
69
- return new OrderBy(name, DESC, ...options);
88
+ return orderBy(name, DESC, ...options);
70
89
  }
71
90
 
72
91
  function eq(fieldName, value, ...options) {
73
- return new Where(fieldName, EQ, value, null, ...options);
92
+ return condition(fieldName, EQ, value, ...options);
74
93
  }
75
94
 
76
95
  function ne(fieldName, value, ...options) {
77
- return new Where(fieldName, NE, value, null, ...options);
96
+ return condition(fieldName, NE, value, ...options);
78
97
  }
79
98
 
80
99
  function lt(fieldName, value, ...options) {
81
- return new Where(fieldName, LT, value, null, ...options);
100
+ return condition(fieldName, LT, value, ...options);
82
101
  }
83
102
 
84
103
  function lte(fieldName, value, ...options) {
85
- return new Where(fieldName, LTE, value, null, ...options);
104
+ return condition(fieldName, LTE, value, ...options);
86
105
  }
87
106
 
88
107
  function gt(fieldName, value, ...options) {
89
- return new Where(fieldName, GT, value, null, ...options);
108
+ return condition(fieldName, GT, value, ...options);
90
109
  }
91
110
 
92
111
  function gte(fieldName, value, ...options) {
93
- return new Where(fieldName, GTE, value, null, ...options);
112
+ return condition(fieldName, GTE, value, ...options);
94
113
  }
95
114
 
96
115
  function like(fieldName, value, ...options) {
97
- return new Where(fieldName, LIKE, value, null, ...options);
116
+ return condition(fieldName, LIKE, value, ...options);
98
117
  }
99
118
 
100
119
  function notLike(fieldName, value, ...options) {
101
- return new Where(fieldName, NOT_LIKE, value, null, ...options);
120
+ return condition(fieldName, NOT_LIKE, value, ...options);
102
121
  }
103
122
 
104
123
  function likeRegex(fieldName, value, ...options) {
105
- return new Where(fieldName, LIKE_REGEX, value, null, ...options);
124
+ return condition(fieldName, LIKE_REGEX, value, ...options);
106
125
  }
107
126
 
108
127
  function notLikeRegex(fieldName, value, ...options) {
109
- return new Where(fieldName, NOT_LIKE_REGEX, value, null, ...options);
128
+ return condition(fieldName, NOT_LIKE_REGEX, value, ...options);
110
129
  }
111
130
 
112
131
  function in_(fieldName, value, ...options) {
113
- return new Where(fieldName, IN, value, null, ...options);
132
+ return condition(fieldName, IN, value, ...options);
114
133
  }
115
134
 
116
135
  function notIn(fieldName, value, ...options) {
117
- return new Where(fieldName, NOT_IN, value, null, ...options);
136
+ return condition(fieldName, NOT_IN, value, ...options);
118
137
  }
119
138
 
120
139
  function between(fieldName, value1, value2, ...options) {
121
- return new Where(fieldName, BETWEEN, value1, value2, ...options);
140
+ return condition2(fieldName, BETWEEN, value1, value2, ...options);
122
141
  }
123
142
 
124
143
  function notBetween(fieldName, value1, value2, ...options) {
125
- return new Where(fieldName, NOT_BETWEEN, value1, value2, ...options);
126
- }
127
-
128
- function jsonContains(fieldName, value, ...options) {
129
- return new Where(fieldName, JSON_CONTAINS, value, null, ...options);
130
- }
131
-
132
- function jsonOverlaps(fieldName, value, ...options) {
133
- return new Where(fieldName, JSON_OVERLAPS, value, null, ...options);
134
- }
135
-
136
- function whereClause(whereClause, params) {
137
- return new WhereClause(whereClause, params);
144
+ return condition2(fieldName, NOT_BETWEEN, value1, value2, ...options);
138
145
  }
139
146
 
140
147
 
141
148
  export {
142
149
  query,
143
150
  where,
144
- groupBy,
145
151
  orderBy,
146
152
  offset,
147
153
  limit,
@@ -164,6 +170,5 @@ export {
164
170
  notIn,
165
171
  between,
166
172
  notBetween,
167
- jsonContains,
168
173
  whereClause,
169
174
  };
@@ -1,23 +1,22 @@
1
1
  import {Query} from "./Query.js";
2
- import {removeIf} from "@scx-js/scx-common";
2
+ import {checkUseExpression} from "./BuildControl.js";
3
+ import {OrderBy} from "./OrderBy.js";
4
+ import {ASC, DESC} from "./OrderByType.js";
3
5
 
4
6
  class QueryImpl extends Query {
5
7
 
6
8
  #where;
7
- #groupBy;
8
- #orderBy;
9
+ #orderBys;
9
10
  #offset;
10
11
  #limit;
11
12
 
12
-
13
13
  /**
14
14
  * 创建 Query 对象
15
15
  */
16
16
  constructor(old) {
17
17
  super();
18
18
  this.#where = null;
19
- this.#groupBy = [];
20
- this.#orderBy = [];
19
+ this.#orderBys = [];
21
20
  this.#offset = null;
22
21
  this.#limit = null;
23
22
  }
@@ -27,27 +26,26 @@ class QueryImpl extends Query {
27
26
  return this;
28
27
  }
29
28
 
30
- groupBy(...groupByClauses) {
31
- this.clearGroupBy();
32
- this.addGroupBy(...groupByClauses);
33
- return this;
34
- }
35
29
 
36
-
37
- orderBy(...orderByClauses) {
38
- this.clearOrderBy();
39
- this.addOrderBy(...orderByClauses);
30
+ orderBys(...orderBys) {
31
+ this.#orderBys = [...orderBys];
40
32
  return this;
41
33
  }
42
34
 
43
35
 
44
- offset(limitOffset) {
45
- this.#offset = limitOffset;
36
+ offset(offset) {
37
+ if (offset < 0) {
38
+ throw new Error("Limit 参数错误 : offset (偏移量) 不能小于 0 !!!");
39
+ }
40
+ this.#offset = offset;
46
41
  return this;
47
42
  }
48
43
 
49
- limit(numberOfRows) {
50
- this.#limit = numberOfRows;
44
+ limit(limit) {
45
+ if (limit < 0) {
46
+ throw new Error("Limit 参数错误 : limit (行长度) 不能小于 0 !!!");
47
+ }
48
+ this.#limit = limit;
51
49
  return this;
52
50
  }
53
51
 
@@ -55,12 +53,8 @@ class QueryImpl extends Query {
55
53
  return this.#where;
56
54
  }
57
55
 
58
- getGroupBy() {
59
- return this.#groupBy;
60
- }
61
-
62
- getOrderBy() {
63
- return this.#orderBy;
56
+ getOrderBys() {
57
+ return this.#orderBys;
64
58
  }
65
59
 
66
60
  getOffset() {
@@ -76,13 +70,8 @@ class QueryImpl extends Query {
76
70
  return this;
77
71
  }
78
72
 
79
- clearGroupBy() {
80
- this.#groupBy = [];
81
- return this;
82
- }
83
-
84
- clearOrderBy() {
85
- this.#orderBy = [];
73
+ clearOrderBys() {
74
+ this.#orderBys = [];
86
75
  return this;
87
76
  }
88
77
 
@@ -96,46 +85,24 @@ class QueryImpl extends Query {
96
85
  return this;
97
86
  }
98
87
 
99
- addGroupBy(...groupByClauses) {
100
- for (let groupByClause of groupByClauses) {
101
- if (groupByClause == null) {
102
- continue;
103
- }
104
- if (Array.isArray(groupByClause)) {
105
- this.addGroupBy(...groupByClause);
106
- continue;
107
- }
108
- this.#groupBy.push(groupByClause);
109
- }
88
+ orderBy(...orderBys) {
89
+ this.#orderBys.push(...orderBys);
110
90
  return this;
111
91
  }
112
92
 
113
- addOrderBy(...orderByClauses) {
114
- for (let orderByClause of orderByClauses) {
115
- if (orderByClause == null) {
116
- continue;
117
- }
118
- if (Array.isArray(orderByClause)) {
119
- this.addOrderBy(...orderByClause);
120
- continue;
121
- }
122
- this.#orderBy.push(orderByClause);
123
- }
124
- return this;
125
- }
126
93
 
127
- removeWhereIf(filter) {
128
- removeIf(this.#where, filter);
94
+ asc(selector, ...controls) {
95
+ let useExpression = checkUseExpression(controls);
96
+ let o = new OrderBy(selector, ASC, useExpression);
97
+ this.orderBy(o);
129
98
  return this;
130
99
  }
131
100
 
132
- removeGroupByIf(filter) {
133
- removeIf(this.#groupBy, filter);
134
- return this;
135
- }
136
101
 
137
- removeOrderByIf(filter) {
138
- removeIf(this.#orderBy, filter);
102
+ desc(selector, ...controls) {
103
+ let useExpression = checkUseExpression(controls);
104
+ let o = new OrderBy(selector, DESC, useExpression);
105
+ this.orderBy(o);
139
106
  return this;
140
107
  }
141
108
 
@@ -16,13 +16,8 @@ class QueryLike extends Query {
16
16
  return this;
17
17
  }
18
18
 
19
- groupBy(...groupByClauses) {
20
- this.query().groupBy(...groupByClauses);
21
- return this;
22
- }
23
-
24
- orderBy(...orderByClauses) {
25
- this.query().orderBy(...orderByClauses);
19
+ orderBys(...orderBys) {
20
+ this.query().orderBy(...orderBys);
26
21
  return this;
27
22
  }
28
23
 
@@ -40,12 +35,8 @@ class QueryLike extends Query {
40
35
  return this.query().getWhere();
41
36
  }
42
37
 
43
- getGroupBy() {
44
- return this.query().getGroupBy();
45
- }
46
-
47
- getOrderBy() {
48
- return this.query().getOrderBy();
38
+ getOrderBys() {
39
+ return this.query().getOrderBys();
49
40
  }
50
41
 
51
42
  getOffset() {
@@ -61,16 +52,6 @@ class QueryLike extends Query {
61
52
  return this;
62
53
  }
63
54
 
64
- clearGroupBy() {
65
- this.query().clearGroupBy();
66
- return this;
67
- }
68
-
69
- clearOrderBy() {
70
- this.query().clearOrderBy();
71
- return this;
72
- }
73
-
74
55
  clearOffset() {
75
56
  this.query().clearOffset();
76
57
  return this;
@@ -81,23 +62,18 @@ class QueryLike extends Query {
81
62
  return this;
82
63
  }
83
64
 
84
- addGroupBy(...groupByClauses) {
85
- this.query().addGroupBy(groupByClauses);
86
- return this;
87
- }
88
-
89
- addOrderBy(...orderByClauses) {
90
- this.query().addOrderBy(orderByClauses);
65
+ orderBy(...orderByClauses) {
66
+ this.query().orderBy(...orderByClauses);
91
67
  return this;
92
68
  }
93
69
 
94
- removeGroupByIf(filter) {
95
- this.query().removeGroupByIf(filter);
70
+ asc(selector, ...controls) {
71
+ this.query().asc(selector, ...controls);
96
72
  return this;
97
73
  }
98
74
 
99
- removeOrderByIf(filter) {
100
- this.query().removeOrderByIf(filter);
75
+ desc(selector, ...controls) {
76
+ this.query().desc(selector, ...controls);
101
77
  return this;
102
78
  }
103
79