pqb 0.31.4 → 0.31.6

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/index.mjs CHANGED
@@ -1774,35 +1774,68 @@ function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
1774
1774
  return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1775
1775
  }
1776
1776
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1777
- var _a, _b, _c, _d, _e;
1777
+ var _a;
1778
1778
  const index = column.indexOf(".");
1779
1779
  if (index !== -1) {
1780
- const table = column.slice(0, index);
1781
- const key = column.slice(index + 1);
1782
- if (key === "*") {
1783
- if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1784
- return select ? `row_to_json("${table}".*)` : `"${table}".r`;
1780
+ return columnWithDotToSql(
1781
+ ctx,
1782
+ data,
1783
+ shape,
1784
+ column,
1785
+ index,
1786
+ quotedAs,
1787
+ select
1788
+ );
1789
+ }
1790
+ if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1791
+ return `"${column}".r`;
1792
+ }
1793
+ return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1794
+ };
1795
+ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1796
+ var _a;
1797
+ const index = column.indexOf(".");
1798
+ if (index !== -1) {
1799
+ return columnWithDotToSql(ctx, data, data.shape, column, index, quotedAs);
1800
+ } else {
1801
+ if ((_a = data.joinedShapes) == null ? void 0 : _a[column]) {
1802
+ return `"${column}".r`;
1803
+ }
1804
+ if (data.select) {
1805
+ for (const s of data.select) {
1806
+ if (typeof s === "object" && "selectAs" in s) {
1807
+ if (column in s.selectAs) {
1808
+ return simpleColumnToSQL(ctx, column, data.shape[column]);
1809
+ }
1810
+ }
1785
1811
  }
1786
- return column;
1787
1812
  }
1788
- const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1789
- const quoted = `"${table}"`;
1790
- const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
1791
- if (col) {
1792
- if (col.data.name) {
1793
- return `"${tableName}"."${col.data.name}"`;
1794
- }
1795
- if (col.data.computed) {
1796
- return `${col.data.computed.toSQL(ctx, quoted)}`;
1797
- }
1798
- return `"${tableName}"."${key}"`;
1813
+ return simpleColumnToSQL(ctx, column, data.shape[column], quotedAs);
1814
+ }
1815
+ };
1816
+ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) => {
1817
+ var _a, _b, _c, _d;
1818
+ const table = column.slice(0, index);
1819
+ const key = column.slice(index + 1);
1820
+ if (key === "*") {
1821
+ if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1822
+ return select ? `row_to_json("${table}".*)` : `"${table}".r`;
1799
1823
  }
1800
- return `"${tableName}"."${key}"`;
1824
+ return column;
1801
1825
  }
1802
- if (!select && ((_e = data.joinedShapes) == null ? void 0 : _e[column])) {
1803
- return `"${column}".r`;
1826
+ const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1827
+ const quoted = `"${table}"`;
1828
+ const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
1829
+ if (col) {
1830
+ if (col.data.name) {
1831
+ return `"${tableName}"."${col.data.name}"`;
1832
+ }
1833
+ if (col.data.computed) {
1834
+ return `${col.data.computed.toSQL(ctx, quoted)}`;
1835
+ }
1836
+ return `"${tableName}"."${key}"`;
1804
1837
  }
1805
- return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1838
+ return `"${tableName}"."${key}"`;
1806
1839
  };
1807
1840
  const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1808
1841
  var _a, _b, _c, _d;
@@ -4034,7 +4067,7 @@ const addOrder = (ctx, data, column, quotedAs, dir) => {
4034
4067
  const order = dir || (!search.order || search.order === true ? emptyObject : search.order);
4035
4068
  return `${order.coverDensity ? "ts_rank_cd" : "ts_rank"}(${order.weights ? `${addValue(ctx.values, `{${order.weights}}`)}, ` : ""}${search.vectorSQL}, "${column}"${order.normalization !== void 0 ? `, ${addValue(ctx.values, order.normalization)}` : ""}) ${order.dir || "DESC"}`;
4036
4069
  }
4037
- return `${columnToSql(ctx, data, data.shape, column, quotedAs)} ${dir || "ASC"}`;
4070
+ return `${maybeSelectedColumnToSql(ctx, data, column, quotedAs)} ${dir || "ASC"}`;
4038
4071
  };
4039
4072
 
4040
4073
  const windowToSql = (ctx, data, window, quotedAs) => {
@@ -4352,14 +4385,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
4352
4385
  const name = ((_c = shape[merge]) == null ? void 0 : _c.data.name) || merge;
4353
4386
  sql = `"${name}" = excluded."${name}"`;
4354
4387
  } else if ("except" in merge) {
4355
- const notExcluded = [];
4356
- const except = toArray(merge.except);
4357
- for (let i = 0; i < columns.length; i++) {
4358
- if (!except.includes(columns[i])) {
4359
- notExcluded.push(quotedColumns[i]);
4360
- }
4361
- }
4362
- sql = mergeColumnsSql(notExcluded);
4388
+ sql = mergeColumnsSql(columns, quotedColumns, target, merge.except);
4363
4389
  } else {
4364
4390
  sql = merge.reduce((sql2, item, i) => {
4365
4391
  var _a2;
@@ -4368,7 +4394,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
4368
4394
  }, "");
4369
4395
  }
4370
4396
  } else {
4371
- sql = mergeColumnsSql(quotedColumns);
4397
+ sql = mergeColumnsSql(columns, quotedColumns, target);
4372
4398
  }
4373
4399
  ctx.sql.push("DO UPDATE SET", sql);
4374
4400
  } else if (query.onConflict.set) {
@@ -4396,8 +4422,22 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
4396
4422
  pushWhereStatementSql(ctx, q, query, quotedAs);
4397
4423
  return pushReturningSql(ctx, q, query, quotedAs, query.afterCreateSelect);
4398
4424
  };
4399
- const mergeColumnsSql = (quotedColumns2) => {
4400
- return quotedColumns2.map((column) => `${column} = excluded.${column}`).join(", ");
4425
+ const mergeColumnsSql = (columns, quotedColumns2, target, except) => {
4426
+ const notExcluded = [];
4427
+ const exclude = typeof target === "string" ? [target] : Array.isArray(target) ? [...target] : [];
4428
+ if (except) {
4429
+ if (typeof except === "string") {
4430
+ exclude.push(except);
4431
+ } else {
4432
+ exclude.push(...except);
4433
+ }
4434
+ }
4435
+ for (let i = 0; i < columns.length; i++) {
4436
+ if (!exclude.includes(columns[i])) {
4437
+ notExcluded.push(quotedColumns2[i]);
4438
+ }
4439
+ }
4440
+ return notExcluded.map((column) => `${column} = excluded.${column}`).join(", ");
4401
4441
  };
4402
4442
  const encodeRow = (ctx, q, QueryClass, row, runtimeDefaults, quotedAs) => {
4403
4443
  const arr = row.map((value) => {
@@ -4784,7 +4824,7 @@ const makeSQL = (table, options) => {
4784
4824
  }
4785
4825
  if (query.group) {
4786
4826
  const group = query.group.map(
4787
- (item) => isExpression(item) ? item.toSQL(ctx, quotedAs) : columnToSql(ctx, table.q, table.q.shape, item, quotedAs)
4827
+ (item) => isExpression(item) ? item.toSQL(ctx, quotedAs) : maybeSelectedColumnToSql(ctx, table.q, item, quotedAs)
4788
4828
  );
4789
4829
  sql.push(`GROUP BY ${group.join(", ")}`);
4790
4830
  }