pqb 0.39.1 → 0.39.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.
package/dist/index.js CHANGED
@@ -1868,15 +1868,22 @@ const queryTypeWithLimitOne = {
1868
1868
  };
1869
1869
  const isQueryReturnsAll = (q) => !q.q.returnType || q.q.returnType === "all";
1870
1870
 
1871
- function simpleColumnToSQL(ctx, key, column, quotedAs) {
1871
+ const applySqlComputed = (ctx, q, computed, as, quotedAs) => {
1872
+ var _a;
1873
+ const parser = computed.result.value.parseFn;
1874
+ if (parser)
1875
+ ((_a = q.parsers) != null ? _a : q.parsers = {})[as] = parser;
1876
+ return computed.toSQL(ctx, quotedAs);
1877
+ };
1878
+ function simpleColumnToSQL(ctx, q, key, column, quotedAs) {
1872
1879
  if (!column)
1873
1880
  return `"${key}"`;
1874
1881
  const { data } = column;
1875
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1882
+ return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1876
1883
  }
1877
- function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
1884
+ function simpleExistingColumnToSQL(ctx, q, key, column, quotedAs) {
1878
1885
  const { data } = column;
1879
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1886
+ return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1880
1887
  }
1881
1888
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1882
1889
  var _a;
@@ -1895,7 +1902,7 @@ const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1895
1902
  if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1896
1903
  return `"${column}".r`;
1897
1904
  }
1898
- return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1905
+ return simpleColumnToSQL(ctx, data, column, shape[column], quotedAs);
1899
1906
  };
1900
1907
  const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1901
1908
  var _a;
@@ -1910,12 +1917,12 @@ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1910
1917
  for (const s of data.select) {
1911
1918
  if (typeof s === "object" && "selectAs" in s) {
1912
1919
  if (column in s.selectAs) {
1913
- return simpleColumnToSQL(ctx, column, data.shape[column]);
1920
+ return simpleColumnToSQL(ctx, data, column, data.shape[column]);
1914
1921
  }
1915
1922
  }
1916
1923
  }
1917
1924
  }
1918
- return simpleColumnToSQL(ctx, column, data.shape[column], quotedAs);
1925
+ return simpleColumnToSQL(ctx, data, column, data.shape[column], quotedAs);
1919
1926
  }
1920
1927
  };
1921
1928
  const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) => {
@@ -1933,17 +1940,30 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
1933
1940
  return `"${tableName}"."${col.data.name}"`;
1934
1941
  }
1935
1942
  if (col.data.computed) {
1936
- return `${col.data.computed.toSQL(ctx, quoted)}`;
1943
+ return applySqlComputed(ctx, data, col.data.computed, column, quoted);
1937
1944
  }
1938
1945
  return `"${tableName}"."${key}"`;
1939
1946
  }
1940
1947
  return `"${tableName}"."${key}"`;
1941
1948
  };
1942
- const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select) => {
1949
+ const columnToSqlWithAs = (ctx, data, column, as, quotedAs, select) => {
1950
+ const index = column.indexOf(".");
1951
+ return index !== -1 ? tableColumnToSqlWithAs(
1952
+ ctx,
1953
+ data,
1954
+ column,
1955
+ column.slice(0, index),
1956
+ column.slice(index + 1),
1957
+ as,
1958
+ quotedAs,
1959
+ select
1960
+ ) : ownColumnToSqlWithAs(ctx, data, column, as, quotedAs, select);
1961
+ };
1962
+ const tableColumnToSqlWithAs = (ctx, data, column, table, key, as, quotedAs, select) => {
1943
1963
  var _a, _b, _c;
1944
1964
  if (key === "*") {
1945
1965
  if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1946
- return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
1966
+ return select ? `row_to_json("${table}".*) "${as}"` : `"${table}".r "${as}"`;
1947
1967
  }
1948
1968
  return column;
1949
1969
  }
@@ -1952,29 +1972,41 @@ const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select)
1952
1972
  const col = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
1953
1973
  if (col) {
1954
1974
  if (col.data.name && col.data.name !== key) {
1955
- return `"${tableName}"."${col.data.name}" "${key}"`;
1975
+ return `"${tableName}"."${col.data.name}" "${as}"`;
1956
1976
  }
1957
1977
  if (col.data.computed) {
1958
- return `${col.data.computed.toSQL(ctx, quoted)} "${key}"`;
1978
+ return `${applySqlComputed(
1979
+ ctx,
1980
+ data,
1981
+ col.data.computed,
1982
+ as,
1983
+ quoted
1984
+ )} "${as}"`;
1959
1985
  }
1960
1986
  }
1961
- return `"${tableName}"."${key}"`;
1987
+ return `"${tableName}"."${key}"${key === as ? "" : ` "${as}"`}`;
1962
1988
  };
1963
- const ownColumnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1989
+ const ownColumnToSqlWithAs = (ctx, data, column, as, quotedAs, select) => {
1964
1990
  var _a;
1965
1991
  if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1966
- return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
1992
+ return select ? `row_to_json("${column}".*) "${as}"` : `"${column}".r "${as}"`;
1967
1993
  }
1968
1994
  const col = data.shape[column];
1969
1995
  if (col) {
1970
1996
  if (col.data.name && col.data.name !== column) {
1971
- return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}" "${column}"`;
1997
+ return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}"${col.data.name === as ? "" : ` "${as}"`}`;
1972
1998
  }
1973
1999
  if (col.data.computed) {
1974
- return `${col.data.computed.toSQL(ctx, quotedAs)} "${column}"`;
2000
+ return `${applySqlComputed(
2001
+ ctx,
2002
+ data,
2003
+ col.data.computed,
2004
+ as,
2005
+ quotedAs
2006
+ )} "${as}"`;
1975
2007
  }
1976
2008
  }
1977
- return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
2009
+ return `${quotedAs ? `${quotedAs}.` : ""}"${column}"${column === as ? "" : ` "${as}"`}`;
1978
2010
  };
1979
2011
  const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
1980
2012
  return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
@@ -2306,7 +2338,13 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2306
2338
  let column = query.shape[key];
2307
2339
  let quotedColumn;
2308
2340
  if (column) {
2309
- quotedColumn = simpleExistingColumnToSQL(ctx, key, column, quotedAs);
2341
+ quotedColumn = simpleExistingColumnToSQL(
2342
+ ctx,
2343
+ query,
2344
+ key,
2345
+ column,
2346
+ quotedAs
2347
+ );
2310
2348
  } else if (!column) {
2311
2349
  const index = key.indexOf(".");
2312
2350
  if (index !== -1) {
@@ -2314,7 +2352,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2314
2352
  const quoted = `"${table2}"`;
2315
2353
  const name = key.slice(index + 1);
2316
2354
  column = quotedAs === quoted ? query.shape[name] : (_b = (_a = query.joinedShapes) == null ? void 0 : _a[table2]) == null ? void 0 : _b[name];
2317
- quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
2355
+ quotedColumn = simpleColumnToSQL(ctx, query, name, column, quoted);
2318
2356
  } else {
2319
2357
  column = (_d = (_c = query.joinedShapes) == null ? void 0 : _c[key]) == null ? void 0 : _d.value;
2320
2358
  quotedColumn = `"${key}".r`;
@@ -4849,13 +4887,21 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4849
4887
  item,
4850
4888
  tableName,
4851
4889
  key,
4890
+ key === "*" ? tableName : key,
4852
4891
  quotedAs,
4853
4892
  true
4854
4893
  );
4855
4894
  } else {
4856
4895
  if (hookSelect == null ? void 0 : hookSelect.get(item))
4857
4896
  (selected != null ? selected : selected = {})[item] = quotedAs;
4858
- sql = ownColumnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4897
+ sql = ownColumnToSqlWithAs(
4898
+ ctx,
4899
+ table.q,
4900
+ item,
4901
+ item,
4902
+ quotedAs,
4903
+ true
4904
+ );
4859
4905
  }
4860
4906
  }
4861
4907
  list.push(sql);
@@ -4874,14 +4920,14 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4874
4920
  }
4875
4921
  } else if (value) {
4876
4922
  list.push(
4877
- `${columnToSql(
4923
+ columnToSqlWithAs(
4878
4924
  ctx,
4879
4925
  table.q,
4880
- table.q.shape,
4881
4926
  value,
4927
+ as,
4882
4928
  quotedAs,
4883
4929
  true
4884
- )} "${as}"`
4930
+ )
4885
4931
  );
4886
4932
  }
4887
4933
  }
@@ -4910,7 +4956,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4910
4956
  quotedTable = quotedAs;
4911
4957
  columnName = select;
4912
4958
  col = query.shape[select];
4913
- sql = simpleColumnToSQL(ctx, select, col, quotedAs);
4959
+ sql = simpleColumnToSQL(ctx, query, select, col, quotedAs);
4914
4960
  }
4915
4961
  if (selected == null ? void 0 : selected[columnName]) {
4916
4962
  if ((selected == null ? void 0 : selected[columnName]) === quotedTable) {
@@ -11379,6 +11425,8 @@ class ColumnRefExpression extends orchidCore.Expression {
11379
11425
  makeSQL(ctx, quotedAs) {
11380
11426
  return simpleExistingColumnToSQL(
11381
11427
  ctx,
11428
+ // it's for parsers for computed SQL. In the column ref case, parsers should be set when selecting the column ref.
11429
+ {},
11382
11430
  this.name,
11383
11431
  this.result.value,
11384
11432
  quotedAs