UnistPointinterfaceno test imports itinterface UnistPointA point of a unist position.
| Member | Type | What it is |
|---|---|---|
line | number | |
column | number | |
offset | number |
The syntax tree as a unist tree (commons lang-sql 0003-sql-ast-tools FR-009 to FR-013): toUnist projects a parse, its comments included, to nodes that carry a type (the kind), children or a value, a position of unist points and a data of the fields that are not nodes, so that unist-util-visit, unist-util-select and the rest of the unist utilities read SQL as they read Markdown. An extension of the SQL language core: it imports the core and nothing else, and the types are structural, so that nothing here depends on @types/unist.
A node with children has children, in the order of the text with the comments among them; an identifier, a number, a string, a boolean, NULL and a parameter have a value. A child says in its data which field of its parent it is (field) and its place in a list (index), so nothing of the structure is lost. The children's types are the same for every kind (spec OQ-1).
import { visit } from "unist-util-visit";
const tree = toUnist(parse("SELECT a FROM t"));
visit(tree, "table", (t) => console.log(t.position?.start.line)); // 1Spec: 0003-sql-ast-tools FR-009, FR-010, FR-011, FR-012, FR-013
UnistPointinterfaceno test imports itinterface UnistPointA point of a unist position.
| Member | Type | What it is |
|---|---|---|
line | number | |
column | number | |
offset | number |
UnistPositioninterfaceno test imports itinterface UnistPositionThe position of a unist node.
| Member | Type | What it is |
|---|---|---|
start | UnistPoint | |
end | UnistPoint |
SqlParentinterfaceinterface SqlParentA node with children.
| Member | Type | What it is |
|---|---|---|
type | "add-column" | "add-constraint" | "alias" | "alter-table" | "array" | "assignment" | "between" | "binary" | "boolean" | "call" | "case" | "cast" | "check" | "collate" | "column-def" | "column-option" | "command" | "create-index" | "create-routine" | "create-table" | "create-view" | "cte" | "data-type" | "default" | "delete" | "drop" | "drop-column" | "drop-constraint" | "exists" | "extract" | "field" | "foreign-key" | "frame" | "frame-bound" | "ident" | "in" | "in-query" | "insert" | "interval" | "is" | "join" | "keyword-arg" | "lambda" | "like" | "name" | "named-arg" | "not-null" | "null" | "number" | "on-conflict" | "operator" | "order-item" | "param" | "primary-key" | "reference" | "rename-column" | "rename-table" | "routine-body" | "routine-param" | "scalar-query" | "script" | "select" | "select-item" | "set-op" | "star" | "star-modifier" | "string" | "struct" | "struct-entry" | "subquery" | "subscript" | "table" | "table-fn" | "table-like" | "table-op" | "tuple" | "type-field" | "typed-string" | "unary" | "unique" | "unparsed" | "update" | "values" | "when" | "window-def" | "window-spec" | "with" | |
children | SqlChild[] | |
data | Record<string, unknown> | |
position? | UnistPosition |
SqlLiteralinterfaceno test imports itinterface SqlLiteralA node with a value.
| Member | Type | What it is |
|---|---|---|
type | "add-column" | "add-constraint" | "alias" | "alter-table" | "array" | "assignment" | "between" | "binary" | "boolean" | "call" | "case" | "cast" | "check" | "collate" | "column-def" | "column-option" | "command" | "create-index" | "create-routine" | "create-table" | "create-view" | "cte" | "data-type" | "default" | "delete" | "drop" | "drop-column" | "drop-constraint" | "exists" | "extract" | "field" | "foreign-key" | "frame" | "frame-bound" | "ident" | "in" | "in-query" | "insert" | "interval" | "is" | "join" | "keyword-arg" | "lambda" | "like" | "name" | "named-arg" | "not-null" | "null" | "number" | "on-conflict" | "operator" | "order-item" | "param" | "primary-key" | "reference" | "rename-column" | "rename-table" | "routine-body" | "routine-param" | "scalar-query" | "script" | "select" | "select-item" | "set-op" | "star" | "star-modifier" | "string" | "struct" | "struct-entry" | "subquery" | "subscript" | "table" | "table-fn" | "table-like" | "table-op" | "tuple" | "type-field" | "typed-string" | "unary" | "unique" | "unparsed" | "update" | "values" | "when" | "window-def" | "window-spec" | "with" | |
value | string | boolean | null | |
data | Record<string, unknown> | |
position? | UnistPosition |
SqlCommentinterfaceno test imports itinterface SqlCommentA comment.
| Member | Type | What it is |
|---|---|---|
type | "comment" | |
value | string | |
data | Record<string, unknown> | |
position? | UnistPosition |
SqlChildtypetype SqlChild = SqlParent | SqlLiteral | SqlComment;A child of a node.
UnistOptionsinterfaceno test imports itinterface UnistOptionsWhat toUnist may be asked.
| Member | Type | What it is |
|---|---|---|
comments? | boolean | Whether the comments are nodes of the tree (the default). |
positions? | boolean | Whether each node has a position (the default). |
toUnistfunctionfunction toUnist(parsed: Parsed, options?: UnistOptions): SqlParentProjects a parse to a unist tree.
| Parameter | What it is |
|---|---|
parsed | what parse gave |
options | whether to include comments and positions |
Returns: the tree, whose root is the script node