@atscript/db-sql-tools 0.1.133 → 0.1.135

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.cjs CHANGED
@@ -348,9 +348,14 @@ const AGG_FN_SQL = {
348
348
  min: "MIN",
349
349
  max: "MAX"
350
350
  };
351
+ /** The SQL function for an aggregate name; throws `INVALID_QUERY` on an unsupported one. */
352
+ function aggFnName(fn, path) {
353
+ (0, _atscript_db_agg.assertAggregateFn)(fn, path);
354
+ return AGG_FN_SQL[fn];
355
+ }
351
356
  /** The bare aggregate call, e.g. `SUM("amount")` / `COUNT(*)`. */
352
357
  function aggFnSql(dialect, expr) {
353
- return `${AGG_FN_SQL[expr.$fn] ?? expr.$fn.toUpperCase()}(${expr.$field === "*" ? "*" : dialect.quoteIdentifier(expr.$field)})`;
358
+ return `${aggFnName(expr.$fn)}(${expr.$field === "*" ? "*" : dialect.quoteIdentifier(expr.$field)})`;
354
359
  }
355
360
  function buildAggExpr(dialect, expr) {
356
361
  return `${aggFnSql(dialect, expr)} AS ${dialect.quoteIdentifier((0, _atscript_db_agg.resolveAlias)(expr))}`;
@@ -703,7 +708,7 @@ function buildProjection(dialect, select) {
703
708
  }
704
709
  /** Builds the SQL expression for a single aggregate column. */
705
710
  function buildAggColExpr(dialect, c) {
706
- return `${AGG_FN_SQL[c.aggFn] ?? c.aggFn.toUpperCase()}(${c.aggField === "*" ? "*" : `${dialect.quoteIdentifier(c.sourceTable)}.${dialect.quoteIdentifier(c.sourceColumn)}`})`;
711
+ return `${aggFnName(c.aggFn, c.viewColumn)}(${c.aggField === "*" ? "*" : `${dialect.quoteIdentifier(c.sourceTable)}.${dialect.quoteIdentifier(c.sourceColumn)}`})`;
707
712
  }
708
713
  /**
709
714
  * Builds a CREATE VIEW statement from a view plan and column mappings.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { AtscriptQueryFieldRef, AtscriptQueryNode, DbControls, TDbDefaultFn, TDbFieldMeta, TDbReferentialAction, TFieldOps, TResolvedBucket, TViewColumnMapping, TViewPlan, UniquSelect } from "@atscript/db";
2
2
  import { FilterExpr, FilterVisitor } from "@uniqu/core";
3
+ import { TDbAggregateFn } from "@atscript/db/agg";
3
4
 
4
5
  //#region src/dialect.d.ts
5
6
  interface TSqlFragment {
@@ -263,7 +264,7 @@ declare const queryOpToSql: Record<string, string>;
263
264
  declare function queryNodeToSql(node: AtscriptQueryNode, resolveFieldRef: (ref: AtscriptQueryFieldRef) => string): string;
264
265
  //#endregion
265
266
  //#region src/agg.d.ts
266
- declare const AGG_FN_SQL: Record<string, string>;
267
+ declare const AGG_FN_SQL: Readonly<Record<TDbAggregateFn, string>>;
267
268
  /**
268
269
  * The SQL a `$groupBy` key renders as: a calendar-bucket alias renders the
269
270
  * bucket EXPRESSION (`dialect.calendarBucket`), anything else the quoted
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { FilterExpr, FilterVisitor } from "@uniqu/core";
2
2
  import { AtscriptQueryFieldRef, AtscriptQueryNode, DbControls, TDbDefaultFn, TDbFieldMeta, TDbReferentialAction, TFieldOps, TResolvedBucket, TViewColumnMapping, TViewPlan, UniquSelect } from "@atscript/db";
3
+ import { TDbAggregateFn } from "@atscript/db/agg";
3
4
 
4
5
  //#region src/dialect.d.ts
5
6
  interface TSqlFragment {
@@ -263,7 +264,7 @@ declare const queryOpToSql: Record<string, string>;
263
264
  declare function queryNodeToSql(node: AtscriptQueryNode, resolveFieldRef: (ref: AtscriptQueryFieldRef) => string): string;
264
265
  //#endregion
265
266
  //#region src/agg.d.ts
266
- declare const AGG_FN_SQL: Record<string, string>;
267
+ declare const AGG_FN_SQL: Readonly<Record<TDbAggregateFn, string>>;
267
268
  /**
268
269
  * The SQL a `$groupBy` key renders as: a calendar-bucket alias renders the
269
270
  * bucket EXPRESSION (`dialect.calendarBucket`), anything else the quoted
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BUCKET_UNITS, TIME_ZONE_NAME_RE, WEEK_STARTS, walkFilter } from "@uniqu/core";
2
2
  import { DbError } from "@atscript/db";
3
- import { resolveAlias } from "@atscript/db/agg";
3
+ import { assertAggregateFn, resolveAlias } from "@atscript/db/agg";
4
4
  //#region src/dialect.ts
5
5
  /**
6
6
  * Replaces positional `?` placeholders with dialect-specific numbered placeholders
@@ -347,9 +347,14 @@ const AGG_FN_SQL = {
347
347
  min: "MIN",
348
348
  max: "MAX"
349
349
  };
350
+ /** The SQL function for an aggregate name; throws `INVALID_QUERY` on an unsupported one. */
351
+ function aggFnName(fn, path) {
352
+ assertAggregateFn(fn, path);
353
+ return AGG_FN_SQL[fn];
354
+ }
350
355
  /** The bare aggregate call, e.g. `SUM("amount")` / `COUNT(*)`. */
351
356
  function aggFnSql(dialect, expr) {
352
- return `${AGG_FN_SQL[expr.$fn] ?? expr.$fn.toUpperCase()}(${expr.$field === "*" ? "*" : dialect.quoteIdentifier(expr.$field)})`;
357
+ return `${aggFnName(expr.$fn)}(${expr.$field === "*" ? "*" : dialect.quoteIdentifier(expr.$field)})`;
353
358
  }
354
359
  function buildAggExpr(dialect, expr) {
355
360
  return `${aggFnSql(dialect, expr)} AS ${dialect.quoteIdentifier(resolveAlias(expr))}`;
@@ -702,7 +707,7 @@ function buildProjection(dialect, select) {
702
707
  }
703
708
  /** Builds the SQL expression for a single aggregate column. */
704
709
  function buildAggColExpr(dialect, c) {
705
- return `${AGG_FN_SQL[c.aggFn] ?? c.aggFn.toUpperCase()}(${c.aggField === "*" ? "*" : `${dialect.quoteIdentifier(c.sourceTable)}.${dialect.quoteIdentifier(c.sourceColumn)}`})`;
710
+ return `${aggFnName(c.aggFn, c.viewColumn)}(${c.aggField === "*" ? "*" : `${dialect.quoteIdentifier(c.sourceTable)}.${dialect.quoteIdentifier(c.sourceColumn)}`})`;
706
711
  }
707
712
  /**
708
713
  * Builds a CREATE VIEW statement from a view plan and column mappings.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-sql-tools",
3
- "version": "0.1.133",
3
+ "version": "0.1.135",
4
4
  "description": "Shared SQL builder utilities for @atscript database adapters.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -37,12 +37,12 @@
37
37
  "access": "public"
38
38
  },
39
39
  "devDependencies": {
40
- "@uniqu/core": "^0.1.10",
40
+ "@uniqu/core": "^0.1.11",
41
41
  "unplugin-atscript": "^0.1.92"
42
42
  },
43
43
  "peerDependencies": {
44
- "@uniqu/core": "^0.1.10",
45
- "@atscript/db": "^0.1.133"
44
+ "@uniqu/core": "^0.1.11",
45
+ "@atscript/db": "^0.1.135"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "vp pack",