Skip to content

Commit

Permalink
Added bulk update/delete support
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Dec 21, 2023
1 parent 330ab26 commit 2f29841
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/drivers/sql-server/ExpressionToSqlServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,16 @@ export default class ExpressionToSqlServer extends ExpressionToSql {

visitUpdateStatement(e: UpdateStatement): ITextQuery {
if (e.join) {

const table = this.visit(e.table);
this.scope.create({ parameter: e.sourceParameter, model: e.sourceParameter.model })
const as = e.join.as as ParameterExpression;
this.scope.create({ parameter: as, model: as.model })
const join = this.visit(e.join.source);
const where = this.visit(e.where);
const joinName = this.scope.nameOf(as);
const asName = this.scope.nameOf(e.sourceParameter);
const set = this.visitArray(e.set, ",");
return prepare `WITH ${joinName} as (${join}) UPDATE ${asName} SET ${set} FROM ${table} AS ${asName} INNER JOIN ${joinName} ON ${where}`;
}
return super.visitUpdateStatement(e);
}
Expand Down
7 changes: 7 additions & 0 deletions src/query/ast/ExpressionToSql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,9 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
const target = this.resolveExpression(me.target);
if (target.type === "ParameterExpression" && me.property.type === "Identifier") {
const id = me.property as Identifier;
if (id.quoted) {
return;
}
const pe = target as ParameterExpression;
const scope = this.scope.get(pe);
const peModel = scope?.model;
Expand Down Expand Up @@ -780,6 +783,10 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {

const resolved = this.resolveExpression(x);

if (!resolved) {
return;
}

if (resolved.type === "MemberExpression") {
x = resolved;
}
Expand Down
4 changes: 2 additions & 2 deletions src/tests/db-tests/tests/update-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function(this: TestConfig) {

await context.products
.where(void 0, (p) => (x) => x.productID > 1)
.trace(console.log)
// .trace(console.log)
.update(void 0, (p) => (x) => ({
productDescription: "updated"
}));
Expand All @@ -32,7 +32,7 @@ export default async function(this: TestConfig) {
const n = await context.products
.where(void 0, (p) => (x) => true === true)
.limit(1)
.trace(console.log)
// .trace(console.log)
.update(void 0, (p) => (x) => ({
productDescription: "x"
}));
Expand Down

0 comments on commit 2f29841

Please sign in to comment.