Skip to content

Commit 181b06e

Browse files
author
fernandocode
committed
#1387 - Adicionada possibilidade de informar em um where uma coluna para um alias da projection onde ele não aplique o alias padrão da tabela
1 parent 2bb45df commit 181b06e

9 files changed

Lines changed: 49 additions & 18 deletions

File tree

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.4.8",
3+
"version": "0.4.9",
44
"description": "Library to assist in creating and maintaining SQL commands.",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",

src/core/column-params.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ValueTypeToParse } from "./utils";
2+
import { Resultable } from "./resultable";
23

34
export interface ColumnParams {
4-
column: string;
5+
column: string | Resultable;
56
params: ValueTypeToParse[];
67
}

src/core/column-ref.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { Resultable } from "./resultable";
12

2-
export class ColumnRef {
3+
export class ColumnRef implements Resultable {
34
constructor(
45
public column?: string,
56
public alias?: string

src/core/plan-ref.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { Resultable } from "./resultable";
12

2-
export class PlanRef {
3+
export class PlanRef implements Resultable {
34
constructor(
45
public value?: string
56
) {

src/core/resultable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Resultable {
2+
result(): string;
3+
}

src/core/utils.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { MetadataTableBase } from "../metadata-table-base";
2424
import { ProjectionCompile } from "../crud/projection-compile";
2525
import * as lodash from "lodash";
2626
import { QueryCompiled } from ".";
27+
import { Resultable } from "./resultable";
2728

2829
export type ParamType = ValueType | ReplacementParam;
2930

@@ -169,6 +170,13 @@ export class Utils {
169170
return instance instanceof PlanRef;
170171
}
171172

173+
public static isResultable(instance: any): instance is Resultable {
174+
return instance
175+
&& this.isObject(instance)
176+
&& "result" in instance
177+
&& this.isFunction(instance.result);
178+
}
179+
172180
public static isEmpty(value: any): boolean {
173181
if (this.isBoolean(value) || this.isDate(value)) {
174182
return this.isNull(value);
@@ -247,13 +255,14 @@ export class Utils {
247255
params: []
248256
};
249257
case (ExpressionOrValueEnum.Ref):
250-
return {
251-
column: (expression as ColumnRef).result(),
252-
params: []
253-
};
258+
// return {
259+
// column: (expression as ColumnRef).result(),
260+
// params: []
261+
// };
254262
case (ExpressionOrValueEnum.Plan):
255263
return {
256-
column: (expression as PlanRef).result(),
264+
// column: (expression as PlanRef).result(),
265+
column: expression as Resultable,
257266
params: []
258267
};
259268
case (ExpressionOrValueEnum.Value):
@@ -381,12 +390,15 @@ export class Utils {
381390
}
382391

383392
public static addAlias(
384-
column: string, alias: string
393+
column: string | Resultable, alias: string
385394
): string {
386-
if (column && alias && Utils.isNameColumn(column)) {
395+
if (this.isResultable(column)) {
396+
return (column as Resultable).result();
397+
}
398+
if (column && alias && Utils.isNameColumn(column as string)) {
387399
return `${alias}.${column}`;
388400
}
389-
return column;
401+
return column as string;
390402
}
391403

392404
private static isColumnReservedNameOrNotAllowed(columnName: string): boolean {

src/crud/where-base-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ export abstract class WhereBaseBuilder<
455455
params: []
456456
} as ColumnParams;
457457
param.forEach((value) => {
458-
result.column += result.column.length > 0 ? ", ?" : "?";
458+
result.column += (result.column as string).length > 0 ? ", ?" : "?";
459459
result.params.push(value);
460460
});
461461
return result;

src/test/executable-builder.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ describe("ExecutableBuilder", () => {
99

1010
beforeEach(async () => {
1111
database = await new SQLiteDatabase().init();
12-
console.log("database", database);
1312
});
1413

1514
it("build sql batch", () => {
1615

17-
const execute = new ExecutableBuilder(true);
16+
const execute = new ExecutableBuilder(false);
1817
const compiled = [
1918
{
2019
query: "DROP TABLE IF EXISTS MyTable",
@@ -53,7 +52,7 @@ describe("ExecutableBuilder", () => {
5352
});
5453

5554
it("executeBatch", async () => {
56-
const execute = new ExecutableBuilder(true);
55+
const execute = new ExecutableBuilder(false);
5756
const compiled = [
5857
{
5958
query: "DROP TABLE IF EXISTS MyTable",
@@ -69,8 +68,6 @@ describe("ExecutableBuilder", () => {
6968
} as QueryCompiled
7069
];
7170
const result = await execute.executeBatch(compiled, database).toPromise();
72-
console.log("result", result.length, result);
73-
console.log("equal", result.length, compiled.length);
7471
expect(result.length).to.equal(compiled.length);
7572
expect(result[2].rowsAffected).to.equal(1);
7673
expect(result[2].insertId).to.equal(1);

src/test/where.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import * as moment from "moment";
66
import { Crud } from "../crud/crud";
77
import { getMapper } from "./mappers-table-new";
88
import { TestClazzRef } from "./models/test-clazz-ref";
9+
import { PlanRef } from "../core/plan-ref";
10+
import { ColumnRef } from "../core/column-ref";
911

1012
describe("Where", () => {
1113

@@ -632,4 +634,18 @@ describe("Where", () => {
632634
expect(result[0].query).to.equal("SELECT (tes.id || tes.description) AS iddescription FROM TestClazz AS tes WHERE (tes.id || tes.description) = ?");
633635
});
634636

637+
it("where in column alias by projection without alias table default", () => {
638+
const query = crud.query(TestClazz);
639+
query.projection(projection => {
640+
projection.add(x => x.id, "iiiiddd");
641+
});
642+
query.where(where => {
643+
where.equal(new PlanRef("iiiiddd"), x => x.id);
644+
where.equal(new ColumnRef("description", query.alias()), new ColumnRef("dateStr"));
645+
});
646+
const result = query.compile();
647+
expect(result[0].params.length).to.equal(0);
648+
expect(result[0].query).to.equal("SELECT tes.id AS iiiiddd FROM TestClazz AS tes WHERE iiiiddd = tes.id AND tes.description = dateStr");
649+
});
650+
635651
});

0 commit comments

Comments
 (0)