Skip to content

Commit 3945151

Browse files
committed
- Add new features;
- Update Docs; - Add tests;
1 parent 7d96166 commit 3945151

18 files changed

Lines changed: 873 additions & 248 deletions

README.md

Lines changed: 100 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,87 @@ npm install --save database-builder
1212
```
1313
This will install the current stable version of `database-builder` in your `node_modules` directory and save the entry in `package.json`.
1414

15-
### Step 2: Usage Typescript (Angular 2+ example)
15+
### Step 2: Usage Typescript
1616

17-
#### Step 2.1: Usage Query<T>
17+
[In StackBlitz](https://stackblitz.com/edit/typescript-cfzt6q)
1818

1919
```ts
20-
import { Component } from '@angular/core';
2120
import { Query } from 'database-builder';
21+
import { TestClazz, TestClazzRef } from './models';
2222

23-
@Component({
24-
selector: 'app-component',
25-
templateUrl: 'app.html'
26-
})
27-
export class AppComponent {
28-
29-
ngOnInit(){
30-
let query = new Query(TestClazz);
31-
query.
32-
}
33-
}
23+
let querySimple = new Query(TestClazz);
24+
print(querySimple.compile());
25+
/**
26+
* {
27+
* params: [],
28+
* query: "SELECT tes.* FROM TestClazz AS tes"
29+
* }
30+
*/
31+
32+
const queryWhere = new Query(TestClazz);
33+
queryWhere.where(where => {
34+
where.contains(x => x.description, "abc");
35+
where.greatValue(x => x.id, 1);
36+
});
37+
print(queryWhere.compile());
38+
/**
39+
* {
40+
* params: ["%abc%", 1],
41+
* query: "SELECT tes.* FROM TestClazz AS tes WHERE tes.description LIKE ? AND tes.id > ?"
42+
* }
43+
*/
44+
45+
const queryProjections = new Query(TestClazz);
46+
queryProjections.projection(projection => {
47+
projection.add(x => x.description);
48+
projection.sum(x => x.id);
49+
projection.max(x => x.referenceTest.id);
50+
projection.count(x => x.id, "countId");
51+
});
52+
print(queryProjections.compile());
53+
/**
54+
* {
55+
* params: [],
56+
* query: "SELECT tes.description AS description, SUM(tes.id) AS id, MAX(tes.referenceTest_id) AS referenceTest_id, COUNT(tes.id) AS countId FROM TestClazz AS tes"
57+
* }
58+
*/
59+
60+
const queryOrderBy = new Query(TestClazz);
61+
queryOrderBy.orderBy(x => x.id);
62+
print(queryOrderBy.compile());
63+
/**
64+
* {
65+
* params: [],
66+
* query: "SELECT tes.* FROM TestClazz AS tes ORDER BY tes.id ASC"
67+
* }
68+
*/
69+
70+
const queryGroupBy = new Query(TestClazz);
71+
queryGroupBy.groupBy(x => x.id, (having, projection) => {
72+
having.greatValue(projection.count(x => x.id), 10);
73+
});
74+
print(queryGroupBy.compile());
75+
/**
76+
* {
77+
* params: [10],
78+
* query: "SELECT tes.* FROM TestClazz AS tes GROUP BY tes.id HAVING COUNT(tes.id) > ?"
79+
* }
80+
*/
81+
82+
const queryLimitOffset = new Query(TestClazz);
83+
queryLimitOffset.limit(10/*, 5*/);
84+
print(queryLimitOffset.compile());
85+
/**
86+
* {
87+
* params: [10, 5],
88+
* query: "SELECT tes.* FROM TestClazz AS tes LIMIT ? OFFSET ?"
89+
* }
90+
*/
91+
```
92+
93+
models.ts
3494

95+
```ts
3596
export class TestClazz {
3697

3798
public description: string = "";
@@ -46,6 +107,30 @@ export class TestClazzRef{
46107
}
47108
```
48109

110+
### Usage Angular 2+
49111

112+
```ts
113+
import { Component } from '@angular/core';
114+
import { Query } from 'database-builder';
115+
import { TestClazz } from './models';
50116

51-
# TODO: Getting Started Ionic 2+
117+
@Component({
118+
selector: 'app-component',
119+
templateUrl: 'app.html'
120+
})
121+
export class AppComponent {
122+
123+
ngOnInit(){
124+
let query = new Query(TestClazz);
125+
const result = query.compile();
126+
console.log(result);
127+
/**
128+
* result:
129+
* {
130+
* params: [],
131+
* query: "SELECT tes.* FROM TestClazz AS tes"
132+
* }
133+
*/
134+
}
135+
}
136+
```

src/core/column-ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export class ColumnRef {
88
}
99

1010
public result(): string {
11-
return `${this.alias}.${this.column}`;
11+
if (this.alias) {
12+
return `${this.alias}.${this.column}`;
13+
}
14+
return this.column;
1215
}
1316
}

src/core/utils.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type ValueTypeToParse = ValueType | moment.Moment | Date | object;
2424

2525
export type ExpressionOrColumn<T> = Expression<T> | string;
2626

27-
export type ExpressionOrValue<T> = Expression<T> | ValueTypeToParse | ColumnRef | ProjectionsHelper<T>;
27+
export type TypeWhere<T> = Expression<T> | ValueTypeToParse | ColumnRef | ProjectionsHelper<T>;
2828

2929
export type TypeProjection<T> = ProjectionsHelper<T> | ColumnRef | PlanRef;
3030

@@ -39,6 +39,10 @@ export class Utils {
3939
return typeof value === type;
4040
}
4141

42+
public static isArray(value: any): boolean {
43+
return Array.isArray(value);
44+
}
45+
4246
public static isString(value: any): boolean {
4347
return this.is(value, "string");
4448
}
@@ -128,7 +132,7 @@ export class Utils {
128132
}
129133

130134
public static expressionOrValue<T>(
131-
value: ExpressionOrValue<T>
135+
value: TypeWhere<T>
132136
): ExpressionOrValueEnum {
133137
return this.isProjectionsHelper(value)
134138
? ExpressionOrValueEnum.Projection
@@ -149,7 +153,7 @@ export class Utils {
149153
}
150154
}
151155

152-
public static getColumnValue<T>(expression: ExpressionOrValue<T>): ColumnParams {
156+
public static getColumnWhere<T>(expression: TypeWhere<T>): ColumnParams {
153157
const type = this.expressionOrValue(expression);
154158
switch (type) {
155159
case (ExpressionOrValueEnum.Expression):
@@ -222,12 +226,11 @@ export class Utils {
222226
}
223227

224228
public static isValue(value: any): boolean {
225-
return !this.isNameColumn(value) &&
226-
(
227-
this.isValueNumber(value) || this.isString(value) || this.isValueBoolean(value)
228-
|| this.isDate(value)
229-
|| this.isMoment(value)
230-
);
229+
return this.isValueNumber(value)
230+
|| this.isString(value)
231+
|| this.isValueBoolean(value)
232+
|| this.isDate(value)
233+
|| this.isMoment(value);
231234
}
232235

233236
public static normalizeSqlString(inputSql: string): string {

src/crud/projection-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class ProjectionBuilder<T> {
4242
return new ProjectionsHelper(this._typeT, this._aliasTable, false);
4343
}
4444

45-
public col(column: string): ColumnRef {
45+
public ref(column: string): ColumnRef {
4646
return new ColumnRef(column, this._aliasTable);
4747
}
4848

src/crud/query/query-builder-base-contract.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export interface QueryBuilderBaseContract<T, TQuery extends QueryBuilderBaseCont
1313

1414
clone(): TQuery;
1515

16-
// ref(expression: ExpressionOrColumn<T>): string;
17-
ref2(expression: ExpressionOrColumn<T>): ColumnRef;
16+
ref(expression: ExpressionOrColumn<T>): ColumnRef;
1817

1918
hasAlias(alias: string): boolean;
2019

@@ -26,13 +25,9 @@ export interface QueryBuilderBaseContract<T, TQuery extends QueryBuilderBaseCont
2625

2726
whereExp(expression: LambdaExpression<T>): TQuery;
2827

29-
/**
30-
* @deprecated Use `select`
31-
* @param projectionCallback
32-
*/
3328
projection(projectionCallback: (projection: ProjectionBuilder<T>) => void): TQuery;
3429

35-
select(selectCallback: (select: ProjectionBuilder<T>) => void): TQuery;
30+
select(...expressions: Array<ExpressionOrColumn<T>>): TQuery;
3631

3732
orderBy(expression: ExpressionOrColumn<T>, order?: OrderBy): TQuery;
3833

src/crud/query/query-builder-base.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { HavingBuilder } from "../having-builder";
1717
import { ProjectionsHelper } from "../../core/projections-helper";
1818
import { BuilderCompiled } from "../../core/builder-compiled";
1919
import { ColumnRef } from "../../core/column-ref";
20+
import { QueryCompilable } from "../../core/query-compilable";
2021

2122
let NEXT_VALUE_ALIAS: number = 0;
2223

@@ -85,7 +86,7 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
8586
// return this.addAlias(Utils.getColumn(expression));
8687
// }
8788

88-
public ref2(expression: ExpressionOrColumn<T>): ColumnRef {
89+
public ref(expression: ExpressionOrColumn<T>): ColumnRef {
8990
return new ColumnRef(
9091
Utils.getColumn(expression),
9192
this.alias
@@ -108,9 +109,20 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
108109
return false;
109110
}
110111

111-
public from(query: QueryCompiled): TQuery {
112-
this._tablename = `(${query.query})`;
113-
this._fromParams = query.params;
112+
public from(query: QueryCompiled | QueryCompilable): TQuery {
113+
if ((query as QueryCompilable).compile) {
114+
return this.from((query as QueryCompilable).compile());
115+
}
116+
this._tablename = `(${(query as QueryCompiled).query})`;
117+
this._fromParams = (query as QueryCompiled).params;
118+
return this._getInstance();
119+
}
120+
121+
public union(query: QueryCompiled | QueryCompilable): TQuery {
122+
if ((query as QueryCompilable).compile) {
123+
return this.union((query as QueryCompilable).compile());
124+
}
125+
this._unionsQuery.push(query as QueryCompiled);
114126
return this._getInstance();
115127
}
116128

@@ -139,19 +151,20 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
139151
return this._getInstance();
140152
}
141153

142-
/**
143-
* @deprecated Use `select`
144-
* @param projectionCallback
145-
*/
146154
public projection(projectionCallback: (projection: ProjectionBuilder<T>) => void): TQuery {
147-
return this.select(projectionCallback);
148-
}
149-
150-
public select(selectCallback: (select: ProjectionBuilder<T>) => void): TQuery {
151155
const instanceProjection: ProjectionBuilder<T> = this.createProjectionBuilder();
152-
selectCallback(instanceProjection);
156+
projectionCallback(instanceProjection);
153157
this.compileProjection(instanceProjection.compile());
154158
return this._getInstance();
159+
// return this.select(projectionCallback);
160+
}
161+
162+
public select(...expressions: Array<ExpressionOrColumn<T>>): TQuery {
163+
return this.projection(projection => projection.columns(...expressions));
164+
// const instanceProjection: ProjectionBuilder<T> = this.createProjectionBuilder();
165+
// instanceProjection.columns(...expressions);
166+
// this.compileProjection(instanceProjection.compile());
167+
// return this._getInstance();
155168
}
156169

157170
public orderBy(expression: ExpressionOrColumn<T>, order: OrderBy = OrderBy.ASC): TQuery {
@@ -180,11 +193,6 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
180193
return this._getInstance();
181194
}
182195

183-
public union(query: QueryCompiled): TQuery {
184-
this._unionsQuery.push(query);
185-
return this._getInstance();
186-
}
187-
188196
public execute(database: Database): Promise<ResultExecuteSql> {
189197
return this._executableBuilder.execute(this.compile(), database);
190198
}

src/crud/query/query.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { QueryBuilder } from "./query-builder";
1111
import { ExpressionOrColumn, ValueType } from "./../../core/utils";
1212
import { QueryCompilable } from "./../../core/query-compilable";
1313
import { OrderBy } from "../../core/enums/order-by";
14-
// import { JoinQueryBuilder } from "./join-query-builder";
1514
import { JoinType } from "../enums/join-type";
1615
import { LambdaExpression } from "lambda-expression";
1716
import { JoinQueryBuilder } from "./join-query-builder";
@@ -43,19 +42,20 @@ export class Query<T> implements QueryCompilable {
4342
return this._queryBuilder.alias;
4443
}
4544

46-
// public ref(expression: ExpressionOrColumn<T>): string {
47-
// return this._queryBuilder.ref(expression);
48-
// }
49-
50-
public ref2(expression: ExpressionOrColumn<T>): ColumnRef {
51-
return this._queryBuilder.ref2(expression);
45+
public ref(expression: ExpressionOrColumn<T>): ColumnRef {
46+
return this._queryBuilder.ref(expression);
5247
}
5348

54-
public from(query: QueryCompiled): Query<T> {
49+
public from(query: QueryCompiled | QueryCompilable): Query<T> {
5550
this._queryBuilder.from(query);
5651
return this;
5752
}
5853

54+
public union(query: QueryCompiled | QueryCompilable): Query<T> {
55+
this._queryBuilder.union(query);
56+
return this;
57+
}
58+
5959
public join<TJoin>(
6060
typeTJoin: new () => TJoin,
6161
onWhere: (where: WhereBuilder<TJoin>) => void,
@@ -90,8 +90,8 @@ export class Query<T> implements QueryCompilable {
9090
return this;
9191
}
9292

93-
public select(selectCallback: (select: ProjectionBuilder<T>) => void): Query<T> {
94-
this._queryBuilder.select(selectCallback);
93+
public select(...expressions: Array<ExpressionOrColumn<T>>): Query<T> {
94+
this._queryBuilder.select(...expressions);
9595
return this;
9696
}
9797

0 commit comments

Comments
 (0)