@asla/yoursql 0.5.0 → 0.5.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/dist/mod.js +31 -22
- package/dist/select/TableQuery.d.ts +20 -5
- package/dist/select/select.d.ts +6 -6
- package/dist/util.d.ts +4 -4
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -62,35 +62,41 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
|
|
|
62
62
|
*
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
|
-
function where(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
function where(conditions, type) {
|
|
66
|
+
const sql = condition(conditions, type);
|
|
67
|
+
if (sql)
|
|
68
|
+
return "\nWHERE " + sql;
|
|
69
|
+
return "";
|
|
69
70
|
}
|
|
70
71
|
/**
|
|
71
72
|
*
|
|
72
73
|
* 生成 HAVING 语句
|
|
73
74
|
* @public
|
|
74
75
|
*/
|
|
75
|
-
function having(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
function having(conditions, type) {
|
|
77
|
+
const sql = condition(conditions, type);
|
|
78
|
+
if (sql)
|
|
79
|
+
return "\nHAVING " + sql;
|
|
80
|
+
return "";
|
|
79
81
|
}
|
|
80
|
-
function
|
|
81
|
-
if (typeof
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (typeof
|
|
86
|
-
|
|
82
|
+
function condition(conditions, type = "AND") {
|
|
83
|
+
if (typeof conditions === "function")
|
|
84
|
+
conditions = conditions();
|
|
85
|
+
if (!conditions)
|
|
86
|
+
return;
|
|
87
|
+
if (typeof conditions === "string")
|
|
88
|
+
return conditions;
|
|
87
89
|
else {
|
|
88
|
-
if (
|
|
89
|
-
sql
|
|
90
|
-
|
|
91
|
-
sql +=
|
|
90
|
+
if (conditions.length) {
|
|
91
|
+
let sql = "";
|
|
92
|
+
type = " " + type + " ";
|
|
93
|
+
sql += conditions[0];
|
|
94
|
+
for (let i = 1; i < conditions.length; i++)
|
|
95
|
+
sql += type + conditions[i];
|
|
96
|
+
return sql;
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
92
99
|
}
|
|
93
|
-
return sql;
|
|
94
100
|
}
|
|
95
101
|
/**
|
|
96
102
|
* @public
|
|
@@ -152,9 +158,9 @@ function orderBy(by) {
|
|
|
152
158
|
if (typeof by === "function")
|
|
153
159
|
by = by();
|
|
154
160
|
let sql = "";
|
|
161
|
+
if (!by)
|
|
162
|
+
return sql;
|
|
155
163
|
if (typeof by === "string") {
|
|
156
|
-
if (!by)
|
|
157
|
-
return sql;
|
|
158
164
|
sql += "\nORDER BY " + by;
|
|
159
165
|
}
|
|
160
166
|
else if (by instanceof Array) {
|
|
@@ -668,6 +674,9 @@ class DbTableQuery extends DbTable {
|
|
|
668
674
|
fromAs(as) {
|
|
669
675
|
return new Selection(this, as);
|
|
670
676
|
}
|
|
677
|
+
select(columns, as) {
|
|
678
|
+
return this.fromAs(as).select(columns);
|
|
679
|
+
}
|
|
671
680
|
insert(values, option) {
|
|
672
681
|
let insertCol;
|
|
673
682
|
let valuesStr;
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import { SqlValuesCreator, SqlRaw } from "../sql_value/sql_value.ts";
|
|
2
2
|
import { ColumnsSelected, SelectColumns, UpdateRowValue, TableType } from "./type.ts";
|
|
3
|
-
import { Selection } from "./select.ts";
|
|
3
|
+
import { CurrentWhere, Selection } from "./select.ts";
|
|
4
4
|
import { DbTable, SqlQueryStatement } from "./selectable.ts";
|
|
5
|
-
import {
|
|
5
|
+
import { ConditionParam } from "../util.ts";
|
|
6
6
|
/** @public */
|
|
7
7
|
export declare class DbTableQuery<T extends TableType = Record<string, any>, C extends TableType = Partial<T>> extends DbTable<T> {
|
|
8
8
|
private statement;
|
|
9
9
|
constructor(name: string, columns: readonly string[], statement: SqlValuesCreator);
|
|
10
10
|
fromAs(as?: string): Selection;
|
|
11
|
+
/** 选择单表全部列 */
|
|
12
|
+
select(columns: "*", as?: string): CurrentWhere<T>;
|
|
13
|
+
/**
|
|
14
|
+
* 选择单表
|
|
15
|
+
* @param columns - 对象选择
|
|
16
|
+
*/
|
|
17
|
+
select<R extends {
|
|
18
|
+
[key in keyof T]?: string | boolean;
|
|
19
|
+
} | Record<string, string>>(columns: R, as?: string): CurrentWhere<{
|
|
20
|
+
[key in keyof R]: R[key] extends boolean ? key extends keyof T ? T[key] : unknown : R[key] extends keyof T ? T[R[key]] : unknown;
|
|
21
|
+
}>;
|
|
22
|
+
/** 选择单表- 所有类型 */
|
|
23
|
+
select<R extends {}>(columns: "*" | string[] | {
|
|
24
|
+
[key in keyof R]?: key extends keyof T ? string | boolean : string;
|
|
25
|
+
}, as?: string): CurrentWhere<R>;
|
|
11
26
|
insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
|
|
12
27
|
insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
|
|
13
28
|
update(values: UpdateRowValue<T>, option?: UpdateOption): string;
|
|
@@ -21,14 +36,14 @@ export interface InsertOption<T extends object> {
|
|
|
21
36
|
updateValues?: {
|
|
22
37
|
[key in keyof T]?: undefined | SqlRaw | T[key];
|
|
23
38
|
};
|
|
24
|
-
where?:
|
|
39
|
+
where?: ConditionParam;
|
|
25
40
|
}
|
|
26
41
|
/** @public */
|
|
27
42
|
export interface UpdateOption {
|
|
28
|
-
where?:
|
|
43
|
+
where?: ConditionParam;
|
|
29
44
|
}
|
|
30
45
|
/** @public */
|
|
31
46
|
export interface DeleteOption {
|
|
32
|
-
where?:
|
|
47
|
+
where?: ConditionParam;
|
|
33
48
|
}
|
|
34
49
|
//# sourceMappingURL=TableQuery.d.ts.map
|
package/dist/select/select.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
|
|
2
|
-
import { OrderByParam,
|
|
2
|
+
import { OrderByParam, ConditionParam } from "../util.ts";
|
|
3
3
|
import type { TableType } from "./type.ts";
|
|
4
4
|
/** @public */
|
|
5
5
|
export interface CurrentLimit<T extends TableType> extends SqlQueryStatement<T> {
|
|
@@ -7,11 +7,11 @@ export interface CurrentLimit<T extends TableType> extends SqlQueryStatement<T>
|
|
|
7
7
|
}
|
|
8
8
|
/** @public */
|
|
9
9
|
export interface CurrentOrderBy<T extends TableType> extends CurrentLimit<T> {
|
|
10
|
-
orderBy(param: OrderByParam): CurrentLimit<T>;
|
|
10
|
+
orderBy(param: OrderByParam | (() => OrderByParam | void)): CurrentLimit<T>;
|
|
11
11
|
}
|
|
12
12
|
/** @public */
|
|
13
13
|
export interface CurrentHaving<T extends TableType> extends CurrentOrderBy<T> {
|
|
14
|
-
having(param:
|
|
14
|
+
having(param: ConditionParam | (() => ConditionParam | void)): CurrentLimit<T>;
|
|
15
15
|
}
|
|
16
16
|
/** @public */
|
|
17
17
|
export interface CurrentGroupBy<T extends TableType> extends CurrentOrderBy<T> {
|
|
@@ -19,7 +19,7 @@ export interface CurrentGroupBy<T extends TableType> extends CurrentOrderBy<T> {
|
|
|
19
19
|
}
|
|
20
20
|
/** @public */
|
|
21
21
|
export interface CurrentWhere<T extends TableType> extends CurrentGroupBy<T> {
|
|
22
|
-
where(param:
|
|
22
|
+
where(param: ConditionParam | (() => ConditionParam | void)): CurrentGroupBy<T>;
|
|
23
23
|
}
|
|
24
24
|
/** @public */
|
|
25
25
|
export declare class Selection {
|
|
@@ -35,9 +35,9 @@ export declare class Selection {
|
|
|
35
35
|
crossJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
|
|
36
36
|
from(selectable: SqlSelectable<any> | string, as?: string): Selection;
|
|
37
37
|
select<T extends TableType = TableType>(columns: "*" | string[]): CurrentWhere<T>;
|
|
38
|
-
select<T extends TableType
|
|
38
|
+
select<T extends TableType>(columns: {
|
|
39
39
|
[key in keyof T]: string | boolean;
|
|
40
40
|
}): CurrentWhere<T>;
|
|
41
|
-
select
|
|
41
|
+
select(columns: "*" | string[] | Record<string, string | boolean>): CurrentWhere<TableType>;
|
|
42
42
|
}
|
|
43
43
|
//# sourceMappingURL=select.d.ts.map
|
package/dist/util.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { OrderValue } from "./select/type.ts";
|
|
|
6
6
|
*/
|
|
7
7
|
export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): Set<string>;
|
|
8
8
|
/** @public */
|
|
9
|
-
export type
|
|
9
|
+
export type ConditionParam = string | string[];
|
|
10
10
|
/**
|
|
11
11
|
* 生成 WHERE 语句
|
|
12
12
|
* @public
|
|
@@ -14,13 +14,13 @@ export type WhereParam = string | string[];
|
|
|
14
14
|
*
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
export declare function where(
|
|
17
|
+
export declare function where(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
|
|
18
18
|
/**
|
|
19
19
|
*
|
|
20
20
|
* 生成 HAVING 语句
|
|
21
21
|
* @public
|
|
22
22
|
*/
|
|
23
|
-
export declare function having(
|
|
23
|
+
export declare function having(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
|
|
24
24
|
/**
|
|
25
25
|
* @public
|
|
26
26
|
* ```ts
|
|
@@ -54,5 +54,5 @@ export type OrderByParam = string | (string | OrderBehavior)[] | Record<string,
|
|
|
54
54
|
* orderBy({}) // ""
|
|
55
55
|
* ```
|
|
56
56
|
*/
|
|
57
|
-
export declare function orderBy(by
|
|
57
|
+
export declare function orderBy(by?: OrderByParam | void | (() => OrderByParam | void)): string;
|
|
58
58
|
//# sourceMappingURL=util.d.ts.map
|