@nongt/visql-core 0.11.0 → 0.12.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.
@@ -5,13 +5,16 @@ export type BinaryOperator = typeof binaryOperators[number];
5
5
  export declare const unaryOperators: readonly ["IS NULL", "IS NOT NULL"];
6
6
  export declare function isUnaryOperator(operator: BinaryOperator | undefined): boolean;
7
7
  export type LogicalOperator = 'AND' | 'OR';
8
+ /** A calculation's operators (issue 26). */
9
+ export declare const arithmeticOperators: readonly ["+", "-", "*", "/"];
10
+ export type ArithmeticOperator = typeof arithmeticOperators[number];
8
11
  /** A value's type (issue 130, see valueTypes.ts). `integer` is a whole
9
12
  * number, `number` any. A `date` is a calendar day and a `dateTime` a day
10
13
  * and a time, both as calendarDate.ts holds them; a `time` is a time of
11
14
  * day, stored as `HH:MM:SS` text. */
12
15
  export type ValueType = 'text' | 'integer' | 'number' | 'date' | 'dateTime' | 'time' | 'boolean';
13
16
  export type LiteralValue = string | number | bigint | boolean | null | Date;
14
- export type ExpressionNode = LiteralNode | FieldNode | FunctionCallNode | BinaryNode | LogicalNode | NotNode | IfNode | CaseNode | ParamNode | ExistsNode | SubqueryNode | ListNode;
17
+ export type ExpressionNode = LiteralNode | FieldNode | FunctionCallNode | BinaryNode | ArithmeticNode | LogicalNode | NotNode | IfNode | CaseNode | ParamNode | ExistsNode | SubqueryNode | ListNode;
15
18
  /** A named value supplied by the application when the SQL is generated —
16
19
  * `astToParameterizedSql(query, dialect, { values: { [name]: … } })` —
17
20
  * never by the user. Code (typically a backend) adds it, e.g. a tenant or
@@ -93,6 +96,17 @@ export interface BinaryNode extends Record<string, unknown>, ColumnCopy {
93
96
  /** Absent exactly when `operator` is IS NULL / IS NOT NULL. */
94
97
  right?: ExpressionNode;
95
98
  }
99
+ /** A calculation on two numbers (issue 26): `left + right`, `left / right`.
100
+ * A value, where `binary` is a condition. `a + b + c` nests, `(a + b) + c`;
101
+ * sqlGen writes parentheses only where the order needs them. Division
102
+ * keeps its fraction in every database unless sqlGen's `division` option
103
+ * says otherwise. */
104
+ export interface ArithmeticNode extends Record<string, unknown>, ColumnCopy {
105
+ type: 'arithmetic';
106
+ operator: ArithmeticOperator;
107
+ left: ExpressionNode;
108
+ right: ExpressionNode;
109
+ }
96
110
  export interface LogicalNode extends Record<string, unknown> {
97
111
  type: 'logical';
98
112
  operator: LogicalOperator;
@@ -60,6 +60,11 @@ export declare const DEFAULT_MESSAGES: {
60
60
  removeElse: string;
61
61
  addArg: string;
62
62
  removeArg: string;
63
+ calcFirst: string;
64
+ calcSecond: string;
65
+ calcOperator: string;
66
+ /** Makes a calculation the first value of a new one. */
67
+ wrapInCalculation: string;
63
68
  typeField: string;
64
69
  typeValue: string;
65
70
  /** NULL, picked as a value (no value, on purpose). */
@@ -71,6 +76,8 @@ export declare const DEFAULT_MESSAGES: {
71
76
  typeDate: string;
72
77
  typeBoolean: string;
73
78
  typeFunction: string;
79
+ /** A calculation, `a + b` (issue 26). */
80
+ typeCalculation: string;
74
81
  typeExpression: string;
75
82
  /** A named param (set by the application, shown read-only). */
76
83
  typeParam: string;
@@ -62,7 +62,7 @@ export type IssueLocationStep =
62
62
  } | {
63
63
  kind: 'not';
64
64
  }
65
- /** A comparison's side. */
65
+ /** A comparison's or a calculation's side. */
66
66
  | {
67
67
  kind: 'side';
68
68
  side: 'left' | 'right';
@@ -1,4 +1,4 @@
1
- import type { BinaryNode, BinaryOperator, ExistsNode, ExpressionNode, FieldNode, FunctionCallNode, FunctionSortKey, LiteralNode, ListNode, LiteralValue, LogicalNode, LogicalOperator, NotNode, ParamNode, RelativeDate, SubqueryNode } from './ast.js';
1
+ import type { ArithmeticNode, ArithmeticOperator, BinaryNode, BinaryOperator, ExistsNode, ExpressionNode, FieldNode, FunctionCallNode, FunctionSortKey, LiteralNode, ListNode, LiteralValue, LogicalNode, LogicalOperator, NotNode, ParamNode, RelativeDate, SubqueryNode } from './ast.js';
2
2
  import { type DerivedQuery, type JoinClause, type TableFrom, type JoinType, type QueryPipeline, type QueryValue, type SortDirection } from './queryAst.js';
3
3
  import type { DatabaseSchema } from './schema.js';
4
4
  export declare const expr: {
@@ -17,6 +17,9 @@ export declare const expr: {
17
17
  }) => ParamNode;
18
18
  /** `left <operator> right`. */
19
19
  compare: (left: ExpressionNode, operator: BinaryOperator, right: ExpressionNode) => BinaryNode;
20
+ /** A calculation (issue 26), `left <operator> right`:
21
+ * `expr.calc(price, '*', quantity)`. */
22
+ calc: (left: ExpressionNode, operator: ArithmeticOperator, right: ExpressionNode) => ArithmeticNode;
20
23
  /** `operand IS NULL`. */
21
24
  isNull: (operand: ExpressionNode) => BinaryNode;
22
25
  /** `operand IS NOT NULL`. */
@@ -29,6 +29,12 @@ export declare function writeMarked<T>(marks: SpanMarks, write: () => T): T;
29
29
  * NULL AND b IS NULL))`, the same rows in any database, for SQL Server
30
30
  * before 2022, which has no IS NOT DISTINCT FROM. */
31
31
  export type NullSafeEqual = 'native' | 'expanded';
32
+ /** How a calculation's `/` is written (issue 26). `'decimal'`: with its
33
+ * fraction in every database, `5 / 2` is 2.5 — written `a * 1.0 / b` in
34
+ * PostgreSQL and SQL Server, which drop it when both sides are whole
35
+ * numbers (MySQL's `/` keeps it). `'native'`: `a / b`, each database's
36
+ * own division. */
37
+ export type Division = 'decimal' | 'native';
32
38
  export declare function astToWhereClause(ast: ExpressionNode, dialect: SqlDialect, params?: SqlParams): string;
33
39
  export declare function formatFromClause(from: FromClause, dialect: SqlDialect, params?: SqlParams): string;
34
40
  export declare function formatJoinClause(join: JoinClause, dialect: SqlDialect, params?: SqlParams): string;
@@ -66,6 +72,8 @@ export declare function astToSql(pipeline: QueryPipeline, dialect: SqlDialect, o
66
72
  schema?: DatabaseSchema;
67
73
  /** How a null-safe equal is written (see ParameterizedSqlOptions). */
68
74
  nullSafeEqual?: NullSafeEqual;
75
+ /** How a calculation divides (see ParameterizedSqlOptions). */
76
+ division?: Division;
69
77
  }): string;
70
78
  /** SQL with its values as bound parameters, for executing it. */
71
79
  export interface ParameterizedSql {
@@ -101,6 +109,10 @@ export interface ParameterizedSqlOptions {
101
109
  /** How `IS NOT DISTINCT FROM` is written: `'native'` (default), or
102
110
  * `'expanded'` into `=` and IS NULL for SQL Server before 2022. */
103
111
  nullSafeEqual?: NullSafeEqual;
112
+ /** How a calculation's `/` is written: `'decimal'` (default), with its
113
+ * fraction everywhere (`5 / 2` is 2.5), or `'native'`, each database's
114
+ * own (2 in PostgreSQL and SQL Server for two whole numbers). */
115
+ division?: Division;
104
116
  }
105
117
  /** The query as SQL whose values — in WHERE, JOIN ON and SELECT, including
106
118
  * inside functions, IF/CASE and IN lists — are bound parameters rather than
@@ -2,7 +2,7 @@ import type { LiteralValue } from './ast.js';
2
2
  import type { QueryValue } from './queryAst.js';
3
3
  /** The format saveQuery writes. Raised when a change to the query's shape
4
4
  * needs older stored queries migrated (see MIGRATIONS). */
5
- export declare const QUERY_FORMAT_VERSION = 5;
5
+ export declare const QUERY_FORMAT_VERSION = 6;
6
6
  /** A query as stored: plain JSON, with the format it's in. */
7
7
  export interface StoredQuery {
8
8
  version: number;
@@ -3,7 +3,7 @@ export { MAX_NUMBER_DIGITS, MAX_TEXT_LENGTH, MAX_TYPE_NAME_LENGTH } from './core
3
3
  export { calendarDay, fromIsoDay, isoDay } from './core/calendarDate.js';
4
4
  export { createEmptyQuery } from './core/pipelineFlow.js';
5
5
  export { addMeasure, keepMeasuresInStep, measureFacts, type MeasureFact, type MeasureSpec, } from './core/measures.js';
6
- export type { BinaryNode, BinaryOperator, CaseNode, ExistsNode, ExistsQuery, ExpressionNode, FieldNode, FunctionCallNode, FunctionSortKey, IfNode, LiteralNode, LiteralValue, LogicalNode, LogicalOperator, NotNode, ParamNode, RelativeDate, ListNode, SubqueryNode, } from './core/ast.js';
6
+ export type { ArithmeticNode, ArithmeticOperator, BinaryNode, BinaryOperator, CaseNode, ExistsNode, ExistsQuery, ExpressionNode, FieldNode, FunctionCallNode, FunctionSortKey, IfNode, LiteralNode, LiteralValue, LogicalNode, LogicalOperator, NotNode, ParamNode, RelativeDate, ListNode, SubqueryNode, } from './core/ast.js';
7
7
  export { addUnionBranch, addUnionJoin, addUnionTable, createUnion, removeUnionTable, renameUnionColumn, setUnionAll, unwrapUnion, unionName, unionWithQuery, unionableTables, unionTableColumns, type UnionTableOptions, type UnionTableColumn, type UnionTableColumnKind, type UnionTableSpec, } from './core/unionTables.js';
8
8
  export { SharedBaseError, addSharedQuery, joinSharedQuery, makeShared, makeSubQuery, removeSharedQuery, renameSharedQuery, setSharedBase, sharedQueryName, sharedUses, } from './core/sharedQueries.js';
9
9
  export { joinMultiplicity, repeatedReads, repeatedTables, rowsPerBase, type JoinMultiplicity, } from './core/fanOut.js';
@@ -13,7 +13,7 @@ export { queryParams, type ParamColumn, type QueryParam } from './core/params.js
13
13
  export { relativeDateLabel } from './core/relativeDates.js';
14
14
  export { QUERY_FORMAT_VERSION, QueryFormatError, loadQuery, saveQuery, type StoredQuery, } from './core/storedQuery.js';
15
15
  export type { ResolveName } from './core/resolveNames.js';
16
- export { astToParameterizedSql, astToSql, DIALECTS, DIALECT_LABELS, type NullSafeEqual, type ParameterizedSql, type ParameterizedSqlOptions, type SqlDialect, } from './core/sqlGen.js';
16
+ export { astToParameterizedSql, astToSql, DIALECTS, DIALECT_LABELS, type Division, type NullSafeEqual, type ParameterizedSql, type ParameterizedSqlOptions, type SqlDialect, } from './core/sqlGen.js';
17
17
  export { EMPTY_SCHEMA, filterSchema, validateSchema, type DatabaseColumnType, type DatabaseSchema, type ColumnOption, type Relation, findJoinPath, reachableTables, suggestJoin, type JoinPathOptions, type JoinSuggestion, type SuggestJoinOptions, type SchemaIssue, type SchemaIssueKind, type Table, type TableColumn, type TableGroup, } from './core/schema.js';
18
18
  export { QUERY_ISSUE_KINDS, QUERY_WARNING_KINDS, validateQuery, type QueryIssue, type QueryIssueKind, type QueryWarningKind, type QueryIssueSeverity, type ValidateQueryOptions, } from './core/queryValidation.js';
19
19
  export type { OnExplain, PlanFinding, PlanFindingKind } from './core/planFindings.js';
@@ -7,8 +7,8 @@ import type { SqlQuery, SqlSpan } from './syntax.js';
7
7
  import { type SqlPosition } from './tokenize.js';
8
8
  /** What can stop SQL from being imported:
9
9
  * - `syntax`: SQL that can't be read at all.
10
- * - `unsupported`: SQL the query can't hold (UNION at the top, WITH,
11
- * arithmetic, a window function, …).
10
+ * - `unsupported`: SQL the query can't hold (a window frame, RECURSIVE,
11
+ * `||`, …).
12
12
  * - `unknownTable`, `unknownColumn`: a name the schema doesn't have.
13
13
  * - `ambiguousColumn`: a column name more than one table has.
14
14
  * - `duplicateTable`: two tables of one query by the same name.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nongt/visql-core",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "The query logic of @nongt/visql, for a backend: load, check, scope, write SQL, import SQL, read query plans. No React, no dependencies.",
5
5
  "keywords": [
6
6
  "sql",