Skip to content

Commit

Permalink
fix: selectRaw unnecessary wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lundibundi committed Oct 25, 2023
1 parent e4c1f19 commit 07c5635
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/select-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SelectBuilder extends QueryConditionsBuilder {
}

selectRaw(sqlOrBuilder) {
this.operations.select.add({ sql: sqlOrBuilder });
this.operations.select.add({ raw: sqlOrBuilder });
return this;
}

Expand Down Expand Up @@ -256,6 +256,11 @@ class SelectBuilder extends QueryConditionsBuilder {
select,
(op) => {
if (typeof op === 'string') return op;
if (op.raw) {
return typeof op.raw === 'string'
? op.raw
: this._whereValueMapper(op.raw).build();
}
const alias = op.alias ? ` AS ${op.alias}` : '';
if (op.sql) {
const sql =
Expand Down
52 changes: 46 additions & 6 deletions test/sqlgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,21 +593,51 @@ test.testSync('Select fn operation', (test, { builder, params }) => {
test.testSync('Select custom operation', (test, { builder, params }) => {
builder.from('table').selectRaw('array_agg("f1")').whereEq('f2', 42);
const query = builder.build();
test.strictSame(
query,
'SELECT (array_agg("f1")) FROM "table" WHERE "f2" = $1'
);
test.strictSame(query, 'SELECT array_agg("f1") FROM "table" WHERE "f2" = $1');
test.strictSame(params.build(), [42]);
});

test.testSync(
'Select custom operation with QueryBuilder',
'Select raw custom operation with QueryBuilder',
(test, { builder, params }) => {
builder
.from('table')
.selectRaw(new RawBuilder('array_agg("f1")'))
.whereEq('f2', 42);
const query = builder.build();
test.strictSame(
query,
'SELECT array_agg("f1") FROM "table" WHERE "f2" = $1'
);
test.strictSame(params.build(), [42]);
}
);

test.testSync(
'Select raw with function builder with QueryBuilder',
(test, { builder, params }) => {
builder
.from('table')
.selectRaw((b) => b.from('table').select('a').limit(1))
.whereEq('f2', 42);
const query = builder.build();
// Invalid query here because of selectRaw.
test.strictSame(
query,
'SELECT SELECT "a" FROM "table" LIMIT $1 FROM "table" WHERE "f2" = $2'
);
test.strictSame(params.build(), [1, 42]);
}
);

test.testSync(
'Select custom operation with QueryBuilder',
(test, { builder, params }) => {
builder
.from('table')
.select(new RawBuilder('array_agg("f1")'))
.whereEq('f2', 42);
const query = builder.build();
test.strictSame(
query,
'SELECT (array_agg("f1")) FROM "table" WHERE "f2" = $1'
Expand All @@ -626,12 +656,22 @@ test.testSync(
const query = builder.build();
test.strictSame(
query,
'SELECT (array_agg("f1")) FROM "table" WHERE "f2" = $1'
'SELECT array_agg("f1") FROM "table" WHERE "f2" = $1'
);
test.strictSame(params.build(), [42]);
}
);

test.testSync('Select raw with count', (test, { builder, params }) => {
builder.from('table').selectRaw('COUNT(*) as count').whereEq('f2', 42);
const query = builder.build();
test.strictSame(
query,
'SELECT COUNT(*) as count FROM "table" WHERE "f2" = $1'
);
test.strictSame(params.build(), [42]);
});

test.testSync(
'Select multiple operations order',
(test, { builder, params }) => {
Expand Down

0 comments on commit 07c5635

Please sign in to comment.