pqb 0.42.9 → 0.43.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/index.mjs CHANGED
@@ -5648,11 +5648,18 @@ function queryJson(self, coalesce) {
5648
5648
  return q;
5649
5649
  }
5650
5650
 
5651
- const pushSelectSql = (ctx, table, query, quotedAs) => {
5652
- const sql = selectToSql(ctx, table, query, quotedAs);
5651
+ const pushSelectSql = (ctx, table, query, quotedAs, aliases) => {
5652
+ const sql = selectToSql(
5653
+ ctx,
5654
+ table,
5655
+ query,
5656
+ quotedAs,
5657
+ query.hookSelect,
5658
+ aliases
5659
+ );
5653
5660
  if (sql) ctx.sql.push(sql);
5654
5661
  };
5655
- const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect) => {
5662
+ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect, aliases) => {
5656
5663
  let selected;
5657
5664
  const list = [];
5658
5665
  if (query.select) {
@@ -5694,6 +5701,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
5694
5701
  }
5695
5702
  }
5696
5703
  list.push(sql);
5704
+ aliases?.push("");
5697
5705
  } else if (item) {
5698
5706
  if ("selectAs" in item) {
5699
5707
  const obj = item.selectAs;
@@ -5703,8 +5711,9 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
5703
5711
  if (typeof value === "object") {
5704
5712
  if (isExpression(value)) {
5705
5713
  list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
5714
+ aliases?.push(as);
5706
5715
  } else {
5707
- pushSubQuerySql(ctx, value, as, list, quotedAs);
5716
+ pushSubQuerySql(ctx, value, as, list, quotedAs, aliases);
5708
5717
  }
5709
5718
  } else if (value) {
5710
5719
  list.push(
@@ -5717,10 +5726,12 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
5717
5726
  true
5718
5727
  )
5719
5728
  );
5729
+ aliases?.push(as);
5720
5730
  }
5721
5731
  }
5722
5732
  } else {
5723
5733
  list.push(selectedObjectToSQL(ctx, quotedAs, item));
5734
+ aliases?.push("");
5724
5735
  }
5725
5736
  }
5726
5737
  }
@@ -5771,7 +5782,7 @@ function selectedObjectToSQL(ctx, quotedAs, item) {
5771
5782
  const selectAllSql = (query, quotedAs) => {
5772
5783
  return query.join?.length ? query.selectAllColumns?.map((item) => `${quotedAs}.${item}`).join(", ") || `${quotedAs}.*` : query.selectAllColumns?.join(", ") || "*";
5773
5784
  };
5774
- const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
5785
+ const pushSubQuerySql = (ctx, query, as, list, quotedAs, aliases) => {
5775
5786
  const { returnType = "all" } = query.q;
5776
5787
  if (isQueryNone(query)) {
5777
5788
  let sql;
@@ -5797,6 +5808,7 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
5797
5808
  throw new UnhandledTypeError(query, returnType);
5798
5809
  }
5799
5810
  list.push(`${sql} "${as}"`);
5811
+ aliases?.push(as);
5800
5812
  return;
5801
5813
  }
5802
5814
  if (query.q.joinedForSelect) {
@@ -5821,7 +5833,10 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
5821
5833
  default:
5822
5834
  throw new UnhandledTypeError(query, returnType);
5823
5835
  }
5824
- if (sql) list.push(`${coalesce(ctx, query, sql, quotedAs)} "${as}"`);
5836
+ if (sql) {
5837
+ list.push(`${coalesce(ctx, query, sql, quotedAs)} "${as}"`);
5838
+ aliases?.push(as);
5839
+ }
5825
5840
  return;
5826
5841
  }
5827
5842
  switch (returnType) {
@@ -6591,7 +6606,8 @@ const makeSQL = (table, options) => {
6591
6606
  if (query.distinct) {
6592
6607
  pushDistinctSql(ctx, table, query.distinct, quotedAs);
6593
6608
  }
6594
- pushSelectSql(ctx, table, query, quotedAs);
6609
+ const aliases = query.group ? [] : void 0;
6610
+ pushSelectSql(ctx, table, query, quotedAs, aliases);
6595
6611
  if (table.table || query.from) {
6596
6612
  pushFromAndAs(ctx, table, query, quotedAs);
6597
6613
  }
@@ -6607,9 +6623,14 @@ const makeSQL = (table, options) => {
6607
6623
  pushWhereStatementSql(ctx, table, query, quotedAs);
6608
6624
  }
6609
6625
  if (query.group) {
6610
- const group = query.group.map(
6611
- (item) => isExpression(item) ? item.toSQL(ctx, quotedAs) : maybeSelectedColumnToSql(ctx, table.q, item, quotedAs)
6612
- );
6626
+ const group = query.group.map((item) => {
6627
+ if (isExpression(item)) {
6628
+ return item.toSQL(ctx, quotedAs);
6629
+ } else {
6630
+ const i = aliases.indexOf(item);
6631
+ return i !== -1 ? i + 1 : columnToSql(ctx, table.q, table.shape, item, quotedAs);
6632
+ }
6633
+ });
6613
6634
  sql.push(`GROUP BY ${group.join(", ")}`);
6614
6635
  }
6615
6636
  if (query.having) pushHavingSql(ctx, query, quotedAs);
@@ -11720,6 +11741,10 @@ class QueryMethods {
11720
11741
  * .group('month');
11721
11742
  * ```
11722
11743
  *
11744
+ * Column aliases in `select` take precedence over table columns,
11745
+ * so if in the query above `db.product` had a column `month`,
11746
+ * the query would work in the exact same way, group by would reference the selected `month` expression.
11747
+ *
11723
11748
  * @param columns - column names or a raw SQL
11724
11749
  */
11725
11750
  group(...columns) {