doix-db 0.0.19 → 0.0.21
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/index.js +2 -1
- package/lib/DbLang.js +8 -8
- package/lib/model/DbModel.js +2 -2
- package/lib/query/DbQuery.js +22 -5
- package/lib/query/DbQueryColumn.js +2 -2
- package/lib/query/DbQueryOr.js +33 -0
- package/lib/query/DbQueryTable.js +6 -6
- package/lib/query/DbQueryTableColumnComparison.js +8 -8
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -13,5 +13,6 @@ module.exports = {
|
|
|
13
13
|
DbModel: require ('./lib/model/DbModel.js'),
|
|
14
14
|
DbQuery: require ('./lib/query/DbQuery.js'),
|
|
15
15
|
DbQueryTable: require ('./lib/query/DbQueryTable.js'),
|
|
16
|
-
DbQueryColumn: require ('./lib/query/DbQueryColumn.js'),
|
|
16
|
+
DbQueryColumn: require ('./lib/query/DbQueryColumn.js'),
|
|
17
|
+
DbQueryOr: require ('./lib/query/DbQueryOr.js'),
|
|
17
18
|
}
|
package/lib/DbLang.js
CHANGED
|
@@ -75,11 +75,11 @@ class DbLang {
|
|
|
75
75
|
switch (filter.op) {
|
|
76
76
|
|
|
77
77
|
case 'ILIKE':
|
|
78
|
-
filter.
|
|
78
|
+
filter.sql = 'UPPER(' + filter.sql + ') LIKE UPPER(?)'
|
|
79
79
|
return null
|
|
80
80
|
|
|
81
81
|
case 'NOT ILIKE':
|
|
82
|
-
filter.
|
|
82
|
+
filter.sql = 'UPPER(' + filter.sql + ') NOT LIKE UPPER(?)'
|
|
83
83
|
return null
|
|
84
84
|
|
|
85
85
|
case '=':
|
|
@@ -110,11 +110,11 @@ class DbLang {
|
|
|
110
110
|
|
|
111
111
|
const params = []
|
|
112
112
|
|
|
113
|
-
let select = ''; for (const {
|
|
113
|
+
let select = ''; for (const {sql, qName} of query.columns.values ()) {
|
|
114
114
|
|
|
115
115
|
if (select.length !== 0) select += ','
|
|
116
116
|
|
|
117
|
-
select +=
|
|
117
|
+
select += sql + ' AS ' + qName
|
|
118
118
|
|
|
119
119
|
}
|
|
120
120
|
|
|
@@ -122,7 +122,7 @@ class DbLang {
|
|
|
122
122
|
|
|
123
123
|
if (!t.isFirst) from += ' ' + t.join + ' JOIN '
|
|
124
124
|
|
|
125
|
-
from += t.
|
|
125
|
+
from += t.sql + ' AS ' + t.qName
|
|
126
126
|
|
|
127
127
|
if (!t.isFirst) {
|
|
128
128
|
|
|
@@ -140,11 +140,11 @@ class DbLang {
|
|
|
140
140
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
let order = ''; for (const {
|
|
143
|
+
let order = ''; for (const {sql, desc} of query.order) {
|
|
144
144
|
|
|
145
145
|
if (order.length !== 0) order += ','
|
|
146
146
|
|
|
147
|
-
order +=
|
|
147
|
+
order += sql; if (desc) order += ' DESC'
|
|
148
148
|
|
|
149
149
|
}
|
|
150
150
|
|
|
@@ -176,7 +176,7 @@ class DbLang {
|
|
|
176
176
|
|
|
177
177
|
for (const p of filter.params) params.push (p)
|
|
178
178
|
|
|
179
|
-
return sql + filter.
|
|
179
|
+
return sql + filter.sql
|
|
180
180
|
|
|
181
181
|
}
|
|
182
182
|
|
package/lib/model/DbModel.js
CHANGED
package/lib/query/DbQuery.js
CHANGED
|
@@ -3,7 +3,7 @@ const DbQueryColumn = require ('./DbQueryColumn.js')
|
|
|
3
3
|
|
|
4
4
|
class DbQuery {
|
|
5
5
|
|
|
6
|
-
constructor (model, from = []) {
|
|
6
|
+
constructor (model, from = [], options = {}) {
|
|
7
7
|
|
|
8
8
|
this.model = model
|
|
9
9
|
|
|
@@ -15,13 +15,30 @@ class DbQuery {
|
|
|
15
15
|
|
|
16
16
|
this.order = []
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
this.options = options
|
|
19
|
+
|
|
20
|
+
for (const [sql, o] of from) this.addTable (sql, o)
|
|
21
|
+
|
|
22
|
+
if ('order' in options) for (const o of options.order) {
|
|
23
|
+
|
|
24
|
+
if (Array.isArray (o)) {
|
|
25
|
+
|
|
26
|
+
this.orderBy (o [0], o [1])
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
|
|
31
|
+
this.orderBy (o)
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
}
|
|
19
36
|
|
|
20
37
|
}
|
|
21
38
|
|
|
22
|
-
addTable (
|
|
39
|
+
addTable (sql, o = {}) {
|
|
23
40
|
|
|
24
|
-
return new DbQueryTable (this,
|
|
41
|
+
return new DbQueryTable (this, sql, o)
|
|
25
42
|
|
|
26
43
|
}
|
|
27
44
|
|
|
@@ -61,7 +78,7 @@ class DbQuery {
|
|
|
61
78
|
|
|
62
79
|
}
|
|
63
80
|
|
|
64
|
-
const t = q.addTable (table.relation ? table.relation.name : table.
|
|
81
|
+
const t = q.addTable (table.relation ? table.relation.name : table.sql, o)
|
|
65
82
|
|
|
66
83
|
for (const f of table.filters) f.clone (t)
|
|
67
84
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
class DbQueryOr {
|
|
2
|
+
|
|
3
|
+
constructor (filters = []) {
|
|
4
|
+
|
|
5
|
+
this.filters = filters
|
|
6
|
+
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
get sql () {
|
|
10
|
+
|
|
11
|
+
const {filters} = this; if (filters.length === 0) throw Error ('No one term in this search condition')
|
|
12
|
+
|
|
13
|
+
return filters.map (i => i.sql).join (' OR ')
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get params () {
|
|
18
|
+
|
|
19
|
+
let a = []
|
|
20
|
+
|
|
21
|
+
for (const {params} of this.filters)
|
|
22
|
+
|
|
23
|
+
if (Array.isArray (params))
|
|
24
|
+
|
|
25
|
+
a = a.concat (params)
|
|
26
|
+
|
|
27
|
+
return a
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = DbQueryOr
|
|
@@ -6,7 +6,7 @@ const JOIN_TYPES_ALOWED = ['LEFT', 'INNER', 'CROSS']
|
|
|
6
6
|
|
|
7
7
|
class DbQueryTable {
|
|
8
8
|
|
|
9
|
-
constructor (query,
|
|
9
|
+
constructor (query, sql, o = {}) {
|
|
10
10
|
|
|
11
11
|
const {model, lang} = query, {map} = model
|
|
12
12
|
|
|
@@ -16,13 +16,13 @@ class DbQueryTable {
|
|
|
16
16
|
|
|
17
17
|
this.lang = lang
|
|
18
18
|
|
|
19
|
-
if (map.has (
|
|
19
|
+
if (map.has (sql)) {
|
|
20
20
|
|
|
21
|
-
this.relation = map.get (
|
|
21
|
+
this.relation = map.get (sql)
|
|
22
22
|
|
|
23
|
-
this.
|
|
23
|
+
this.sql = this.relation.qName
|
|
24
24
|
|
|
25
|
-
this.alias = o.as ||
|
|
25
|
+
this.alias = o.as || sql
|
|
26
26
|
|
|
27
27
|
const {columns} = this.relation
|
|
28
28
|
|
|
@@ -35,7 +35,7 @@ class DbQueryTable {
|
|
|
35
35
|
|
|
36
36
|
this.alias = o.as
|
|
37
37
|
|
|
38
|
-
this.
|
|
38
|
+
this.sql = sql
|
|
39
39
|
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -8,7 +8,7 @@ class DbQueryTableColumnComparison {
|
|
|
8
8
|
|
|
9
9
|
this.op = op.toUpperCase ()
|
|
10
10
|
|
|
11
|
-
this.
|
|
11
|
+
this.sql = table.qName + '.' + this.column.qName
|
|
12
12
|
|
|
13
13
|
this.setParams (value)
|
|
14
14
|
|
|
@@ -20,7 +20,7 @@ class DbQueryTableColumnComparison {
|
|
|
20
20
|
|
|
21
21
|
const result = new DbQueryTableColumnComparison (table, this.column.name, this.op, this.params)
|
|
22
22
|
|
|
23
|
-
result.
|
|
23
|
+
result.sql = this.sql
|
|
24
24
|
|
|
25
25
|
result.params = this.params
|
|
26
26
|
|
|
@@ -36,7 +36,7 @@ class DbQueryTableColumnComparison {
|
|
|
36
36
|
|
|
37
37
|
this.params = []
|
|
38
38
|
|
|
39
|
-
this.
|
|
39
|
+
this.sql += ' ' + op
|
|
40
40
|
|
|
41
41
|
return
|
|
42
42
|
|
|
@@ -46,7 +46,7 @@ class DbQueryTableColumnComparison {
|
|
|
46
46
|
|
|
47
47
|
this.params = lang.toParamsSql (query)
|
|
48
48
|
|
|
49
|
-
this.
|
|
49
|
+
this.sql += ' (' + params.pop () + ')'
|
|
50
50
|
|
|
51
51
|
return
|
|
52
52
|
|
|
@@ -58,7 +58,7 @@ class DbQueryTableColumnComparison {
|
|
|
58
58
|
|
|
59
59
|
const right = lang.genComparisonRightPart (this)
|
|
60
60
|
|
|
61
|
-
if (right !== null) this.
|
|
61
|
+
if (right !== null) this.sql += ' ' + op + ' ' + right
|
|
62
62
|
|
|
63
63
|
return
|
|
64
64
|
|
|
@@ -70,15 +70,15 @@ class DbQueryTableColumnComparison {
|
|
|
70
70
|
|
|
71
71
|
this.params = []
|
|
72
72
|
|
|
73
|
-
this.
|
|
73
|
+
this.sql = this.op === 'IN' ? '0=1' : '0=0'
|
|
74
74
|
|
|
75
75
|
}
|
|
76
76
|
else {
|
|
77
77
|
|
|
78
78
|
this.params = value
|
|
79
79
|
|
|
80
|
-
this.
|
|
81
|
-
this.
|
|
80
|
+
this.sql += ' ' + op + ' '
|
|
81
|
+
this.sql += lang.genComparisonRightPart (this)
|
|
82
82
|
|
|
83
83
|
}
|
|
84
84
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "doix-db",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"description": "Shared database related code for doix",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://github.com/do-/node-doix-db#readme",
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"doix": "^0.0.
|
|
43
|
+
"doix": "^0.0.37"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"jest": "^29.3.1"
|