doix-db 0.0.22 → 0.0.24
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
|
@@ -7,6 +7,8 @@ const QQ_ESC = new stringEscape ([
|
|
|
7
7
|
[ '"', '""'],
|
|
8
8
|
])
|
|
9
9
|
|
|
10
|
+
const DELIM_UPDATE = [',', ' AND ']
|
|
11
|
+
|
|
10
12
|
class DbLang {
|
|
11
13
|
|
|
12
14
|
getDbObjectClass (o) {
|
|
@@ -105,6 +107,61 @@ class DbLang {
|
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
}
|
|
110
|
+
|
|
111
|
+
genUpdateParamsSql (name, data) {
|
|
112
|
+
|
|
113
|
+
const {model} = this; if (!model) throw Error ('Model not set')
|
|
114
|
+
|
|
115
|
+
const {map} = model; if (!map.has (name)) throw Error ('Entity not defined: ' + name)
|
|
116
|
+
|
|
117
|
+
const {pk, columns, qName} = map.get (name)
|
|
118
|
+
|
|
119
|
+
const sql = ['', ''], params = []
|
|
120
|
+
|
|
121
|
+
for (const [k, v] of Object.entries (data)) {
|
|
122
|
+
|
|
123
|
+
if (v === undefined) continue
|
|
124
|
+
|
|
125
|
+
if (!(k in columns)) continue
|
|
126
|
+
|
|
127
|
+
const {qName, nullable} = columns [k]
|
|
128
|
+
|
|
129
|
+
const i = pk.includes (k) ? 1 : 0
|
|
130
|
+
|
|
131
|
+
if (sql [i].length !== 0) sql [i] += DELIM_UPDATE [i]
|
|
132
|
+
|
|
133
|
+
sql [i] += qName + '='
|
|
134
|
+
|
|
135
|
+
if (v === null && !nullable) {
|
|
136
|
+
|
|
137
|
+
sql [i] += 'DEFAULT'
|
|
138
|
+
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
|
|
142
|
+
sql [i] += '?'
|
|
143
|
+
|
|
144
|
+
if (i === 0) params.push (v)
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
for (const k of pk) {
|
|
151
|
+
|
|
152
|
+
const v = data [k]; if (v == null) throw Error (`The value of ${k} must be defined and not null`)
|
|
153
|
+
|
|
154
|
+
params.push (v)
|
|
155
|
+
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (sql [0].length === 0) return null
|
|
159
|
+
|
|
160
|
+
params.push (`UPDATE ${qName} SET ${sql [0]} WHERE ${sql [1]}`)
|
|
161
|
+
|
|
162
|
+
return params
|
|
163
|
+
|
|
164
|
+
}
|
|
108
165
|
|
|
109
166
|
toParamsSql (query) {
|
|
110
167
|
|
package/lib/model/DbModel.js
CHANGED
package/lib/query/DbQuery.js
CHANGED
|
@@ -16,18 +16,6 @@ class DbQueryTableColumnComparison {
|
|
|
16
16
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
clone (table) {
|
|
20
|
-
|
|
21
|
-
const result = new DbQueryTableColumnComparison (table, this.column.name, this.op, this.params)
|
|
22
|
-
|
|
23
|
-
result.sql = this.sql
|
|
24
|
-
|
|
25
|
-
result.params = this.params
|
|
26
|
-
|
|
27
|
-
return result
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
19
|
setParams (value) {
|
|
32
20
|
|
|
33
21
|
const {op, table} = this, {lang} = table
|