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