IF Commons GitHub

emit

Quoting and placeholders (commons lang-sql 0007-sql-emit FR-001 to FR-005): what the lexical of a dialect already knows, as functions, so that a library that builds statements writes no quote and no parameter by hand. An identifier is quoted as the dialect's first quoted-identifier syntax writes it, a string as its first string syntax writes it, and a parameter in the style the dialect reads; each result is read back by the lexer of the same dialect as one token.

quoteIdent(postgres, 'a"b');        // ok('"a""b"')
identifier(postgres, "user");       // ok('"user"'): a keyword is quoted
quoteString(mysql, "it's");         // ok("'it\\'s'")
placeholder(postgres, 2);           // ok("$2")
placeholder(sqlite, 1, "name");     // ok(":name")

Spec: 0007-sql-emit FR-001, FR-002, FR-004

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

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

describeEmitErrorfunction
function describeEmitError(e: EmitError): string

One sentence for an emit error.

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

EmitErrortypeno test imports it
type EmitError =
  | { readonly kind: "no-quoted-identifier"; readonly dialect: string }
  | { readonly kind: "no-string"; readonly dialect: string }
  | { readonly kind: "cannot-escape"; readonly dialect: string; readonly quote: string }
  | { readonly kind: "no-placeholder"; readonly dialect: string; readonly wanted: "named" | "positional" };

Why a name, a value or a parameter could not be written for a dialect.

identifierfunction
function identifier(dialect: Dialect, name: string): Result<string, EmitError>

The name bare where it is a plain lower-case word that is not a keyword of the dialect, and quoted otherwise.

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

paramStylesfunction
function paramStyles(dialect: Dialect): readonly ParamStyle[]

The styles of parameter the dialect reads, in the order its lexical lists them.

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

placeholderfunction
function placeholder(dialect: Dialect, position: number, name?: string | undefined): Result<string, EmitError>

The text of a parameter: for a name, the first of the colon, at and dollar styles the dialect has; for a position (from 1), $n or ?; an error value where none fits.

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

qualifiedfunction
function qualified(dialect: Dialect, parts: readonly string[]): Result<string, EmitError>

The parts joined by ., each as identifier gives it.

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

quoteIdentfunction
function quoteIdent(dialect: Dialect, name: string): Result<string, EmitError>

The name written as the dialect's first quoted-identifier syntax writes it; an error value where the dialect has none.

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

quoteStringfunction
function quoteString(dialect: Dialect, value: string): Result<string, EmitError>

The value written as the dialect's first string syntax writes it; an error value where the dialect has none, or where the quote cannot be written inside.

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

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