@axiosleo/orm-mysql 0.9.2 → 0.9.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/bin/orm-mysql.js +1 -1
- package/index.d.ts +3 -1
- package/package.json +1 -1
- package/src/builder.js +27 -2
- package/src/query.js +2 -1
package/bin/orm-mysql.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -70,7 +70,9 @@ export type QueryOperatorOptions = QueryOperatorBaseOptions & {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
export declare class Query {
|
|
73
|
-
|
|
73
|
+
options: QueryOperatorOptions;
|
|
74
|
+
|
|
75
|
+
constructor(operator?: OperatorType, alias?: string | null);
|
|
74
76
|
|
|
75
77
|
table(tableName: string, alias: string | null): this;
|
|
76
78
|
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -32,7 +32,21 @@ class Builder {
|
|
|
32
32
|
}
|
|
33
33
|
// eslint-disable-next-line no-fallthrough
|
|
34
34
|
case 'select': {
|
|
35
|
-
|
|
35
|
+
options.attrs = options.attrs || [];
|
|
36
|
+
const attrs = options.attrs.map((attr) => {
|
|
37
|
+
if (attr instanceof Function) {
|
|
38
|
+
const query = attr();
|
|
39
|
+
const builder = new Builder(query.options);
|
|
40
|
+
let s = `(${builder.sql})`;
|
|
41
|
+
if (query.alias) {
|
|
42
|
+
return query.alias.indexOf(' ') > -1 ? s + ' ' + this._buildFieldKey(query.alias)
|
|
43
|
+
: s + ' AS ' + this._buildFieldKey(query.alias);
|
|
44
|
+
}
|
|
45
|
+
return s;
|
|
46
|
+
}
|
|
47
|
+
return attr;
|
|
48
|
+
});
|
|
49
|
+
emit(tmp, `SELECT ${attrs.length ? attrs.map((a) => this._buildFieldKey(a)).join(',') : '*'} FROM ${this._buildTables(options.tables)}`);
|
|
36
50
|
emit(tmp, this._buildJoins(options.joins));
|
|
37
51
|
emit(tmp, this._buildContidion(options.conditions));
|
|
38
52
|
emit(tmp, this._buildOrders(options.orders));
|
|
@@ -199,6 +213,11 @@ class Builder {
|
|
|
199
213
|
}
|
|
200
214
|
|
|
201
215
|
_buildConditionValues(val) {
|
|
216
|
+
if (is.string(val)) {
|
|
217
|
+
if (val.startsWith('`') && val.endsWith('`')) {
|
|
218
|
+
return val;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
202
221
|
if (val instanceof Query) {
|
|
203
222
|
const builder = new Builder(val.options);
|
|
204
223
|
this.values = this.values.concat(builder.values);
|
|
@@ -263,7 +282,13 @@ class Builder {
|
|
|
263
282
|
return `(${this._buildContidion(c.value, '')})`;
|
|
264
283
|
}
|
|
265
284
|
let res = this._buildConditionValues(c.value);
|
|
266
|
-
|
|
285
|
+
if (!is.empty(res)) {
|
|
286
|
+
if (res.startsWith('`') && res.endsWith('`')) {
|
|
287
|
+
return `${this._buildFieldKey(c.key)} ${c.opt} ${res}`;
|
|
288
|
+
}
|
|
289
|
+
return `${this._buildFieldKey(c.key)} ${c.opt} (${res})`;
|
|
290
|
+
}
|
|
291
|
+
return `${this._buildFieldKey(c.key)} ${c.opt} ?`;
|
|
267
292
|
}).join('')}`;
|
|
268
293
|
}
|
|
269
294
|
return sql;
|
package/src/query.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
class Query {
|
|
4
|
-
constructor(operator = 'select') {
|
|
4
|
+
constructor(operator = 'select', alias = null) {
|
|
5
5
|
this.options = {
|
|
6
6
|
driver: 'mysql',
|
|
7
7
|
queryHandler: null,
|
|
@@ -16,6 +16,7 @@ class Query {
|
|
|
16
16
|
suffix: null,
|
|
17
17
|
transaction: false
|
|
18
18
|
};
|
|
19
|
+
this.alias = alias || null;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
table(tableName, alias) {
|