IF Commons GitHub

rewrite

Rewriting a tree (commons lang-sql 0003-sql-ast-tools FR-001 to FR-004): from the leaves up, by rules given for sorts of node. A rule for a sort is called for each node that stands in a place of that sort, with its children already rewritten, and returns a node of the same sort or undefined to keep it, so a rule cannot put a node of another sort where one does not belong. A rewrite changes nothing it is given: a node no rule changed is the same object in the result, and the whole tree is, where no rule changed anything.

The sorts are those of ast.ts. A name is an expression where it stands as one (a column), and the expr rule is called for it; where a field is typed as a name (the name of a table, of a function, of a qualifier) the name rule is. The same holds for a statement (the statement rule is called where a script holds it) and the queries and sources inside it. The ranges of the nodes above a replaced one stay those of the text they were read from.

// lower-case every identifier
const lower = rewriteScript(parse("SELECT A FROM T").script, { ident: (i) => (i.name === i.name.toLowerCase() ? undefined : { ...i, name: i.name.toLowerCase() }) });

Spec: 0003-sql-ast-tools FR-001, FR-002, FR-003, FR-004

Source: commons/lang-sql/src/rewrite.ts

6 of 6 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

Rulesinterface
interface Rules

What a rewrite calls: a rule for each sort, each given a node and returning a node of the same sort, or undefined to keep it.

MemberTypeWhat it is
ident?((n: Ident) => Ident | undefined)Called for every identifier.
name?((n: Name) => Name | undefined)Called for a name where a field is typed as a name: a table's, a function's, a qualifier.
expr?((n: Expr) => Expr | undefined)Called for every expression, a column (name) included.
query?((n: Query) => Query | undefined)Called for every query that is not a statement of the script: a subquery, a common table expression, a view's.
source?((n: Source) => Source | undefined)Called for every source in a FROM and in a join.
statement?((n: Statement) => Statement | undefined)Called for every statement of a script.

Imported by 4 tests of the run (4 passed, 0 failed): the library's results

rewriteExprfunction
function rewriteExpr(expr: Expr, rules: Rules): Expr

Rewrites an expression; its own expr rule is called too.

Imported by 4 tests of the run (4 passed, 0 failed): the library's results

rewriteQueryfunction
function rewriteQuery(query: Query, rules: Rules): Query

Rewrites a query; its own query rule is called too.

Imported by 4 tests of the run (4 passed, 0 failed): the library's results

rewriteScriptfunction
function rewriteScript(script: Script, rules: Rules): Script

Rewrites a script.

ParameterWhat it is
scriptthe tree
ruleswhat to call for each sort

Returns: the script, the same object where no rule changed anything

Imported by 9 tests of the run (9 passed, 0 failed): the library's results

rewriteSourcefunction
function rewriteSource(source: Source, rules: Rules): Source

Rewrites a source; its own source rule is called too.

Imported by 4 tests of the run (4 passed, 0 failed): the library's results

rewriteStatementfunction
function rewriteStatement(statement: Statement, rules: Rules): Statement

Rewrites a statement; its own statement rule is called too.

Imported by 4 tests of the run (4 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.