@dbsp/adapter-pgsql 7.1.0 → 7.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12931,6 +12931,15 @@ function assertSafeOperator(op2, opts) {
12931
12931
  );
12932
12932
  }
12933
12933
  }
12934
+ function expressionNamedColumnRef(name, ctx) {
12935
+ const dotIdx = name.indexOf(".");
12936
+ if (dotIdx === -1) return expressionUnqualifiedColumnRef(name, ctx);
12937
+ return expressionQualifiedColumnRef(
12938
+ name.slice(dotIdx + 1),
12939
+ name.slice(0, dotIdx),
12940
+ ctx
12941
+ );
12942
+ }
12934
12943
  function compileExpressionIntent(intent, ctx, state) {
12935
12944
  const kind = intent.kind;
12936
12945
  switch (kind) {
@@ -12961,7 +12970,7 @@ function compileExpressionIntent(intent, ctx, state) {
12961
12970
  (arg) => compileExpressionIntent(arg, ctx, state)
12962
12971
  );
12963
12972
  const orderByNodes = i.aggOrderBy && i.aggOrderBy.length > 0 ? i.aggOrderBy.map((ob) => {
12964
- const colNode = expressionUnqualifiedColumnRef(ob.field, ctx);
12973
+ const colNode = expressionNamedColumnRef(ob.field, ctx);
12965
12974
  return sortBy(colNode, ob.direction === "desc" ? "DESC" : "ASC");
12966
12975
  }) : void 0;
12967
12976
  const filterNode = i.filter ? ctx.compileCustomFnFilter?.(i.filter, ctx, state) : void 0;
@@ -12978,13 +12987,7 @@ function compileExpressionIntent(intent, ctx, state) {
12978
12987
  }
12979
12988
  case "ref": {
12980
12989
  const i = intent;
12981
- const dotIdx = i.column.indexOf(".");
12982
- if (dotIdx !== -1) {
12983
- const table2 = i.column.slice(0, dotIdx);
12984
- const col = i.column.slice(dotIdx + 1);
12985
- return expressionQualifiedColumnRef(col, table2, ctx);
12986
- }
12987
- return expressionUnqualifiedColumnRef(i.column, ctx);
12990
+ return expressionNamedColumnRef(i.column, ctx);
12988
12991
  }
12989
12992
  case "param": {
12990
12993
  const i = intent;
@@ -19271,13 +19274,22 @@ var prefixedRelationColumnHandler = {
19271
19274
 
19272
19275
  // src/handlers/expression/window.ts
19273
19276
  var WINDOW_FRAME_DEFAULT = 1034;
19277
+ function windowColumnRef(name, ctx) {
19278
+ const dotIdx = name.indexOf(".");
19279
+ if (dotIdx === -1) return expressionColumnRef(name, ctx);
19280
+ return expressionQualifiedColumnRef(
19281
+ name.slice(dotIdx + 1),
19282
+ name.slice(0, dotIdx),
19283
+ ctx
19284
+ );
19285
+ }
19274
19286
  function isWindowOrderBy(orderBy) {
19275
19287
  return Array.isArray(orderBy) && orderBy.every(
19276
19288
  (item) => typeof item === "object" && item !== null && "column" in item && typeof item.column === "string"
19277
19289
  );
19278
19290
  }
19279
19291
  function buildSortBy(column2, direction, ctx) {
19280
- const colRef = expressionColumnRef(column2, ctx);
19292
+ const colRef = windowColumnRef(column2, ctx);
19281
19293
  const sortBy2 = {
19282
19294
  node: colRef,
19283
19295
  sortby_dir: direction === "DESC" ? "SORTBY_DESC" : "SORTBY_ASC",
@@ -19292,7 +19304,7 @@ function buildWindowDef(decision, ctx) {
19292
19304
  const windowDef = { frameOptions: WINDOW_FRAME_DEFAULT };
19293
19305
  if (partition && partition.length > 0) {
19294
19306
  windowDef.partitionClause = partition.map(
19295
- (col) => expressionColumnRef(col, ctx)
19307
+ (col) => windowColumnRef(col, ctx)
19296
19308
  );
19297
19309
  }
19298
19310
  if (orderBy && orderBy.length > 0) {
@@ -19351,7 +19363,7 @@ function createLagLeadHandler(funcName, types) {
19351
19363
  if (!column2) {
19352
19364
  throw new Error(`${upperName} requires a column`);
19353
19365
  }
19354
- const colRef = expressionColumnRef(column2, ctx);
19366
+ const colRef = windowColumnRef(column2, ctx);
19355
19367
  const args = [colRef];
19356
19368
  const offset = decision.args?.[0] ?? 1;
19357
19369
  args.push(bindParameter(unwrapParamIntent(offset), state));
@@ -19373,7 +19385,7 @@ function createColumnWindowHandler(funcName, types) {
19373
19385
  if (!column2) {
19374
19386
  throw new Error(`${upperName} requires a column`);
19375
19387
  }
19376
- const colRef = expressionColumnRef(column2, ctx);
19388
+ const colRef = windowColumnRef(column2, ctx);
19377
19389
  return buildWindowFunction(funcName, [colRef], decision, ctx);
19378
19390
  }
19379
19391
  };
@@ -19408,7 +19420,7 @@ var genericWindowHandler = {
19408
19420
  }
19409
19421
  const args = [];
19410
19422
  if (decision.column) {
19411
- args.push(expressionColumnRef(decision.column, ctx));
19423
+ args.push(windowColumnRef(decision.column, ctx));
19412
19424
  }
19413
19425
  if (decision.args && Array.isArray(decision.args)) {
19414
19426
  for (const arg of decision.args) {
@@ -26280,7 +26292,12 @@ function compileUpdate(config, ctx, state) {
26280
26292
  )
26281
26293
  }));
26282
26294
  let whereClause;
26283
- if (config.where && config.where.length > 0) {
26295
+ if (config.whereCompiler) {
26296
+ whereClause = config.whereCompiler(
26297
+ { ...ctx, currentAlias: tableAlias },
26298
+ state
26299
+ );
26300
+ } else if (config.where && config.where.length > 0) {
26284
26301
  const dispatch = createWhereDispatcher(compileWhereIntent);
26285
26302
  const subCtx = { ...ctx, currentAlias: tableAlias };
26286
26303
  if (config.where.length === 1) {
@@ -26398,7 +26415,12 @@ function compileUnnestUpdate(config, ctx, state) {
26398
26415
  function compileDelete(config, ctx, state) {
26399
26416
  const tableAlias = config.tableMetadata?.physicalName ?? config.table;
26400
26417
  let whereClause;
26401
- if (config.where && config.where.length > 0) {
26418
+ if (config.whereCompiler) {
26419
+ whereClause = config.whereCompiler(
26420
+ { ...ctx, currentAlias: tableAlias },
26421
+ state
26422
+ );
26423
+ } else if (config.where && config.where.length > 0) {
26402
26424
  const dispatch = createWhereDispatcher(compileWhereIntent);
26403
26425
  const subCtx = { ...ctx, currentAlias: tableAlias };
26404
26426
  if (config.where.length === 1) {
@@ -27052,8 +27074,8 @@ import {
27052
27074
  POSTGRESQL_CAPABILITIES as POSTGRESQL_CAPABILITIES3,
27053
27075
  plan as planFn
27054
27076
  } from "@dbsp/core";
27055
- import { inspectMutationRows } from "@dbsp/core/internal";
27056
- import { isParamIntent as isParamIntent8 } from "@dbsp/types";
27077
+ import { inspectMutationRows, resolveSelectWhere } from "@dbsp/core/internal";
27078
+ import { isParamIntent as isParamIntent8, RangeAllocator } from "@dbsp/types";
27057
27079
 
27058
27080
  // src/resolved-include-decisions.ts
27059
27081
  function lowerResolvedIncludes(execution, defaultPk) {
@@ -29185,12 +29207,37 @@ function compileInsertFrom2(intent, options, deps) {
29185
29207
  sourceCte
29186
29208
  );
29187
29209
  }
29210
+ function resolvedMutationWhere(where, table2, deps) {
29211
+ if (!where || deps.model === void 0) return void 0;
29212
+ const allocator = new RangeAllocator();
29213
+ const root = allocator.bind(table2, table2);
29214
+ for (const name of [
29215
+ ...deps.bindingNames ?? [],
29216
+ ...deps.scope?.bindings.keys() ?? []
29217
+ ]) {
29218
+ allocator.reserve(name);
29219
+ }
29220
+ const resolved = resolveSelectWhere(
29221
+ where,
29222
+ root,
29223
+ [root],
29224
+ allocator,
29225
+ deps.model
29226
+ );
29227
+ if (!resolved) {
29228
+ throw new Error(
29229
+ `Internal error: the mutation WHERE on '${table2}' resolved to nothing.`
29230
+ );
29231
+ }
29232
+ return (ctx, state) => compileResolvedCondition(resolved, ctx, state);
29233
+ }
29188
29234
  function compileUpdate2(intent, options, deps) {
29189
29235
  const ctx = mutationContext(deps, intent.table, MAX_DEPTH_LIMIT);
29190
29236
  const state = createCompilerState();
29191
29237
  const setColumns = Object.keys(intent.set ?? {});
29192
29238
  const columnTypes = getColumnTypes(intent.table, setColumns, deps);
29193
- const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29239
+ const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29240
+ const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29194
29241
  const config = {
29195
29242
  table: declaredMutationTable(deps, intent.table),
29196
29243
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29199,6 +29246,7 @@ function compileUpdate2(intent, options, deps) {
29199
29246
  value
29200
29247
  })),
29201
29248
  ...resolvedWhere && { where: [whereIntentAsDecision(resolvedWhere)] },
29249
+ ...whereCompiler && { whereCompiler },
29202
29250
  ...intent.returning && { returning: [...intent.returning] },
29203
29251
  ...intent.returning && {
29204
29252
  returningSources: mutationReturningSources(
@@ -29332,11 +29380,13 @@ function compileBatchUpdate(intent, options, deps) {
29332
29380
  function compileDelete2(intent, options, deps) {
29333
29381
  const ctx = mutationContext(deps, intent.table, MAX_DEPTH_LIMIT);
29334
29382
  const state = createCompilerState();
29335
- const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29383
+ const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29384
+ const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29336
29385
  const config = {
29337
29386
  table: declaredMutationTable(deps, intent.table),
29338
29387
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
29339
29388
  ...resolvedWhere && { where: [whereIntentAsDecision(resolvedWhere)] },
29389
+ ...whereCompiler && { whereCompiler },
29340
29390
  ...intent.returning && { returning: [...intent.returning] },
29341
29391
  ...intent.returning && {
29342
29392
  returningSources: mutationReturningSources(
@@ -29432,7 +29482,12 @@ function compileUpsert2(intent, options, deps, rowShape) {
29432
29482
  );
29433
29483
  const hasRawExprs = Object.keys(rawExprs).length > 0;
29434
29484
  const actionWhere = intent.action.type === "doUpdate" && intent.action.where ? intent.action.where : void 0;
29435
- const resolvedActionWhere = actionWhere ? resolveExistsIntent(actionWhere, intent.table, deps) : void 0;
29485
+ const actionWhereCompiler = resolvedMutationWhere(
29486
+ actionWhere,
29487
+ intent.table,
29488
+ deps
29489
+ );
29490
+ const resolvedActionWhere = actionWhere ? actionWhereCompiler ? actionWhere : resolveExistsIntent(actionWhere, intent.table, deps) : void 0;
29436
29491
  const config = {
29437
29492
  table: declaredMutationTable(deps, intent.table),
29438
29493
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29479,7 +29534,13 @@ function compileUpsert2(intent, options, deps, rowShape) {
29479
29534
  actionWhereIntent: resolvedActionWhere,
29480
29535
  compileActionWhere: (where, paramState) => {
29481
29536
  const actionState = createCompilerState();
29482
- const actionWhere2 = compileUpsertActionWhere(
29537
+ const actionWhere2 = actionWhereCompiler ? actionWhereCompiler(
29538
+ {
29539
+ ...mutationContext(deps, intent.table, MAX_DEPTH_LIMIT),
29540
+ currentAlias: declaredMutationTable(deps, intent.table)
29541
+ },
29542
+ actionState
29543
+ ) : compileUpsertActionWhere(
29483
29544
  where,
29484
29545
  intent.table,
29485
29546
  actionState,
@@ -33931,6 +33992,21 @@ var PgAdapter = class {
33931
33992
  // correctly via a fresh inner PlanCompiler (same pattern as PlanCompiler
33932
33993
  // case 'selectCustomExpression').
33933
33994
  compileSubquery(query, paramOffset) {
33995
+ if (deps.model !== void 0) {
33996
+ const issued = planFn3(query, deps.model, {
33997
+ ...options?.defaultFilters && {
33998
+ defaultFilters: options.defaultFilters
33999
+ },
34000
+ ...dialectCapabilities !== void 0 && { dialectCapabilities }
34001
+ });
34002
+ const envelope = compileSelectEnvelope(issued, options, deps);
34003
+ if (envelope.ast === void 0)
34004
+ throw new Error("A planned selectExpression body produced no AST.");
34005
+ return {
34006
+ ast: renumberParamRefsInAst(envelope.ast, paramOffset),
34007
+ parameters: envelope.parameters
34008
+ };
34009
+ }
33934
34010
  const innerCompiler = new PlanCompiler({
33935
34011
  dbCasing: deps.dbCasing ?? "preserve",
33936
34012
  defaultPkColumnName: deps.defaultPk,
@@ -59268,4 +59344,4 @@ export {
59268
59344
  planPgTransitionRun,
59269
59345
  applyPgTransitionRun
59270
59346
  };
59271
- //# sourceMappingURL=chunk-GEN6X6M2.js.map
59347
+ //# sourceMappingURL=chunk-ZFU56LKS.js.map