@crvouga/mockingbird-service-postgres 3.0.1 → 3.1.0

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.
package/dist/server.js CHANGED
@@ -24772,6 +24772,46 @@ var decodeParam = (oid, format, bytes) => {
24772
24772
  throw pgError2("internal", `binary parameter format is not supported for type oid ${oid}`, "0A000");
24773
24773
  }
24774
24774
  };
24775
+ function returningShape(db, sql) {
24776
+ let statements;
24777
+ try {
24778
+ statements = parse(sql);
24779
+ } catch {
24780
+ return null;
24781
+ }
24782
+ const stmt = statements.length === 1 ? statements[0] : void 0;
24783
+ if (!stmt || stmt.type !== "insert" && stmt.type !== "update" && stmt.type !== "delete") return null;
24784
+ const targets = stmt.returning;
24785
+ if (!targets || targets.length === 0) return null;
24786
+ const table = db.state.findTable(stmt.table);
24787
+ if (!table) return null;
24788
+ const label = stmt.alias ?? table.name;
24789
+ const columns = [];
24790
+ const columnTypes = [];
24791
+ for (const target of targets) {
24792
+ if (target.expr.type === "star") {
24793
+ const qualifier = target.expr.table?.at(-1);
24794
+ if (qualifier !== void 0 && qualifier !== label && qualifier !== table.name) return null;
24795
+ for (const column of table.columns) {
24796
+ columns.push(column.name);
24797
+ columnTypes.push(column.type.id);
24798
+ }
24799
+ continue;
24800
+ }
24801
+ if (target.expr.type === "colref") {
24802
+ const name = target.expr.parts.at(-1) ?? "?column?";
24803
+ const column = table.columns.find((item) => item.name === name);
24804
+ columns.push(target.alias ?? name);
24805
+ columnTypes.push(column?.type.id ?? "text");
24806
+ continue;
24807
+ }
24808
+ columns.push(target.alias ?? inferColumnName(target.expr));
24809
+ columnTypes.push("text");
24810
+ }
24811
+ if (columns.length === 0) return null;
24812
+ const command2 = stmt.type === "insert" ? "INSERT" : stmt.type === "update" ? "UPDATE" : "DELETE";
24813
+ return { columns, columnTypes, rows: [], rowCount: 0, command: command2 };
24814
+ }
24775
24815
  var Connection = class {
24776
24816
  constructor(socket, cluster, options) {
24777
24817
  this.socket = socket;
@@ -25652,11 +25692,16 @@ var Connection = class {
25652
25692
  else this.write(backend.rowDescription(this.fields(outcome.result, portal.resultFormats)));
25653
25693
  }
25654
25694
  /**
25655
- * The row shape of a statement that returns rows, for Describe on a statement: a trial run with
25656
- * null parameters inside a transaction that is rolled back. Anything else (or a trial that
25657
- * fails) is described as returning no data.
25695
+ * The row shape of a statement that returns rows, for Describe on a statement.
25696
+ * `INSERT`/`UPDATE`/`DELETE ... RETURNING` is described from the catalog: a trial run binds
25697
+ * nulls and fails NOT NULL, and Describe would answer NoData. Bun then reads one DataRow cell
25698
+ * and treats the rest of the message as a broken protocol frame.
25699
+ * Other row-returning statements are a trial run with null parameters, rolled back. A trial
25700
+ * that fails is described as returning no data.
25658
25701
  */
25659
25702
  async shapeOf(prepared) {
25703
+ const described = returningShape(this.database, prepared.sql);
25704
+ if (described) return described;
25660
25705
  if (!RETURNS_ROWS.test(prepared.sql)) return null;
25661
25706
  const db = this.database;
25662
25707
  await this.cluster.acquireTurn(this);