-
Notifications
You must be signed in to change notification settings - Fork 15
/
where.ts
121 lines (101 loc) · 3.03 KB
/
where.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { replaceParams } from "./util.ts";
/**
* Where sub sql builder
*/
export class Where {
private expr: string;
private params: any[];
constructor(expr: string, params: any[]) {
this.expr = expr;
this.params = params;
}
get value(): string {
return this.toString();
}
toString(): string {
return replaceParams(this.expr, this.params);
}
static expr(expr: string, ...params: any[]): Where {
return new Where(expr, params);
}
static eq(field: string, value: any) {
return this.expr("?? = ?", field, value);
}
/**
* eq from object
* @param data
*/
static from(data: any): Where {
const conditions = Object.keys(data).map((key) => this.eq(key, data[key]));
return this.and(...conditions);
}
static gt(field: string, value: any) {
return this.expr("?? > ?", field, value);
}
static gte(field: string, value: any) {
return this.expr("?? >= ?", field, value);
}
static lt(field: string, value: any) {
return this.expr("?? < ?", field, value);
}
static lte(field: string, value: any) {
return this.expr("?? <= ?", field, value);
}
static ne(field: string, value: any) {
return this.expr("?? != ?", field, value);
}
static isNull(field: string) {
return this.expr("?? IS NULL", field);
}
static isNotNull(field: string) {
return this.expr("?? IS NOT NULL", field);
}
static in(field: string, ...values: any[]) {
const params: any[] = values.length > 1 ? values : values[0];
return this.expr("?? IN ?", field, params);
}
static notIn(field: string, ...values: any[]) {
const params: any[] = values.length > 1 ? values : values[0];
return this.expr("?? NOT IN ?", field, params);
}
static like(field: string, value: any) {
return this.expr("?? LIKE ?", field, value);
}
static between(field: string, startValue: any, endValue: any) {
return this.expr("?? BETWEEN ? AND ?", field, startValue, endValue);
}
static field(name: string) {
return {
gt: (value: any) => this.gt(name, value),
gte: (value: any) => this.gte(name, value),
lt: (value: any) => this.lt(name, value),
lte: (value: any) => this.lte(name, value),
ne: (value: any) => this.ne(name, value),
eq: (value: any) => this.eq(name, value),
isNull: () => this.isNull(name),
isNotNull: () => this.isNotNull(name),
in: (...values: any[]) => this.in(name, ...values),
notIn: (...values: any[]) => this.notIn(name, ...values),
like: (value: any) => this.like(name, value),
between: (start: any, end: any) => this.between(name, start, end),
};
}
static and(...expr: (null | undefined | Where)[]): Where {
const sql = `(${
expr
.filter((e) => e)
.map((e) => e!.value)
.join(" AND ")
})`;
return new Where(sql, []);
}
static or(...expr: (null | undefined | Where)[]): Where {
const sql = `(${
expr
.filter((e) => e)
.map((e) => e!.value)
.join(" OR ")
})`;
return new Where(sql, []);
}
}