@asla/yoursql 0.0.1
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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/mod.d.ts +7 -0
- package/dist/mod.js +590 -0
- package/dist/select/TableQuery.d.ts +39 -0
- package/dist/select/_statement.d.ts +15 -0
- package/dist/select/select.d.ts +55 -0
- package/dist/select/selectable.d.ts +37 -0
- package/dist/select/type.d.ts +55 -0
- package/dist/sql_value/db_type.d.ts +11 -0
- package/dist/sql_value/sql_value.d.ts +51 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Asnow
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[![ESM package][package]][package-url]
|
|
2
|
+
[![NPM version][npm]][npm-url]
|
|
3
|
+
[![JSR version][jsr]][jsr-url]
|
|
4
|
+
[![Install size][size]][size-url]
|
|
5
|
+
|
|
6
|
+
[package]: https://img.shields.io/badge/package-ESM-ffe536.svg
|
|
7
|
+
[package-url]: https://nodejs.org/api/esm.html
|
|
8
|
+
[npm]: https://img.shields.io/npm/v/yoursql.svg
|
|
9
|
+
[npm-url]: https://npmjs.com/package/yoursql
|
|
10
|
+
[jsr]: https://jsr.io/badges/@asla/yoursql
|
|
11
|
+
[jsr-url]: https://jsr.io/@asla/yoursql
|
|
12
|
+
[node]: https://img.shields.io/node/v/yoursql.svg
|
|
13
|
+
[node-url]: https://nodejs.org
|
|
14
|
+
[size]: https://packagephobia.com/badge?p=yoursql
|
|
15
|
+
[size-url]: https://packagephobia.com/result?p=yoursql
|
|
16
|
+
|
|
17
|
+
SQL 生成器
|
|
18
|
+
|
|
19
|
+
[API 文档](https://jsr.io/@asla/yoursql/doc)
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./sql_value/db_type.ts";
|
|
2
|
+
export * from "./sql_value/sql_value.ts";
|
|
3
|
+
export * from "./select/type.ts";
|
|
4
|
+
export * from "./select/select.ts";
|
|
5
|
+
export * from "./select/selectable.ts";
|
|
6
|
+
export * from "./select/TableQuery.ts";
|
|
7
|
+
//# sourceMappingURL=mod.d.ts.map
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
19
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
20
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
21
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
25
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
26
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
27
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
28
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
32
|
+
var e = new Error(message);
|
|
33
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
var _SqlRaw_value;
|
|
37
|
+
/**
|
|
38
|
+
* SQL 原始字符对象
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
class SqlRaw {
|
|
42
|
+
constructor(value) {
|
|
43
|
+
_SqlRaw_value.set(this, void 0);
|
|
44
|
+
__classPrivateFieldSet(this, _SqlRaw_value, value, "f");
|
|
45
|
+
}
|
|
46
|
+
toString() {
|
|
47
|
+
return __classPrivateFieldGet(this, _SqlRaw_value, "f");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
_SqlRaw_value = new WeakMap();
|
|
51
|
+
/**
|
|
52
|
+
* SQL value 生成器
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
class SqlValuesCreator {
|
|
56
|
+
/**
|
|
57
|
+
* 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
|
|
58
|
+
* @example 输入 a'b'c , 返回 a''b''c.
|
|
59
|
+
*/
|
|
60
|
+
static string(value) {
|
|
61
|
+
return "'" + value.replaceAll("'", "''") + "'";
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @param map - 自定义对象转换
|
|
65
|
+
*/
|
|
66
|
+
constructor(map = new Map()) {
|
|
67
|
+
this.map = map;
|
|
68
|
+
}
|
|
69
|
+
string(value) {
|
|
70
|
+
return SqlValuesCreator.string(value);
|
|
71
|
+
}
|
|
72
|
+
number(value) {
|
|
73
|
+
return value.toString();
|
|
74
|
+
}
|
|
75
|
+
/** 将 JS 对象转为 SQL 的字符值的形式 */
|
|
76
|
+
toSqlStr(value) {
|
|
77
|
+
switch (typeof value) {
|
|
78
|
+
case "bigint":
|
|
79
|
+
return this.number(value);
|
|
80
|
+
case "number":
|
|
81
|
+
return this.number(value);
|
|
82
|
+
case "string":
|
|
83
|
+
return this.string(value);
|
|
84
|
+
case "boolean":
|
|
85
|
+
return value.toString();
|
|
86
|
+
case "object":
|
|
87
|
+
if (value === null)
|
|
88
|
+
return "NULL";
|
|
89
|
+
if (value instanceof SqlRaw) {
|
|
90
|
+
return value.toString();
|
|
91
|
+
}
|
|
92
|
+
for (const Class of this.map.keys()) {
|
|
93
|
+
if (value instanceof Class)
|
|
94
|
+
return this.map.get(Class).call(this, value);
|
|
95
|
+
}
|
|
96
|
+
return this.string(JSON.stringify(value));
|
|
97
|
+
default:
|
|
98
|
+
let type = typeof value;
|
|
99
|
+
if (type === "object")
|
|
100
|
+
type = value.constructor?.name ?? "object";
|
|
101
|
+
throw new Error("不支持转换 " + type + " 类型");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 将对象列表转为 SQL 的 VALUES
|
|
106
|
+
* @example 返回示例: " (...),(...) "
|
|
107
|
+
*/
|
|
108
|
+
objectListToValuesList(objectList, keys = Object.keys(objectList[0])) {
|
|
109
|
+
let i = 0;
|
|
110
|
+
let j;
|
|
111
|
+
let value;
|
|
112
|
+
const rowValues = new Array(objectList.length);
|
|
113
|
+
try {
|
|
114
|
+
for (; i < objectList.length; i++) {
|
|
115
|
+
const object = objectList[i];
|
|
116
|
+
const values = [];
|
|
117
|
+
j = 0;
|
|
118
|
+
for (; j < keys.length; j++) {
|
|
119
|
+
value = object[keys[j]];
|
|
120
|
+
values[j] = this.toSqlStr(value);
|
|
121
|
+
}
|
|
122
|
+
rowValues[i] = "\n(" + values.join(",") + ")";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
let message = error instanceof Error ? error.message : String(error);
|
|
127
|
+
throw new Error("第 " + i + " 项,字段 '" + keys[j] + "' 异常," + message);
|
|
128
|
+
}
|
|
129
|
+
return rowValues.join(",");
|
|
130
|
+
}
|
|
131
|
+
objectToValues(object, keys) {
|
|
132
|
+
const values = [];
|
|
133
|
+
let i = 0;
|
|
134
|
+
let value;
|
|
135
|
+
try {
|
|
136
|
+
for (; i < keys.length; i++) {
|
|
137
|
+
value = object[keys[i]];
|
|
138
|
+
values[i] = this.toSqlStr(value);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
let message = error instanceof Error ? error.message : String(error);
|
|
143
|
+
throw new Error("字段 '" + keys[i] + "' 异常," + message);
|
|
144
|
+
}
|
|
145
|
+
return values.join(",");
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 将数组列表转为 SQL 的一个 value
|
|
149
|
+
* @example 返回示例: " 'abc', '6', 'now()' "
|
|
150
|
+
*/
|
|
151
|
+
toValues(values) {
|
|
152
|
+
return values.map(this.toSqlStr.bind(this)).join(",");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @public
|
|
158
|
+
* PgSql的值转换
|
|
159
|
+
*/
|
|
160
|
+
class PgSqlValue extends SqlValuesCreator {
|
|
161
|
+
constructor() {
|
|
162
|
+
const map = new Map();
|
|
163
|
+
map.set(Array, PgSqlValue.prototype.array);
|
|
164
|
+
map.set(Date, PgSqlValue.prototype.timestamp);
|
|
165
|
+
super(map);
|
|
166
|
+
}
|
|
167
|
+
timestamp(value) {
|
|
168
|
+
return SqlValuesCreator.string(value.toISOString());
|
|
169
|
+
}
|
|
170
|
+
array(value) {
|
|
171
|
+
return "ARRAY[" + value.map(this.toSqlStr.bind(this)).join(", ") + "]";
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** @public */
|
|
176
|
+
class SqlSelectable {
|
|
177
|
+
}
|
|
178
|
+
/** @public */
|
|
179
|
+
class DbTable extends SqlSelectable {
|
|
180
|
+
constructor(name, columns) {
|
|
181
|
+
super();
|
|
182
|
+
this.name = name;
|
|
183
|
+
if (columns instanceof Array)
|
|
184
|
+
this.columns = [...columns];
|
|
185
|
+
else
|
|
186
|
+
this.columns = Object.keys(columns);
|
|
187
|
+
}
|
|
188
|
+
toSelect() {
|
|
189
|
+
return this.name;
|
|
190
|
+
}
|
|
191
|
+
toString() {
|
|
192
|
+
return this.name;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* SELECT、UPDATE、DELETE、INSERT INTO 带结果的返回值
|
|
197
|
+
* @public
|
|
198
|
+
*/
|
|
199
|
+
class SqlQueryStatement extends SqlSelectable {
|
|
200
|
+
constructor(sql, columns) {
|
|
201
|
+
super();
|
|
202
|
+
this.sql = sql;
|
|
203
|
+
this.columns = columns;
|
|
204
|
+
}
|
|
205
|
+
toString() {
|
|
206
|
+
return this.sql;
|
|
207
|
+
}
|
|
208
|
+
toSelect() {
|
|
209
|
+
return "(" + this.toString() + ")";
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function genOderBy(orderBy, orderNullRule) {
|
|
214
|
+
let select = [];
|
|
215
|
+
let v;
|
|
216
|
+
for (const key of Object.keys(orderBy)) {
|
|
217
|
+
v = orderBy[key];
|
|
218
|
+
switch (v) {
|
|
219
|
+
case "ASC":
|
|
220
|
+
break;
|
|
221
|
+
case "DESC":
|
|
222
|
+
break;
|
|
223
|
+
default:
|
|
224
|
+
throw new Error("orderBy 只能是 ASE 或 DESC. 当前值:" + String(v));
|
|
225
|
+
}
|
|
226
|
+
select.push(key + " " + v);
|
|
227
|
+
}
|
|
228
|
+
let sql = "\nORDER BY " + select.join(", ");
|
|
229
|
+
if (orderNullRule) {
|
|
230
|
+
switch (orderNullRule) {
|
|
231
|
+
case "FIRST":
|
|
232
|
+
break;
|
|
233
|
+
case "LAST":
|
|
234
|
+
break;
|
|
235
|
+
default:
|
|
236
|
+
throw new Error("orderNullRule 只能是 FIRST 或 LAST. 当前值:" + String(orderNullRule));
|
|
237
|
+
}
|
|
238
|
+
sql += "NULLS " + orderNullRule;
|
|
239
|
+
}
|
|
240
|
+
return sql;
|
|
241
|
+
}
|
|
242
|
+
function selectColumnsOrTable(columns) {
|
|
243
|
+
let sqlSelect;
|
|
244
|
+
let select;
|
|
245
|
+
if (columns instanceof Array) {
|
|
246
|
+
sqlSelect = columns;
|
|
247
|
+
select = columns;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
sqlSelect = [];
|
|
251
|
+
select = [];
|
|
252
|
+
let c;
|
|
253
|
+
for (const key of Object.keys(columns)) {
|
|
254
|
+
c = columns[key];
|
|
255
|
+
if (typeof c === "string" && c !== key) {
|
|
256
|
+
sqlSelect.push(key + " AS " + c);
|
|
257
|
+
select.push(c);
|
|
258
|
+
}
|
|
259
|
+
else if (c) {
|
|
260
|
+
sqlSelect.push(key);
|
|
261
|
+
select.push(key);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (select.length === 0)
|
|
266
|
+
throw new Error("选择列为空");
|
|
267
|
+
return { columns: select, sqlColumns: sqlSelect.join(", ") };
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* @param select 选择的行
|
|
271
|
+
* @param tableColumns 全部行
|
|
272
|
+
* @param push 要讲选择的行加入到集合中。
|
|
273
|
+
*/
|
|
274
|
+
function genNewColumns(select, tableColumns,
|
|
275
|
+
/** newName -> oldName */
|
|
276
|
+
push = new Map()) {
|
|
277
|
+
if (select === "*") {
|
|
278
|
+
for (const key of tableColumns) {
|
|
279
|
+
if (push.has(key))
|
|
280
|
+
throw new ColumnRepeatError(key);
|
|
281
|
+
push.set(key, null);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
genSelectAsColumns(select, push);
|
|
286
|
+
}
|
|
287
|
+
return push;
|
|
288
|
+
}
|
|
289
|
+
function genSelectAsColumns(select,
|
|
290
|
+
/** newName -> oldName */
|
|
291
|
+
push = new Map()) {
|
|
292
|
+
if (select instanceof Array) {
|
|
293
|
+
for (const key of select) {
|
|
294
|
+
if (push.has(key))
|
|
295
|
+
throw new ColumnRepeatError(key);
|
|
296
|
+
push.set(key, null);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
let finalName;
|
|
301
|
+
for (const oldName of Object.keys(select)) {
|
|
302
|
+
if (typeof select[oldName] === "string")
|
|
303
|
+
finalName = select[oldName];
|
|
304
|
+
else if (select[oldName])
|
|
305
|
+
finalName = oldName;
|
|
306
|
+
else
|
|
307
|
+
continue;
|
|
308
|
+
if (push.has(finalName))
|
|
309
|
+
throw new ColumnRepeatError(finalName);
|
|
310
|
+
push.set(finalName, oldName);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return push;
|
|
314
|
+
}
|
|
315
|
+
class ColumnRepeatError extends Error {
|
|
316
|
+
constructor(columnName) {
|
|
317
|
+
super("Column name '" + columnName + "' repeated");
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
var _SelectImpl_instances, _a, _SelectImpl_joinOn, _SelectImpl_join;
|
|
322
|
+
/** @public */
|
|
323
|
+
const createSelect = function createSelect(tb, columns, option) {
|
|
324
|
+
let select = createSelectMap(tb, columns, option).selects;
|
|
325
|
+
return new SelectImpl(select);
|
|
326
|
+
};
|
|
327
|
+
class SelectImpl extends SqlSelectable {
|
|
328
|
+
get columns() {
|
|
329
|
+
return this.getColumns();
|
|
330
|
+
}
|
|
331
|
+
*getColumns() {
|
|
332
|
+
for (const { columns } of this.tableList.values()) {
|
|
333
|
+
yield* columns.keys();
|
|
334
|
+
}
|
|
335
|
+
yield* this.addedColumns.keys();
|
|
336
|
+
}
|
|
337
|
+
constructor(tableList, addedColumns = new Map()) {
|
|
338
|
+
super();
|
|
339
|
+
_SelectImpl_instances.add(this);
|
|
340
|
+
this.tableList = tableList;
|
|
341
|
+
this.addedColumns = addedColumns;
|
|
342
|
+
}
|
|
343
|
+
toSelect() {
|
|
344
|
+
return "(" + this.toString() + ")";
|
|
345
|
+
}
|
|
346
|
+
toString() {
|
|
347
|
+
let tables = [];
|
|
348
|
+
let join = "";
|
|
349
|
+
let selectColumns = [];
|
|
350
|
+
const size = this.tableList.size;
|
|
351
|
+
for (const [tableAs, { columns, from, on, type }] of this.tableList) {
|
|
352
|
+
let tableStr = tableAs === from ? from : from + " AS " + tableAs;
|
|
353
|
+
if (type) {
|
|
354
|
+
join += "\n" + type + " " + tableStr;
|
|
355
|
+
if (on)
|
|
356
|
+
join += " ON " + on;
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
tables.push(tableStr);
|
|
360
|
+
}
|
|
361
|
+
if (size === 1 && tableAs === from) {
|
|
362
|
+
for (const [column, oldName] of columns) {
|
|
363
|
+
if (!oldName || oldName == column)
|
|
364
|
+
selectColumns.push(column);
|
|
365
|
+
else
|
|
366
|
+
selectColumns.push(oldName + " AS " + column);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
for (const [column, oldName] of columns) {
|
|
371
|
+
if (!oldName || oldName === column)
|
|
372
|
+
selectColumns.push(tableAs + "." + column);
|
|
373
|
+
else
|
|
374
|
+
selectColumns.push(tableAs + "." + oldName + " AS " + column);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
for (const [asName, columnStr] of this.addedColumns) {
|
|
379
|
+
if (asName === columnStr)
|
|
380
|
+
selectColumns.push(asName);
|
|
381
|
+
else
|
|
382
|
+
selectColumns.push(columnStr + " AS " + asName);
|
|
383
|
+
}
|
|
384
|
+
if (selectColumns.length === 0)
|
|
385
|
+
throw new Error("Columns 为空");
|
|
386
|
+
let sql = `SELECT ${selectColumns.join(", ")}\nFROM ${tables.join(", ")}` + join;
|
|
387
|
+
return sql;
|
|
388
|
+
}
|
|
389
|
+
fullJoin(tb, columns, option) {
|
|
390
|
+
return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "FULL JOIN", tb, columns, option);
|
|
391
|
+
}
|
|
392
|
+
innerJoin(tb, columns, option) {
|
|
393
|
+
return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "INNER JOIN", tb, columns, option);
|
|
394
|
+
}
|
|
395
|
+
leftJoin(tb, columns, option) {
|
|
396
|
+
return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "LEFT JOIN", tb, columns, option);
|
|
397
|
+
}
|
|
398
|
+
rightJoin(tb, columns, option) {
|
|
399
|
+
return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_joinOn).call(this, "RIGHT JOIN", tb, columns, option);
|
|
400
|
+
}
|
|
401
|
+
naturalJoin(tb, columns, option) {
|
|
402
|
+
return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "NATURAL JOIN", tb, columns, option);
|
|
403
|
+
}
|
|
404
|
+
crossJoin(tb, columns, option) {
|
|
405
|
+
return __classPrivateFieldGet(this, _SelectImpl_instances, "m", _SelectImpl_join).call(this, "CROSS JOIN", tb, columns, option);
|
|
406
|
+
}
|
|
407
|
+
select(tb, columns, option) {
|
|
408
|
+
let select = createSelectMap(tb, columns, option, this.tableList).selects;
|
|
409
|
+
const obj = new _a(select, new Map(this.addedColumns));
|
|
410
|
+
return obj;
|
|
411
|
+
}
|
|
412
|
+
addColumns(add) {
|
|
413
|
+
for (const [asNewName, columnStr] of Object.entries(add)) {
|
|
414
|
+
if (this.addedColumns.has(asNewName))
|
|
415
|
+
throw new Error();
|
|
416
|
+
this.addedColumns.set(asNewName, columnStr);
|
|
417
|
+
}
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
toQuery(option = {}) {
|
|
421
|
+
const { where, orderNullRule } = option;
|
|
422
|
+
let sql = this.toString();
|
|
423
|
+
if (where)
|
|
424
|
+
sql += "\nWHERE " + where;
|
|
425
|
+
const orderBy = option.orderBy;
|
|
426
|
+
if (orderBy)
|
|
427
|
+
sql += genOderBy(orderBy, orderNullRule);
|
|
428
|
+
if (option.limit)
|
|
429
|
+
sql += "\nLIMIT " + option.limit;
|
|
430
|
+
if (option.offset)
|
|
431
|
+
sql += "\nOFFSET " + option.offset;
|
|
432
|
+
return new SqlQueryStatement(sql, Array.from(this.columns));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
_a = SelectImpl, _SelectImpl_instances = new WeakSet(), _SelectImpl_joinOn = function _SelectImpl_joinOn(type, tb, columns, option) {
|
|
436
|
+
let { selects, tbInfo } = createSelectMap(tb, columns, option, this.tableList);
|
|
437
|
+
tbInfo.type = type;
|
|
438
|
+
tbInfo.on = option.on;
|
|
439
|
+
const next = new _a(selects, new Map(this.addedColumns));
|
|
440
|
+
Object.defineProperty(next, "select", { value: undefined, configurable: false, enumerable: false });
|
|
441
|
+
return next;
|
|
442
|
+
}, _SelectImpl_join = function _SelectImpl_join(type, tb, columns, option) {
|
|
443
|
+
let { selects, tbInfo } = createSelectMap(tb, columns, option, this.tableList);
|
|
444
|
+
tbInfo.type = type;
|
|
445
|
+
const next = new _a(selects, new Map(this.addedColumns));
|
|
446
|
+
Object.defineProperty(next, "select", { value: undefined, configurable: false, enumerable: false });
|
|
447
|
+
return next;
|
|
448
|
+
};
|
|
449
|
+
function createSelectMap(tb, columns, option = {}, selects) {
|
|
450
|
+
let select = new Map(selects);
|
|
451
|
+
let tableAs;
|
|
452
|
+
let tbInfo;
|
|
453
|
+
if (typeof tb === "string") {
|
|
454
|
+
throw new Error("tb 应是 SqlSelectable 类型");
|
|
455
|
+
// if (typeof columns !== "object") throw new Error("缺少 option.columns");
|
|
456
|
+
// const newColumns = genSelectAsColumns(columns);
|
|
457
|
+
// tableAs = option.tableAs ?? tb;
|
|
458
|
+
// tbInfo = { table: tb, columns: newColumns };
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
const from = tb.toSelect();
|
|
462
|
+
if (tb instanceof DbTable) {
|
|
463
|
+
tableAs = option.tableAs ?? from;
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
tableAs = option.tableAs ?? "t" + select.size;
|
|
467
|
+
}
|
|
468
|
+
if (columns) {
|
|
469
|
+
const newColumns = genNewColumns(columns, tb.columns);
|
|
470
|
+
tbInfo = { from, columns: newColumns };
|
|
471
|
+
}
|
|
472
|
+
else
|
|
473
|
+
tbInfo = { from, columns: new Map() };
|
|
474
|
+
}
|
|
475
|
+
if (select.has(tableAs))
|
|
476
|
+
throw new TableRepeatError(tableAs);
|
|
477
|
+
select.set(tableAs, tbInfo);
|
|
478
|
+
return { selects: select, tbInfo: tbInfo };
|
|
479
|
+
}
|
|
480
|
+
class TableRepeatError extends Error {
|
|
481
|
+
constructor(tableName) {
|
|
482
|
+
super("Table name '" + tableName + "' repeated");
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
// interface SqlFrom<T extends object> {
|
|
486
|
+
// from<A extends object>(table: SqlSelectable<A>): SqlFrom<T & A>;
|
|
487
|
+
// }
|
|
488
|
+
// declare function from<T extends SqlSelectable<any>[]>(...table: T): Select<MergeSelectable<T>>;
|
|
489
|
+
// type MergeSelectable<T extends any[]> = T extends [SqlSelectable<infer P>, ...infer Q] ? P & MergeSelectable<Q> : {};
|
|
490
|
+
|
|
491
|
+
/** @public */
|
|
492
|
+
class DbTableQuery extends DbTable {
|
|
493
|
+
constructor(name, columns, statement) {
|
|
494
|
+
super(name, columns);
|
|
495
|
+
this.name = name;
|
|
496
|
+
this.statement = statement;
|
|
497
|
+
}
|
|
498
|
+
select(columns, option) {
|
|
499
|
+
return createSelect(this, columns, option);
|
|
500
|
+
}
|
|
501
|
+
insert(values, option) {
|
|
502
|
+
let insertCol;
|
|
503
|
+
let valuesStr;
|
|
504
|
+
if (values instanceof Array) {
|
|
505
|
+
if (values.length === 0)
|
|
506
|
+
throw new Error("值不能为空");
|
|
507
|
+
insertCol = Object.keys(values[0]);
|
|
508
|
+
valuesStr = `VALUES ${this.statement.objectListToValuesList(values, insertCol)}`;
|
|
509
|
+
}
|
|
510
|
+
else if (values instanceof SqlQueryStatement) {
|
|
511
|
+
// todo 验证 values.columns 和 this.columns 是否匹配
|
|
512
|
+
valuesStr = values.toSelect();
|
|
513
|
+
insertCol = values.columns;
|
|
514
|
+
}
|
|
515
|
+
else
|
|
516
|
+
throw new Error("values 应该是 Array 或 TableQuery 类型");
|
|
517
|
+
if (insertCol.length === 0)
|
|
518
|
+
throw new Error("插入列不能为空");
|
|
519
|
+
let sql = `INSERT INTO ${this.name} (${insertCol.join(",")})\n${valuesStr}`;
|
|
520
|
+
if (option) {
|
|
521
|
+
const { updateValues, conflict, where } = option;
|
|
522
|
+
if (conflict) {
|
|
523
|
+
sql += `\nON CONFLICT (${conflict.join(",")})`;
|
|
524
|
+
if (updateValues) {
|
|
525
|
+
const updateKey = Object.entries(updateValues);
|
|
526
|
+
sql += `\nDO UPDATE SET\n${updateKey.map(([key, v = "EXCLUDED." + key]) => key + " = " + v).join(",\n")}`;
|
|
527
|
+
}
|
|
528
|
+
else
|
|
529
|
+
sql += "DO NOTHING";
|
|
530
|
+
if (where)
|
|
531
|
+
sql += "\nWHERE " + where;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return sql;
|
|
535
|
+
}
|
|
536
|
+
insertWithResult(values, returns, option) {
|
|
537
|
+
let sql = this.insert(values, option);
|
|
538
|
+
return genRetuningSql(sql, returns, values instanceof SqlQueryStatement ? values.columns : this.columns);
|
|
539
|
+
}
|
|
540
|
+
update(values, option = {}) {
|
|
541
|
+
const { where } = option;
|
|
542
|
+
const updateKey = Object.entries(values);
|
|
543
|
+
if (updateKey.length === 0)
|
|
544
|
+
throw new Error("值不能为空");
|
|
545
|
+
let setList = [];
|
|
546
|
+
for (const [k, v] of updateKey) {
|
|
547
|
+
if (v === undefined)
|
|
548
|
+
continue;
|
|
549
|
+
setList.push(k + " = " + this.statement.toSqlStr(v));
|
|
550
|
+
}
|
|
551
|
+
let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
|
|
552
|
+
if (where)
|
|
553
|
+
sql += genWhere(where);
|
|
554
|
+
return sql;
|
|
555
|
+
}
|
|
556
|
+
updateWithResult(values, returns, option) {
|
|
557
|
+
let sql = this.update(values, option);
|
|
558
|
+
return genRetuningSql(sql, returns, values instanceof SqlQueryStatement ? values.columns : this.columns);
|
|
559
|
+
}
|
|
560
|
+
delete(option = {}) {
|
|
561
|
+
let sql = "DELTE FROM " + this.name;
|
|
562
|
+
if (option.where)
|
|
563
|
+
sql += genWhere(option.where);
|
|
564
|
+
return sql;
|
|
565
|
+
}
|
|
566
|
+
deleteWithResult(returns = "*", option) {
|
|
567
|
+
let sql = this.delete(option);
|
|
568
|
+
return genRetuningSql(sql, returns, this.columns);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function genRetuningSql(sql, returns, tableColumns) {
|
|
572
|
+
let columnsStr;
|
|
573
|
+
let columns;
|
|
574
|
+
if (returns === "*") {
|
|
575
|
+
columns = tableColumns;
|
|
576
|
+
columnsStr = "*";
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
const res = selectColumnsOrTable(returns);
|
|
580
|
+
columnsStr = res.sqlColumns;
|
|
581
|
+
columns = res.columns;
|
|
582
|
+
}
|
|
583
|
+
sql += "\nRETURNING " + columnsStr;
|
|
584
|
+
return new SqlQueryStatement(sql, columns);
|
|
585
|
+
}
|
|
586
|
+
function genWhere(where) {
|
|
587
|
+
return "\nWHERE " + where;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
export { DbTable, DbTableQuery, PgSqlValue, SqlQueryStatement, SqlRaw, SqlSelectable, SqlValuesCreator, createSelect };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SqlValuesCreator, SqlRaw } from "../sql_value/sql_value.ts";
|
|
2
|
+
import { ColumnsSelected, ColumnsSelectAs, SelectColumns, UpdateRowValue, TableType } from "./type.ts";
|
|
3
|
+
import { Select, SelectTableOption } from "./select.ts";
|
|
4
|
+
import { DbTable, SqlQueryStatement } from "./selectable.ts";
|
|
5
|
+
/** @public */
|
|
6
|
+
export declare class DbTableQuery<T extends TableType = Record<string, any>, C extends TableType = Partial<T>> extends DbTable<T> {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
private statement;
|
|
9
|
+
constructor(name: string, columns: readonly string[], statement: SqlValuesCreator);
|
|
10
|
+
/** 选择全部列 */
|
|
11
|
+
select(columns?: undefined, option?: SelectTableOption): Select<{}>;
|
|
12
|
+
/** 选择全部列 */
|
|
13
|
+
select(columns: "*", option?: SelectTableOption): Select<T>;
|
|
14
|
+
/** 选择表中的列并重命名 */
|
|
15
|
+
select<R extends ColumnsSelectAs<T>>(columns: R, option?: SelectTableOption): Select<SelectColumns<T, R>>;
|
|
16
|
+
insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
|
|
17
|
+
insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
|
|
18
|
+
update(values: UpdateRowValue<T>, option?: UpdateOption): string;
|
|
19
|
+
updateWithResult<R extends ColumnsSelected<T>>(values: UpdateRowValue<T>, returns: R, option?: UpdateOption): SqlQueryStatement<SelectColumns<T, R>>;
|
|
20
|
+
delete(option?: DeleteOption): string;
|
|
21
|
+
deleteWithResult<R extends ColumnsSelected<T>>(returns?: ColumnsSelected<T> | "*", option?: DeleteOption): SqlQueryStatement<SelectColumns<T, R>>;
|
|
22
|
+
}
|
|
23
|
+
/** @public */
|
|
24
|
+
export interface InsertOption<T extends object> {
|
|
25
|
+
conflict?: (keyof T)[];
|
|
26
|
+
updateValues?: {
|
|
27
|
+
[key in keyof T]?: undefined | SqlRaw | T[key];
|
|
28
|
+
};
|
|
29
|
+
where?: string;
|
|
30
|
+
}
|
|
31
|
+
/** @public */
|
|
32
|
+
export interface UpdateOption {
|
|
33
|
+
where?: string;
|
|
34
|
+
}
|
|
35
|
+
/** @public */
|
|
36
|
+
export interface DeleteOption {
|
|
37
|
+
where?: string;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=TableQuery.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ColumnsSelectAs, ColumnsSelected, RowsOrder } from "./type.ts";
|
|
2
|
+
export declare function genOderBy(orderBy: RowsOrder<any>, orderNullRule?: "FIRST" | "LAST"): string;
|
|
3
|
+
export declare function selectColumnsOrTable(columns: ColumnsSelectAs<any> | string[]): {
|
|
4
|
+
columns: string[];
|
|
5
|
+
sqlColumns: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* @param select 选择的行
|
|
9
|
+
* @param tableColumns 全部行
|
|
10
|
+
* @param push 要讲选择的行加入到集合中。
|
|
11
|
+
*/
|
|
12
|
+
export declare function genNewColumns(select: ColumnsSelected<any>, tableColumns: Iterable<string | number | symbol>,
|
|
13
|
+
/** newName -> oldName */
|
|
14
|
+
push?: Map<string, string | null>): Map<string, string | null>;
|
|
15
|
+
//# sourceMappingURL=_statement.d.ts.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { SqlSelectable, InferQueryResult, SqlQueryStatement } from "./selectable.ts";
|
|
2
|
+
import type { ColumnsSelected, RowsOrder, SelectColumns, TableType } from "./type.ts";
|
|
3
|
+
interface AddTableFn<T extends TableType> {
|
|
4
|
+
<Q extends SqlSelectable<any>, A extends InferQueryResult<Q>>(tb: Q, columns?: undefined, option?: SelectTableOption): Select<T>;
|
|
5
|
+
<Q extends SqlSelectable<any>, A extends InferQueryResult<Q>, C extends ColumnsSelected<A>>(tb: Q, columns: C, option?: SelectTableOption): Select<T & SelectColumns<A, C>>;
|
|
6
|
+
}
|
|
7
|
+
interface JoinTableFn<T extends TableType, ExtraOption extends object = {}> {
|
|
8
|
+
<Q extends SqlSelectable<any>, A extends InferQueryResult<Q>>(tb: Q, columns: undefined, option: ExtraOption & SelectTableOption): JoinSelect<T>;
|
|
9
|
+
<Q extends SqlSelectable<any>, A extends InferQueryResult<Q>, C extends ColumnsSelected<A>>(tb: Q, columns: C, option: ExtraOption & SelectTableOption): JoinSelect<T & SelectColumns<A, C>>;
|
|
10
|
+
}
|
|
11
|
+
/** @public */
|
|
12
|
+
export interface SelectFilterOption<T extends object> {
|
|
13
|
+
orderBy?: RowsOrder<T>;
|
|
14
|
+
orderNullRule?: "FIRST" | "LAST";
|
|
15
|
+
where?: string;
|
|
16
|
+
offset?: number;
|
|
17
|
+
limit?: number;
|
|
18
|
+
}
|
|
19
|
+
/** @public */
|
|
20
|
+
export interface FinalSelect<T extends TableType> extends SqlSelectable<T> {
|
|
21
|
+
toQuery(option?: SelectFilterOption<T>): SqlQueryStatement<T>;
|
|
22
|
+
}
|
|
23
|
+
/** @public */
|
|
24
|
+
export interface JoinSelect<T extends TableType> extends FinalSelect<T> {
|
|
25
|
+
addColumns<A extends TableType>(add: {
|
|
26
|
+
[key in keyof A]: string;
|
|
27
|
+
}): FinalSelect<T & A>;
|
|
28
|
+
crossJoin: JoinTableFn<T>;
|
|
29
|
+
naturalJoin: JoinTableFn<T>;
|
|
30
|
+
innerJoin: JoinTableFn<T, {
|
|
31
|
+
on: string;
|
|
32
|
+
}>;
|
|
33
|
+
leftJoin: JoinTableFn<T, {
|
|
34
|
+
on: string;
|
|
35
|
+
}>;
|
|
36
|
+
rightJoin: JoinTableFn<T, {
|
|
37
|
+
on: string;
|
|
38
|
+
}>;
|
|
39
|
+
fullJoin: JoinTableFn<T, {
|
|
40
|
+
on: string;
|
|
41
|
+
}>;
|
|
42
|
+
}
|
|
43
|
+
/** @public */
|
|
44
|
+
export interface Select<T extends TableType> extends JoinSelect<T> {
|
|
45
|
+
/** 选择表 */
|
|
46
|
+
select: AddTableFn<T>;
|
|
47
|
+
}
|
|
48
|
+
/** @public */
|
|
49
|
+
export declare const createSelect: AddTableFn<{}>;
|
|
50
|
+
/** @public */
|
|
51
|
+
export interface SelectTableOption {
|
|
52
|
+
tableAs?: string;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=select.d.ts.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { TableType } from "./type.ts";
|
|
2
|
+
declare const SQL_SELECTABLE: unique symbol;
|
|
3
|
+
/** @public */
|
|
4
|
+
export declare abstract class SqlSelectable<T extends TableType> {
|
|
5
|
+
abstract readonly columns: Iterable<string>;
|
|
6
|
+
/** select from xxx 中的 xxx */
|
|
7
|
+
abstract toSelect(): string;
|
|
8
|
+
abstract toString(): string;
|
|
9
|
+
/** 保留以推断类型 */
|
|
10
|
+
[SQL_SELECTABLE]: (item: T) => never;
|
|
11
|
+
}
|
|
12
|
+
/** @public */
|
|
13
|
+
export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly columns: readonly string[];
|
|
16
|
+
constructor(name: string, columns: readonly (keyof T)[]);
|
|
17
|
+
toSelect(): string;
|
|
18
|
+
toString(): string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* SELECT、UPDATE、DELETE、INSERT INTO 带结果的返回值
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export declare class SqlQueryStatement<T extends TableType = TableType> extends SqlSelectable<T> {
|
|
25
|
+
private sql;
|
|
26
|
+
readonly columns: readonly string[];
|
|
27
|
+
constructor(sql: string, columns: readonly string[]);
|
|
28
|
+
toString(): string;
|
|
29
|
+
toSelect(): string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 推断查询结果的类型
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export type InferQueryResult<T> = T extends SqlSelectable<infer P> ? (P extends TableType ? P : never) : never;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=selectable.d.ts.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { SqlRaw } from "../sql_value/sql_value.ts";
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
* @param T - 表格查询类型
|
|
5
|
+
* @param Rq - 默认选择
|
|
6
|
+
* @param Pa - 可选选择
|
|
7
|
+
*/
|
|
8
|
+
export type PickColumn<T extends {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}, Rq extends keyof T = keyof T, Pa extends Exclude<keyof T, Rq> = never> = {
|
|
11
|
+
[key in Rq as null extends T[key] ? key : never]?: T[key];
|
|
12
|
+
} & {
|
|
13
|
+
[key in Rq as null extends T[key] ? never : key]: T[key];
|
|
14
|
+
} & {
|
|
15
|
+
[key in Pa]?: T[key];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 定义行的排序
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export type RowsOrder<T extends object> = {
|
|
22
|
+
[key in keyof T]?: OrderValue;
|
|
23
|
+
};
|
|
24
|
+
/** @public */
|
|
25
|
+
export type UpdateRowValue<T extends object> = {
|
|
26
|
+
[key in keyof T]?: T[key] | SqlRaw;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* 选择列并重命名
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export type ColumnsSelectAs<T extends TableType> = {
|
|
33
|
+
[key in keyof T]?: boolean | string;
|
|
34
|
+
};
|
|
35
|
+
/** @public */
|
|
36
|
+
export type OrderValue = "ASC" | "DESC";
|
|
37
|
+
/**
|
|
38
|
+
* 表的选择参数
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export type ColumnsSelected<T extends TableType> = ColumnsSelectAs<T> | "*";
|
|
42
|
+
/**
|
|
43
|
+
* 从一个表格选择列,生成新的表格类型
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export type SelectColumns<T extends TableType, R extends ColumnsSelected<T>> = R extends "*" ? T : R extends ColumnsSelectAs<T> ? {
|
|
47
|
+
[key in keyof T as R[key] extends true ? key : StringOnly<R[key]>]: T[key];
|
|
48
|
+
} : never;
|
|
49
|
+
type StringOnly<T> = T extends string ? T : never;
|
|
50
|
+
/** @public */
|
|
51
|
+
export type TableType = {
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
};
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SqlValuesCreator } from "./sql_value.ts";
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
* PgSql的值转换
|
|
5
|
+
*/
|
|
6
|
+
export declare class PgSqlValue extends SqlValuesCreator {
|
|
7
|
+
constructor();
|
|
8
|
+
timestamp(value: Date): string;
|
|
9
|
+
array(value: any[]): string;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=db_type.d.ts.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
declare const SQL_RAW: unique symbol;
|
|
2
|
+
/**
|
|
3
|
+
* SQL 原始字符对象
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export declare class SqlRaw<T = any> {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(value: string);
|
|
9
|
+
toString(): string;
|
|
10
|
+
/** 保留以推断类型 */
|
|
11
|
+
[SQL_RAW]: (item: T) => never;
|
|
12
|
+
}
|
|
13
|
+
/** @public */
|
|
14
|
+
export type JsObjectMapSql = Map<new (...args: any[]) => any, (value: object) => string>;
|
|
15
|
+
/**
|
|
16
|
+
* SQL value 生成器
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export declare class SqlValuesCreator {
|
|
20
|
+
/**
|
|
21
|
+
* 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
|
|
22
|
+
* @example 输入 a'b'c , 返回 a''b''c.
|
|
23
|
+
*/
|
|
24
|
+
static string(value: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* @param map - 自定义对象转换
|
|
27
|
+
*/
|
|
28
|
+
constructor(map?: JsObjectMapSql);
|
|
29
|
+
private readonly map;
|
|
30
|
+
string(value: string): string;
|
|
31
|
+
number(value: number | bigint): string;
|
|
32
|
+
/** 将 JS 对象转为 SQL 的字符值的形式 */
|
|
33
|
+
toSqlStr(value: any): string;
|
|
34
|
+
/**
|
|
35
|
+
* 将对象列表转为 SQL 的 VALUES
|
|
36
|
+
* @example 返回示例: " (...),(...) "
|
|
37
|
+
*/
|
|
38
|
+
objectListToValuesList<T extends object>(objectList: T[], keys?: (keyof T)[]): string;
|
|
39
|
+
/**
|
|
40
|
+
* 将对象转为 SQL 的 value
|
|
41
|
+
* @example 返回示例: " 'abc', '6', 'now()' "
|
|
42
|
+
*/
|
|
43
|
+
objectToValues(object: object, keys: readonly string[]): string;
|
|
44
|
+
/**
|
|
45
|
+
* 将数组列表转为 SQL 的一个 value
|
|
46
|
+
* @example 返回示例: " 'abc', '6', 'now()' "
|
|
47
|
+
*/
|
|
48
|
+
toValues(values: readonly any[]): string;
|
|
49
|
+
}
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=sql_value.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@asla/yoursql",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "./dist/mod.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"ci:test": "vitest run",
|
|
9
|
+
"ci:build": "pnpm rollup -c build/rollup.config.js",
|
|
10
|
+
"build": "pnpm rollup -c build/rollup.config.js && pnpm run ci:check-api -l",
|
|
11
|
+
"test": "vitest",
|
|
12
|
+
"type:check": "tsc",
|
|
13
|
+
"ci:check-api": "api-extractor run -c api-extractor.jsonc -v"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@microsoft/api-extractor": "^7.47.9",
|
|
17
|
+
"@rollup/plugin-typescript": "^12.1.0",
|
|
18
|
+
"rollup": "^4.22.4",
|
|
19
|
+
"tslib": "^2.7.0",
|
|
20
|
+
"typescritp": "^1.0.0",
|
|
21
|
+
"vitest": "^2.1.1"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/asnowc/yoursql"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/mod.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist/**/*.js",
|
|
32
|
+
"dist/**/*d.ts"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public",
|
|
36
|
+
"registry": "https://registry.npmjs.org",
|
|
37
|
+
"provenance": true
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"sql"
|
|
41
|
+
],
|
|
42
|
+
"author": {
|
|
43
|
+
"name": "Eavid",
|
|
44
|
+
"url": "https://github.com/eavidy"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18"
|
|
48
|
+
},
|
|
49
|
+
"license": "MIT"
|
|
50
|
+
}
|