Skip to content

Commit 4bfc901

Browse files
committed
refactor: update
1 parent bfe97e0 commit 4bfc901

18 files changed

+3912
-228
lines changed

package-lock.json

Lines changed: 3769 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builder/JoinQueryBuilder.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {Join, JoinCondition, RawSQL} from './clauses';
22
import {ComparisonOperators, JoinQueryBuilderInterface} from './types';
33

44
export default class JoinQueryBuilder implements JoinQueryBuilderInterface {
5-
#joins: (Join | JoinCondition | RawSQL)[] = [];
5+
private joins: (Join | JoinCondition | RawSQL)[] = [];
66

77
on(
88
column1: string,
99
column2: string,
1010
comparisonOperator: ComparisonOperators = '=',
1111
quoteValue: boolean = false,
1212
) {
13-
this.#joins.push(
13+
this.joins.push(
1414
new JoinCondition(
1515
'ON',
1616
column1,
@@ -29,7 +29,7 @@ export default class JoinQueryBuilder implements JoinQueryBuilderInterface {
2929
comparisonOperator: ComparisonOperators = '=',
3030
quoteValue: boolean = true,
3131
) {
32-
this.#joins.push(
32+
this.joins.push(
3333
new JoinCondition(
3434
'AND',
3535
column1,
@@ -48,7 +48,7 @@ export default class JoinQueryBuilder implements JoinQueryBuilderInterface {
4848
comparisonOperator: ComparisonOperators = '=',
4949
quoteValue: boolean = true,
5050
) {
51-
this.#joins.push(
51+
this.joins.push(
5252
new JoinCondition(
5353
'OR',
5454
column1,
@@ -62,46 +62,46 @@ export default class JoinQueryBuilder implements JoinQueryBuilderInterface {
6262
}
6363

6464
leftJoin(table: string, alias?: string) {
65-
this.#joins.push(
65+
this.joins.push(
6666
new Join('LEFT', table, alias),
6767
);
6868

6969
return this;
7070
}
7171

7272
rightJoin(table: string, alias?: string) {
73-
this.#joins.push(
73+
this.joins.push(
7474
new Join('RIGHT', table, alias),
7575
);
7676

7777
return this;
7878
}
7979

8080
innerJoin(table: string, alias?: string) {
81-
this.#joins.push(
81+
this.joins.push(
8282
new Join('INNER', table, alias),
8383
);
8484

8585
return this;
8686
}
8787

8888
crossJoin(table: string, alias?: string) {
89-
this.#joins.push(
89+
this.joins.push(
9090
new Join('CROSS', table, alias),
9191
);
9292

9393
return this;
9494
}
9595

9696
joinRaw(sql: string, ...args: Array<string | number>) {
97-
this.#joins.push(
97+
this.joins.push(
9898
new RawSQL(sql, args),
9999
);
100100

101101
return this;
102102
}
103103

104104
getJoins() {
105-
return this.#joins;
105+
return this.joins;
106106
}
107107
}

src/builder/QueryBuilder.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './types';
1717

1818
export default class QueryBuilder implements QueryBuilderInterface {
19-
#compiler: QueryCompiler;
19+
private compiler: QueryCompiler;
2020

2121
protected query: Query = {
2222
type: null,
@@ -35,29 +35,22 @@ export default class QueryBuilder implements QueryBuilderInterface {
3535
};
3636

3737
constructor() {
38-
this.#compiler = new QueryCompiler(this.query);
38+
this.compiler = new QueryCompiler(this.query);
3939
}
4040

41-
from(table: string, alias?: string) {
41+
table(table: string, alias?: string) {
4242
this.query.table.push(
4343
new Table(table, alias),
4444
);
4545
return this;
4646
}
4747

48-
into(table: string) {
49-
this.query.table.push(
50-
new Table(table),
51-
);
52-
return this;
53-
}
54-
5548
select(...columns: (string | Column)[]) {
5649
this.setQueryType('SELECT');
5750

58-
columns.map(column => {
51+
columns.forEach(column => {
5952
if (column instanceof Object) {
60-
return Object.entries(column).map(([name, value]) => {
53+
Object.entries(column).map(([name, value]) => {
6154
// subquery
6255
if (value instanceof Function) {
6356
this.query.select.push(
@@ -71,11 +64,11 @@ export default class QueryBuilder implements QueryBuilderInterface {
7164
);
7265
}
7366
});
67+
} else {
68+
this.query.select.push(
69+
new Select(column),
70+
);
7471
}
75-
76-
this.query.select.push(
77-
new Select(column),
78-
);
7972
});
8073

8174
return this;
@@ -157,8 +150,8 @@ export default class QueryBuilder implements QueryBuilderInterface {
157150
const builder = new QueryBuilder();
158151
column(builder);
159152

160-
const sql = (this.query.where.length > 0 ? this.#compiler.getOperator(logicalOperator) : '')
161-
+ `(${builder.#compiler.compileWere(true)})`;
153+
const sql = (this.query.where.length > 0 ? this.compiler.getOperator(logicalOperator) : '')
154+
+ `(${builder.compiler.compileWere(true)})`;
162155

163156
this.query.where.push(sql);
164157
}
@@ -168,7 +161,7 @@ export default class QueryBuilder implements QueryBuilderInterface {
168161

169162
value(builder);
170163

171-
const sql = (this.query.where.length > 0 ? this.#compiler.getOperator(logicalOperator) : '')
164+
const sql = (this.query.where.length > 0 ? this.compiler.getOperator(logicalOperator) : '')
172165
+ `${column} ${comparisonOperator} (${builder.getSQL()})`;
173166

174167
this.query.where.push(sql);
@@ -537,6 +530,6 @@ export default class QueryBuilder implements QueryBuilderInterface {
537530
}
538531

539532
getSQL() {
540-
return this.#compiler.compile();
533+
return this.compiler.compile();
541534
}
542535
}

src/builder/QueryCompiler.ts

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import {escapeString} from '../util/string';
22
import {LogicalOperators, Query} from './types';
3-
import {Join, RawSQL, SelectBuilder, Where} from './clauses';
3+
import {Join, RawSQL, SelectBuilder, Table, Where} from './clauses';
44
import JoinQueryBuilder from './JoinQueryBuilder';
5-
import QueryBuilder from "./QueryBuilder";
5+
import QueryBuilder from './QueryBuilder';
66

77
export class QueryCompiler {
8-
#query: Query;
8+
private query: Query;
99

1010
constructor(query: Query) {
11-
this.#query = query;
11+
this.query = query;
1212
}
1313

1414
compileSelect() {
1515
const statements: string[] = [];
1616
let includeSelectKeyword: boolean = true;
1717

18-
this.#query.select
18+
this.query.select
1919
.forEach((select, i) => {
2020
if (select instanceof RawSQL) {
2121
if (i === 0) {
@@ -42,7 +42,7 @@ export class QueryCompiler {
4242
: '*';
4343

4444
if (includeSelectKeyword) {
45-
return 'SELECT ' + (this.#query.distinct ? 'DISTINCT ' : '') + compiled;
45+
return 'SELECT ' + (this.query.distinct ? 'DISTINCT ' : '') + compiled;
4646
}
4747

4848
return compiled;
@@ -51,49 +51,38 @@ export class QueryCompiler {
5151
compileFrom() {
5252
const clauses: string[] = [];
5353

54-
this.#query.table
54+
this.query.table
5555
.forEach((table) => {
5656
if (table instanceof RawSQL) {
5757
return clauses.push(table.sql);
5858
}
5959

6060
if (table.alias) {
61-
return clauses.push(`${table.table} AS ${table.alias}`);
61+
return clauses.push(`${table.name} AS ${table.alias}`);
6262
}
6363

64-
clauses.push(table.table);
64+
clauses.push(table.name);
6565
});
6666

6767
return 'FROM ' + clauses.join(', ');
6868
}
6969

7070
compileInto() {
71-
const tables: string[] = [];
71+
const table: Table | RawSQL = this.query.table[0];
72+
const tableName = table instanceof RawSQL ? table.sql : table.name;
7273

73-
this.#query.table
74-
.every((table) => {
75-
if (table instanceof RawSQL) {
76-
tables.push(table.sql);
77-
return false;
78-
}
79-
80-
tables.push(table.table);
81-
82-
return false;
83-
});
84-
85-
return 'INSERT INTO ' + tables[0];
74+
return 'INSERT INTO ' + tableName;
8675
}
8776

8877
compileWere(nestedStatement: boolean = false) {
8978
const clauses: string[] = [];
9079
let includeWhereKeyword: boolean = true;
9180

92-
if (!this.#query.where.length) {
81+
if (!this.query.where.length) {
9382
return null;
9483
}
9584

96-
this.#query.where
85+
this.query.where
9786
.forEach((where, i) => {
9887
if (where instanceof RawSQL) {
9988
if (i === 0) {
@@ -131,7 +120,7 @@ export class QueryCompiler {
131120
switch (comparison) {
132121
case 'LIKE':
133122
case 'NOT LIKE':
134-
return (typeof value === "string")
123+
return (typeof value === 'string')
135124
? this.getOperator(logical) + `${column} ${comparison} ${escapeString(value.includes('%') ? value : `%${value}%`)}`
136125
: '';
137126

@@ -155,11 +144,11 @@ export class QueryCompiler {
155144
compileJoin() {
156145
const clauses: string[] = [];
157146

158-
if (!this.#query.join.length) {
147+
if (!this.query.join.length) {
159148
return null;
160149
}
161150

162-
this.#query.join
151+
this.query.join
163152
.forEach((callback) => {
164153
if (callback instanceof RawSQL) {
165154
return clauses.push(callback.sql);
@@ -203,11 +192,11 @@ export class QueryCompiler {
203192
const clauses: string[] = [];
204193
let includeHavingKeyword: boolean = true;
205194

206-
if (!this.#query.having.length) {
195+
if (!this.query.having.length) {
207196
return null;
208197
}
209198

210-
this.#query.having
199+
this.query.having
211200
.forEach((having, i) => {
212201
if (having instanceof RawSQL) {
213202
if (i === 0) {
@@ -239,19 +228,19 @@ export class QueryCompiler {
239228
}
240229

241230
compileGroupBy() {
242-
return this.#query.groupBy.length
243-
? 'GROUP BY ' + this.#query.groupBy.join(', ')
231+
return this.query.groupBy.length
232+
? 'GROUP BY ' + this.query.groupBy.join(', ')
244233
: null;
245234
}
246235

247236
compileOrderBy() {
248237
const statements: string[] = [];
249238

250-
if (!this.#query.orderBy.length) {
239+
if (!this.query.orderBy.length) {
251240
return null;
252241
}
253242

254-
this.#query.orderBy
243+
this.query.orderBy
255244
.forEach((order) => {
256245
statements.push(
257246
`${order.column} ${order.direction}`,
@@ -262,25 +251,25 @@ export class QueryCompiler {
262251
}
263252

264253
compileLimit() {
265-
return this.#query.limit
266-
? 'LIMIT ' + this.#query.limit
254+
return this.query.limit
255+
? 'LIMIT ' + this.query.limit
267256
: null;
268257
}
269258

270259
compileOffset() {
271-
return this.#query.limit && this.#query.offset
272-
? 'OFFSET ' + this.#query.offset
260+
return this.query.limit && this.query.offset
261+
? 'OFFSET ' + this.query.offset
273262
: null;
274263
}
275264

276265
compileUnion() {
277266
const unions: string[] = [];
278267

279-
if (!this.#query.union.length) {
268+
if (!this.query.union.length) {
280269
return null;
281270
}
282271

283-
this.#query.union
272+
this.query.union
284273
.forEach((union) => {
285274
unions.push(
286275
(union.all ? 'UNION ALL ' : 'UNION ') + union.builder.getSQL(),
@@ -335,7 +324,7 @@ export class QueryCompiler {
335324
}
336325

337326
getQueryType(): string {
338-
return this.#query.type || 'SELECT';
327+
return this.query.type || 'SELECT';
339328
}
340329

341330
getOperator(operator: LogicalOperators | null): string {

src/builder/clauses/Table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export class Table {
2-
table: string;
2+
name: string;
33
alias?: string;
44

55
constructor(table: string, alias?: string) {
6-
this.table = table.trim();
6+
this.name = table.trim();
77
this.alias = alias?.trim();
88
}
99
}

src/builder/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export type Query = {
7373
}
7474

7575
interface QueryBuilderInterface {
76-
from(table: string, alias?: string): QueryBuilder;
76+
table(table: string, alias?: string): QueryBuilder;
7777

7878
select(...columns: (string | ColumnAlias)[]): QueryBuilder;
7979

0 commit comments

Comments
 (0)