Skip to content

Commit 0b80094

Browse files
committed
v.0.1.0-beta.16
1 parent 13fd40d commit 0b80094

47 files changed

Lines changed: 909 additions & 599 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "database-builder",
3-
"version": "0.1.0-beta.15",
3+
"version": "0.1.0-beta.16",
44
"description": "Library to assist in creating and maintaining SQL commands.",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",

src/core/builder-compiled.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ValueType } from "./utils";
1+
import { ParamType } from "./utils";
22

33
export class BuilderCompiled {
44

55
constructor(
66
public builder: string = "",
7-
public params: ValueType[] = []) {
7+
public params: ParamType[] = []) {
88
}
99
}

src/core/columns-values-builder.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import { DatabaseBuilderError } from "./errors";
22
import { KeyUtils } from "./key-utils";
33
import { PrimaryKeyType } from "./enums/primary-key-type";
4-
import { MetadataTable } from "../metadata-table";
54
import { ExpressionOrColumn, Utils, ValueTypeToParse } from "./utils";
65
import { ColumnsBaseBuilder } from "./columns-base-builder";
76
import { Column } from "./column";
87
import { FieldType } from "./enums/field-type";
98
import { ColumnsCompiled } from "./columns-compiled";
9+
import { MapperTable } from "../mapper-table";
1010

1111
export abstract class ColumnsValuesBuilder<
1212
T, TThis extends ColumnsValuesBuilder<T, TThis>>
1313
extends ColumnsBaseBuilder<TThis, T, Column> {
1414

1515
// TODO: fixed list task
1616
constructor(
17-
metadata: MetadataTable<T>,
18-
modelToSave: T = metadata.instance,
17+
// metadata: MetadataTable<T>,
18+
mapperTable: MapperTable,
19+
modelToSave: T,
20+
// modelToSave: T = metadata.instance,
1921
// modelToSave: T = void 0,
2022
) {
21-
super(metadata.mapperTable, modelToSave);
23+
super(mapperTable, modelToSave);
2224
// super(metadata, modelToSave);
2325
}
2426

src/core/crud-compiled.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ValueType } from "./utils";
1+
import { ParamType } from "./utils";
22

33
export interface CrudCompiled {
44
sql: string;
5-
params: ValueType[];
5+
params: ParamType[];
66
}

src/core/executable-builder.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { DatabaseBaseTransaction } from './../definitions/database-definition';
1+
import { DatabaseBaseTransaction } from "./../definitions/database-definition";
22
import { DatabaseBase, DatabaseObject, DatabaseResult, DatabaseTransaction } from "../definitions/database-definition";
33
import { QueryCompiled } from "./query-compiled";
4+
import { ReplacementParam } from "./replacement-param";
45

56
export class ExecutableBuilder {
67

@@ -9,11 +10,12 @@ export class ExecutableBuilder {
910
}
1011

1112
public execute(
12-
compiled: QueryCompiled,
13+
compiled: QueryCompiled[],
1314
database: DatabaseBase,
14-
): Promise<DatabaseResult> {
15+
): Promise<DatabaseResult[]> {
1516
this.log(compiled);
16-
return this.executeSql(database, compiled);
17+
return this.executorLinked(compiled, [], database);
18+
// return this.executeSql(database, compiled);
1719
}
1820

1921
private executeSql(
@@ -39,6 +41,49 @@ export class ExecutableBuilder {
3941
});
4042
}
4143

44+
private checkParams(
45+
script: QueryCompiled, resultadosAnteriores: DatabaseResult[]
46+
): QueryCompiled {
47+
const paramsResult: any[] = [];
48+
script.params.forEach(param => {
49+
if (param instanceof ReplacementParam) {
50+
let value = resultadosAnteriores as any;
51+
param.properties.forEach(property => {
52+
value = value[property];
53+
});
54+
paramsResult.push(value);
55+
} else {
56+
paramsResult.push(param);
57+
}
58+
});
59+
script.params = paramsResult;
60+
return script;
61+
}
62+
63+
private executorLinked(
64+
compiled: QueryCompiled[],
65+
dataResultsApplied: DatabaseResult[],
66+
database: DatabaseBase
67+
): Promise<DatabaseResult[]> {
68+
return new Promise((resolve, reject) => {
69+
if (compiled && compiled.length > 0) {
70+
this.executeSql(database, this.checkParams(compiled[0], dataResultsApplied))
71+
.then(result => {
72+
// remove o item executado
73+
compiled.shift();
74+
this.executorLinked(compiled, dataResultsApplied.concat([result]), database)
75+
.then(res => {
76+
resolve([result].concat(res));
77+
})
78+
.catch((err: any) => reject(err));
79+
})
80+
.catch((err: any) => reject(err));
81+
} else {
82+
resolve([]);
83+
}
84+
});
85+
}
86+
4287
private log(log: any) {
4388
if (this.enableLog) {
4489
// tslint:disable-next-line

src/core/query-compilable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { QueryCompiled } from "./query-compiled";
22

33
export interface QueryCompilable {
4-
compile(): QueryCompiled;
4+
compile(): QueryCompiled[];
55
}

src/core/query-compiled.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ValueType } from "./utils";
1+
import { ParamType } from "./utils";
22

33
export interface QueryCompiled {
44
query: string;
5-
params: ValueType[];
5+
params: ParamType[];
66
}

src/core/replacement-param.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class ReplacementParam {
2+
3+
public properties: string[];
4+
5+
constructor(
6+
...properties: string[]
7+
) {
8+
this.properties = properties;
9+
}
10+
}

src/core/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import { ColumnParams } from "./column-params";
1717
import { ColumnRef } from "./column-ref";
1818
import { PlanRef } from "./plan-ref";
1919
import * as uuidv4 from "uuid/v4";
20+
import { ReplacementParam } from "./replacement-param";
21+
22+
export type ParamType = ValueType | ReplacementParam;
2023

2124
export type ValueType = number | string | boolean;
2225
export type ValueTypeToParse = ValueType | moment.Moment | Date | object;

src/crud/crud-base-builder.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ColumnsCompiled } from "../core/columns-compiled";
44
import { QueryCompiled } from "../core/query-compiled";
55
import { CrudCompiled } from "../core/crud-compiled";
66
import { WhereCompiled } from "./where-compiled";
7-
import { MetadataTable } from "../metadata-table";
7+
import { MapperTable } from "../mapper-table";
88

99
let NEXT_VALUE_ALIAS: number = 0;
1010

@@ -17,7 +17,8 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
1717

1818
constructor(
1919
protected readonly _typeT: new () => T,
20-
protected metadata: MetadataTable<T>,
20+
// protected metadata: MetadataTable<T>,
21+
protected mapperTable: MapperTable,
2122
protected readonly _alias: string = void 0,
2223
) {
2324
this._tablename = _typeT.name;
@@ -26,8 +27,12 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
2627
}
2728
}
2829

29-
public getMetadata(): MetadataTable<T> {
30-
return this.metadata;
30+
// public getMetadata(): MetadataTable<T> {
31+
// return this.metadata;
32+
// }
33+
34+
public getMapper(): MapperTable {
35+
return this.mapperTable;
3136
}
3237

3338
public hasAlias(alias: string): boolean {

0 commit comments

Comments
 (0)