Skip to content

Commit 76cfcf7

Browse files
committed
v.0.0.24
Bug in GroupBy and OrderBy column string with function
1 parent 8fce81e commit 76cfcf7

7 files changed

Lines changed: 61 additions & 21 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.0.23",
3+
"version": "0.0.24",
44
"description": "Library to assist in creating and maintaining SQL commands.",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",

src/core/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ export class Utils {
260260
return param;
261261
}
262262

263+
public static addAlias(
264+
column: string, alias: string
265+
): string {
266+
if (column && alias && Utils.isNameColumn(column)) {
267+
return `${alias}.${column}`;
268+
}
269+
return column;
270+
}
271+
263272
private static isColumnReservedNameOrNotAllowed(columnName: string): boolean {
264273
return this.isStartWithNumber(columnName) || this.isReservedBoolean(columnName);
265274
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
8181
return Object.assign({ __proto__: (this._getInstance() as any).__proto__ }, this._getInstance());
8282
}
8383

84-
// public ref(expression: ExpressionOrColumn<T>): string {
85-
// return this.addAlias(Utils.getColumn(expression));
86-
// }
87-
8884
public ref(expression: ExpressionOrColumn<T>): ColumnRef {
8985
return new ColumnRef(
9086
Utils.getColumn(expression),
@@ -167,7 +163,7 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
167163
}
168164

169165
public orderBy(expression: ExpressionOrColumn<T>, order: OrderBy = OrderBy.ASC): TQuery {
170-
this.compileOrderBy(`${this.addAlias(Utils.getColumn(expression))} ${order}`);
166+
this.compileOrderBy(`${Utils.addAlias(Utils.getColumn(expression), this._alias)} ${order}`);
171167
return this._getInstance();
172168
}
173169

@@ -183,7 +179,7 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
183179
expression: ExpressionOrColumn<T>,
184180
havingCallback?: (having: HavingBuilder<T>, projection: ProjectionsHelper<T>) => void
185181
): TQuery {
186-
this.compileGroupBy(this.addAlias(Utils.getColumn(expression)));
182+
this.compileGroupBy(Utils.addAlias(Utils.getColumn(expression), this._alias));
187183
if (havingCallback) {
188184
const whereHaving = new HavingBuilder(this._typeT, "");
189185
havingCallback(whereHaving, new ProjectionsHelper(this._typeT, this._alias, false));
@@ -277,9 +273,10 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
277273
return queryBase;
278274
}
279275

280-
private addAlias(column: string): string {
281-
return `${this._alias}.${column}`;
282-
}
276+
// In Utils
277+
// private addAlias(column: string): string {
278+
// return `${this._alias}.${column}`;
279+
// }
283280

284281
private compileGroupBy(groupBy: string) {
285282
if (groupBy && groupBy.length) {

src/crud/where-base-builder.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ export abstract class WhereBaseBuilder<
375375
const columnLeft = this.processParam(left);
376376
this.buildWhereParams(
377377
condition,
378-
this.addAlias(columnRight.column),
379-
this.addAlias(columnLeft.column),
378+
Utils.addAlias(columnRight.column, this._alias),
379+
Utils.addAlias(columnLeft.column, this._alias),
380380
columnRight.params.concat(columnLeft.params)
381381
);
382382
}
@@ -403,14 +403,15 @@ export abstract class WhereBaseBuilder<
403403
this._where += this.createWhere(conditions, column1, column2);
404404
}
405405

406-
protected addAlias(
407-
column: string,
408-
): string {
409-
if (column && this._alias && Utils.isNameColumn(column)) {
410-
return `${this._alias}.${column}`;
411-
}
412-
return column;
413-
}
406+
// In Utils
407+
// protected addAlias(
408+
// column: string,
409+
// ): string {
410+
// if (column && this._alias && Utils.isNameColumn(column)) {
411+
// return `${this._alias}.${column}`;
412+
// }
413+
// return column;
414+
// }
414415

415416
protected addParam(
416417
param: ValueTypeToParse | ValueTypeToParse[],
@@ -460,7 +461,7 @@ export abstract class WhereBaseBuilder<
460461
this.addParam(metadata.right);
461462
metadata.right = "?";
462463
}
463-
this.buildWhere(metadata.condition, this.addAlias(metadata.left), this.addAlias(metadata.right));
464+
this.buildWhere(metadata.condition, Utils.addAlias(metadata.left, this._alias), Utils.addAlias(metadata.right, this._alias));
464465
}
465466

466467
private addValueParam(

src/test/group-by.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,15 @@ describe("Group By", () => {
8585
expect(result.query).to.equal("SELECT tes.* FROM TestClazz AS tes GROUP BY tes.id HAVING SUM(COUNT(tes.id)) > ? AND SUM(tes.referenceTest_id) <= MAX(COUNT(tes.id))");
8686
});
8787

88+
it("with string", () => {
89+
const query = new Query(TestClazz);
90+
query.projection(select => {
91+
select.add(`strftime('%m', datetime(${select.ref(x => x.date).result()}, 'unixepoch'))`, "month");
92+
});
93+
query.groupBy(`strftime('%m', datetime(${query.ref(x => x.date).result()}, 'unixepoch'))`);
94+
const result = query.compile();
95+
expect(result.params.length).to.equal(0);
96+
expect(result.query).to.equal("SELECT strftime('%m', datetime(tes.date, 'unixepoch')) AS month FROM TestClazz AS tes GROUP BY strftime('%m', datetime(tes.date, 'unixepoch'))");
97+
});
98+
8899
});

src/test/order-by.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,15 @@ describe("Order By", () => {
5656
expect(result.query).to.equal("SELECT tes.* FROM TestClazz AS tes ORDER BY tes.id DESC, tes.referenceTest_id ASC, tes.description DESC");
5757
});
5858

59+
it("with string", () => {
60+
const query = new Query(TestClazz);
61+
query.projection(select => {
62+
select.add(`strftime('%m', datetime(${select.ref(x => x.date).result()}, 'unixepoch'))`, "month");
63+
});
64+
query.asc(`strftime('%m', datetime(${query.ref(x => x.date).result()}, 'unixepoch'))`);
65+
const result = query.compile();
66+
expect(result.params.length).to.equal(0);
67+
expect(result.query).to.equal("SELECT strftime('%m', datetime(tes.date, 'unixepoch')) AS month FROM TestClazz AS tes ORDER BY strftime('%m', datetime(tes.date, 'unixepoch')) ASC");
68+
});
69+
5970
});

src/test/where.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ describe("Where", () => {
2424
expect(result.query).to.equal("SELECT tes.* FROM TestClazz AS tes WHERE tes.id = ?");
2525
});
2626

27+
it("column string", () => {
28+
const query = new Query(TestClazz);
29+
query.where(where => {
30+
where.equal(query.ref("id"), 2);
31+
});
32+
const result = query.compile();
33+
expect(result.params.length).to.equal(1);
34+
expect(result.params[0]).to.equal(2);
35+
expect(result.query).to.equal("SELECT tes.* FROM TestClazz AS tes WHERE tes.id = ?");
36+
});
37+
2738
it("multi", () => {
2839
const query = new Query(TestClazz);
2940
query.where(where => {

0 commit comments

Comments
 (0)