doix-db 0.0.17 → 0.0.18
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/lib/DbLang.js
CHANGED
|
@@ -54,8 +54,53 @@ class DbLang {
|
|
|
54
54
|
return s + ')'
|
|
55
55
|
|
|
56
56
|
}
|
|
57
|
+
|
|
58
|
+
isUnaryOperator (op) {
|
|
59
|
+
|
|
60
|
+
switch (op) {
|
|
61
|
+
|
|
62
|
+
case 'IS NULL':
|
|
63
|
+
case 'IS NOT NULL':
|
|
64
|
+
return true
|
|
65
|
+
|
|
66
|
+
default:
|
|
67
|
+
return false
|
|
68
|
+
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
genComparisonRightPart (filter) {
|
|
74
|
+
|
|
75
|
+
switch (filter.op) {
|
|
76
|
+
|
|
77
|
+
case '=':
|
|
78
|
+
case '<':
|
|
79
|
+
case '>':
|
|
80
|
+
case '<=':
|
|
81
|
+
case '>=':
|
|
82
|
+
case '<>':
|
|
83
|
+
case 'LIKE':
|
|
84
|
+
case 'NOT LIKE':
|
|
85
|
+
return '?'
|
|
86
|
+
|
|
87
|
+
case 'BETWEEN':
|
|
88
|
+
return '? AND ?'
|
|
89
|
+
|
|
90
|
+
case 'IN':
|
|
91
|
+
case 'NOT IN':
|
|
92
|
+
return '(?' + ',?'.repeat (filter.params.length - 1) + ')'
|
|
93
|
+
|
|
94
|
+
default:
|
|
95
|
+
throw Error ('Unknown comparison operator: ' + op)
|
|
96
|
+
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
}
|
|
57
100
|
|
|
58
101
|
toParamsSql (query) {
|
|
102
|
+
|
|
103
|
+
const params = []
|
|
59
104
|
|
|
60
105
|
let select = ''; for (const {expr, qName} of query.columns.values ()) {
|
|
61
106
|
|
|
@@ -65,17 +110,25 @@ class DbLang {
|
|
|
65
110
|
|
|
66
111
|
}
|
|
67
112
|
|
|
68
|
-
// const [t] = query.tables
|
|
69
|
-
|
|
70
|
-
// const from = t.expr + ' AS ' + t.qName
|
|
71
|
-
|
|
72
113
|
let from = ''; for (const t of query.tables) {
|
|
73
114
|
|
|
74
115
|
if (!t.isFirst) from += ' ' + t.join + ' JOIN '
|
|
75
116
|
|
|
76
117
|
from += t.expr + ' AS ' + t.qName
|
|
77
118
|
|
|
78
|
-
if (!t.isFirst)
|
|
119
|
+
if (!t.isFirst) {
|
|
120
|
+
|
|
121
|
+
from += ' ON ' + t.on
|
|
122
|
+
|
|
123
|
+
for (const filter of t.filters) {
|
|
124
|
+
|
|
125
|
+
from += ' AND '
|
|
126
|
+
|
|
127
|
+
from = filter.appendTo (from, params)
|
|
128
|
+
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
}
|
|
79
132
|
|
|
80
133
|
}
|
|
81
134
|
|
|
@@ -89,9 +142,25 @@ class DbLang {
|
|
|
89
142
|
|
|
90
143
|
let sql = 'SELECT ' + select + ' FROM ' + from
|
|
91
144
|
|
|
145
|
+
const {filters} = query.tables [0]; if (filters.length !== 0) {
|
|
146
|
+
|
|
147
|
+
let where = ''; for (const filter of filters) {
|
|
148
|
+
|
|
149
|
+
if (where.length !== 0) where += ' AND '
|
|
150
|
+
|
|
151
|
+
where = filter.appendTo (where, params)
|
|
152
|
+
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
sql += ' WHERE ' + where
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
|
|
92
159
|
if (order.length !== 0) sql += ' ORDER BY ' + order
|
|
93
160
|
|
|
94
|
-
|
|
161
|
+
params.push (sql)
|
|
162
|
+
|
|
163
|
+
return params
|
|
95
164
|
|
|
96
165
|
}
|
|
97
166
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const DbRelation = require ('../model/DbRelation.js')
|
|
2
2
|
const DbQueryColumn = require ('./DbQueryColumn.js')
|
|
3
|
+
const DbQueryTableColumnComparison = require ('./DbQueryTableColumnComparison.js')
|
|
3
4
|
|
|
4
5
|
const JOIN_TYPES_ALOWED = ['LEFT', 'INNER', 'CROSS']
|
|
5
6
|
|
|
@@ -63,6 +64,12 @@ class DbQueryTable {
|
|
|
63
64
|
if ('on' in o) throw Error ('Only LEFT and INNER JOIN can have the ON clause')
|
|
64
65
|
|
|
65
66
|
}
|
|
67
|
+
|
|
68
|
+
this.filters = []
|
|
69
|
+
|
|
70
|
+
if (o.filters) for (const [name, op, value] of o.filters)
|
|
71
|
+
|
|
72
|
+
this.addColumnComparison (name, op, value)
|
|
66
73
|
|
|
67
74
|
query.tables.push (this)
|
|
68
75
|
|
|
@@ -73,6 +80,14 @@ class DbQueryTable {
|
|
|
73
80
|
return this.lang.quoteName (this.alias)
|
|
74
81
|
|
|
75
82
|
}
|
|
83
|
+
|
|
84
|
+
addColumnComparison (name, op, value) {
|
|
85
|
+
|
|
86
|
+
if (value == null && !this.lang.isUnaryOperator (op)) return
|
|
87
|
+
|
|
88
|
+
new DbQueryTableColumnComparison (this, name, op, value)
|
|
89
|
+
|
|
90
|
+
}
|
|
76
91
|
|
|
77
92
|
adjustJoinCondition () {
|
|
78
93
|
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
class DbQueryTableColumnComparison {
|
|
2
|
+
|
|
3
|
+
constructor (table, name, op, value) {
|
|
4
|
+
|
|
5
|
+
this.table = table
|
|
6
|
+
|
|
7
|
+
this.column = table.relation.columns [name]
|
|
8
|
+
|
|
9
|
+
this.op = op.toUpperCase ()
|
|
10
|
+
|
|
11
|
+
this.expr = table.qName + '.' + this.column.qName + ' ' + op
|
|
12
|
+
|
|
13
|
+
this.setParams (value)
|
|
14
|
+
|
|
15
|
+
table.filters.push (this)
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
setParams (value) {
|
|
20
|
+
|
|
21
|
+
const {op, table} = this, {lang} = table
|
|
22
|
+
|
|
23
|
+
if (lang.isUnaryOperator (op)) {
|
|
24
|
+
|
|
25
|
+
this.params = []
|
|
26
|
+
|
|
27
|
+
return
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
/*
|
|
31
|
+
if (value instanceof DbQuery) {
|
|
32
|
+
|
|
33
|
+
this.params = lang.toParamsSql (query)
|
|
34
|
+
|
|
35
|
+
this.expr += ' (' + params.pop () + ')'
|
|
36
|
+
|
|
37
|
+
return
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
*/
|
|
41
|
+
if (this.op !== 'IN' && this.op !== 'NOT IN') {
|
|
42
|
+
|
|
43
|
+
this.params = Array.isArray (value) ? value : [value]
|
|
44
|
+
|
|
45
|
+
this.expr += ' '
|
|
46
|
+
this.expr += lang.genComparisonRightPart (this)
|
|
47
|
+
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!Array.isArray (value)) throw Error (`An Array value is required for ${op}`)
|
|
53
|
+
|
|
54
|
+
if (value.length === 0) {
|
|
55
|
+
|
|
56
|
+
this.params = []
|
|
57
|
+
|
|
58
|
+
this.expr = this.op === 'IN' ? '0=1' : '0=0'
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
|
|
63
|
+
this.params = value
|
|
64
|
+
|
|
65
|
+
this.expr += ' '
|
|
66
|
+
this.expr += lang.genComparisonRightPart (this)
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
appendTo (sql, params) {
|
|
73
|
+
|
|
74
|
+
for (const p of this.params) params.push (p)
|
|
75
|
+
|
|
76
|
+
return sql + this.expr
|
|
77
|
+
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = DbQueryTableColumnComparison
|