@nongt/visql 0.11.0 → 0.12.1

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.
@@ -29,6 +29,9 @@ export declare const ROOT_PATH: AbsolutePath;
29
29
  * (see ExpressionView). Shared so drillStack reconstruction/pruning can
30
30
  * reuse the exact same rule. */
31
31
  export declare const CONDITION_DRILL_TYPES: Set<string>;
32
+ /** Types that open the function sub-canvas (mode 'expression'): a call, or
33
+ * a calculation, whose sides are drawn there like a call's arguments. */
34
+ export declare const EXPRESSION_DRILL_TYPES: Set<string>;
32
35
  /** Read the node at `path`, or undefined if any hop doesn't resolve (wrong
33
36
  * node type at that point, out-of-range index, or an empty optional slot). */
34
37
  export declare function getAtAbsolutePath(root: ExpressionNode | undefined, path: AbsolutePath): ExpressionNode | undefined;
@@ -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;
@@ -5,7 +5,7 @@ interface Props {
5
5
  onChange?: (exp: ExpressionNode) => void;
6
6
  /** Types not to offer here — e.g. a plain value as an ORDER BY key, where
7
7
  * it means nothing (and `ORDER BY 1` would mean "the first column"). */
8
- exclude?: readonly ('field' | 'literal' | 'param' | 'function' | 'if' | 'case')[];
8
+ exclude?: readonly ('field' | 'literal' | 'param' | 'function' | 'arithmetic' | 'if' | 'case')[];
9
9
  /** Only a label, as in read-only mode: no menu, can't be clicked. For a
10
10
  * type chosen elsewhere, e.g. the call at the top of a function's
11
11
  * sub-canvas. */
@@ -0,0 +1,5 @@
1
+ import { type Node, type NodeProps } from '@xyflow/react';
2
+ import type { CalculationNodeData } from '../../expressionFlow.js';
3
+ type CalculationFlowNodeType = Node<CalculationNodeData>;
4
+ declare const CalculationFlowNode: ({ data }: NodeProps<CalculationFlowNodeType>) => import("react/jsx-runtime").JSX.Element;
5
+ export default CalculationFlowNode;
@@ -1,6 +1,6 @@
1
1
  import type { Edge, Node } from '@xyflow/react';
2
- import type { BinaryNode, CaseNode, ExistsNode, ExpressionNode, FunctionCallNode, FunctionSortKey, LogicalNode, NotNode } from './ast.js';
3
- import { type AbsolutePath } from './absolutePath.js';
2
+ import type { ArithmeticNode, ArithmeticOperator, BinaryNode, CaseNode, ExistsNode, ExpressionNode, FunctionCallNode, FunctionSortKey, LogicalNode, NotNode } from './ast.js';
3
+ import { type AbsolutePath, type AbsoluteStep } from './absolutePath.js';
4
4
  import { type ArgDef, type FunctionRegistry } from './functionRegistry.js';
5
5
  /** 'rtl': children left, operator right (grows left on add)
6
6
  * 'ltr': operator left, children right (grows right on add) */
@@ -328,10 +328,26 @@ export declare function applyFlowAction(ast: ExpressionNode, action: FlowAction)
328
328
  /** The structural path a FlowAction most directly touches — used only to decide
329
329
  * where to offer post-undo/redo navigation, not for mutation correctness. */
330
330
  export declare function flowActionTargetPath(action: FlowAction): AbsolutePath;
331
+ /** What the function canvas draws as a node with inputs: a call, its
332
+ * arguments, or a calculation (issue 26), its two sides. */
333
+ export type CanvasCall = FunctionCallNode | ArithmeticNode;
334
+ export declare const isCanvasCall: (node: ExpressionNode) => node is CanvasCall;
335
+ /** A canvas call's inputs, in order: a call's arguments, a calculation's
336
+ * left then right side. A canvas path's numbers index these. */
337
+ export declare const canvasChildren: (node: CanvasCall) => ExpressionNode[];
331
338
  export interface FunctionCallNodeData extends Record<string, unknown> {
332
339
  fn: FunctionCallNode;
333
- /** Path through function.args[] to this function node. [] = root. */
340
+ /** Path through the canvas calls' inputs to this function node. [] = root. */
334
341
  path: number[];
342
+ /** The same place as path steps, from the canvas's root. */
343
+ steps: AbsoluteStep[];
344
+ direction: FlowDirection;
345
+ dispatch: ExpressionFlowDispatch;
346
+ }
347
+ export interface CalculationNodeData extends Record<string, unknown> {
348
+ calc: ArithmeticNode;
349
+ path: number[];
350
+ steps: AbsoluteStep[];
335
351
  direction: FlowDirection;
336
352
  dispatch: ExpressionFlowDispatch;
337
353
  }
@@ -339,6 +355,10 @@ export interface FunctionArgNodeData extends Record<string, unknown> {
339
355
  arg: ExpressionNode;
340
356
  /** Full path: [...parentFnPath, argIndex]. */
341
357
  path: number[];
358
+ /** The same place as path steps, from the canvas's root. */
359
+ steps: AbsoluteStep[];
360
+ /** A calculation's side rather than an argument: which one. */
361
+ side?: 'left' | 'right';
342
362
  direction: FlowDirection;
343
363
  dispatch: ExpressionFlowDispatch;
344
364
  /** ArgDef from the function registry, if the parent function is a known function. */
@@ -420,6 +440,18 @@ export type ExpressionFlowAction =
420
440
  type: 'exprSetPartitionBy';
421
441
  path: number[];
422
442
  partitionBy: ExpressionNode[];
443
+ }
444
+ /** The operator of the calculation at path (issue 26). */
445
+ | {
446
+ type: 'exprSetOperator';
447
+ path: number[];
448
+ operator: ArithmeticOperator;
449
+ }
450
+ /** The calculation at path, as it is, made the first value of a new one
451
+ * (issue 31): how its whole goes into another after it's built. */
452
+ | {
453
+ type: 'exprWrapCalc';
454
+ path: number[];
423
455
  };
424
456
  /** What a COUNT counts: every row (`COUNT(*)`, no argument), the rows
425
457
  * where a value is set (`COUNT(x)`), or the different values
@@ -429,8 +461,11 @@ export declare function countMode(fn: FunctionCallNode): CountMode;
429
461
  export type ExpressionFlowDispatch = (action: ExpressionFlowAction) => void;
430
462
  export declare function applyExpressionFlowAction(ast: ExpressionNode, action: ExpressionFlowAction): ExpressionNode;
431
463
  /** The function-canvas nodes (expressionToFunctionFlow) on the way to the
432
- * spot `path` names, outermost first: each call down its argument steps,
433
- * and the argument itself (a call's node, or a leaf argument's). */
464
+ * spot `path` names, outermost first: each call (or calculation) down its
465
+ * argument or side steps, and the input itself (a call's node, or a leaf
466
+ * input's). A side step is read as a calculation's: past a comparison
467
+ * (whose sides are on a panel of their own) it names nodes that aren't on
468
+ * this canvas, which FocusPulse passes over for the deepest one that is. */
434
469
  export declare function functionFlowNodeIds(path: AbsolutePath): string[];
435
470
  export declare function expressionToFunctionFlow(ast: ExpressionNode, dispatch: ExpressionFlowDispatch, functions?: FunctionRegistry, direction?: FlowDirection): FlowGraph;
436
471
  export declare function layoutFunctionFlow(rawNodes: Node[], prevNodes: Node[], ast: ExpressionNode, direction?: FlowDirection): Node[];
@@ -1,4 +1,4 @@
1
- import type { ExpressionNode } from './ast.js';
1
+ import type { ArithmeticOperator, ExpressionNode } from './ast.js';
2
2
  import { type FunctionRegistry } from './functionRegistry.js';
3
3
  import { type DatabaseSchema } from './schema.js';
4
4
  /** What a part of a formula is: a column, a value, a param, a function's
@@ -18,6 +18,9 @@ export interface FormulaOptions {
18
18
  * in are left out (`…`) until it fits, the outermost kept. */
19
19
  budget?: number;
20
20
  }
21
+ /** How a calculation's operator reads (issue 26): as written by hand, not
22
+ * as SQL has it. */
23
+ export declare const ARITHMETIC_SYMBOLS: Record<ArithmeticOperator, string>;
21
24
  /** The formula's text: its parts, joined. */
22
25
  export declare function formulaText(parts: FormulaPart[]): string;
23
26
  /** `expression` as a formula, as deep as `budget` allows. */
@@ -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;
@@ -46,6 +46,10 @@ declare const MessagesContext: import("react").Context<{
46
46
  removeElse: string;
47
47
  addArg: string;
48
48
  removeArg: string;
49
+ calcFirst: string;
50
+ calcSecond: string;
51
+ calcOperator: string;
52
+ wrapInCalculation: string;
49
53
  typeField: string;
50
54
  typeValue: string;
51
55
  typeNull: string;
@@ -54,6 +58,7 @@ declare const MessagesContext: import("react").Context<{
54
58
  typeDate: string;
55
59
  typeBoolean: string;
56
60
  typeFunction: string;
61
+ typeCalculation: string;
57
62
  typeExpression: string;
58
63
  typeParam: string;
59
64
  typeRelativeDate: 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;
@@ -17,8 +17,8 @@ export declare function sqlTypeValueType(name: string): ValueType | undefined;
17
17
  export declare function literalValueType(literal: LiteralNode): ValueType | undefined;
18
18
  /** The type of `node`'s value, when it's known: a field's column (by
19
19
  * `fieldType`, which knows the query's tables), a function's result (by
20
- * its signature's `returns`), IF/CASE by their branches, a literal by
21
- * itself. Undefined when nothing says (a param, a field with no column,
20
+ * its signature's `returns`), IF/CASE by their branches, a calculation a
21
+ * number, a literal by itself. Undefined when nothing says (a param, a field with no column,
22
22
  * an unknown function). */
23
23
  export declare function expressionType(node: ExpressionNode | undefined, fieldType: (field: FieldNode) => ValueType | undefined, registry: FunctionRegistry): ValueType | undefined;
24
24
  /** The type a literal in a comparison holds: what it's compared with, when
@@ -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",
3
- "version": "0.11.0",
3
+ "version": "0.12.1",
4
4
  "description": "A visual SQL query editor for React: build a whole query on a node canvas, store it as JSON, turn it into SQL.",
5
5
  "keywords": [
6
6
  "sql",