IF Commons GitHub

physical/sql

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0003-sql-port FR-002

The statements of a store, built by the language of SQL: a name is written by identifier of the dialect, a parameter by placeholder in the order it is added, and no value is ever part of the text, so that a path or a prefix with a quote, a percent sign or a backslash in it is only a value. Each dialect differs in three small things the builder hides: the type a column has, how bytes are given to the engine (DuckDB takes them as hex, since it cannot type a binary parameter) and how a row is locked for writing (PostgreSQL's FOR UPDATE).

import { sqlFor } from "./sql.ts";
const sql = sqlFor("postgres", naming);
sql.query().text("SELECT rev FROM ").table("objects").text(" WHERE path = ").param("a").done();
// { text: 'SELECT rev FROM anyfs_objects WHERE path = $1', params: ["a"] }

Source: commons/anyfs-sql/src/physical/sql.ts

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

sqlForfunction
function sqlFor(dialect: "duckdb" | "postgres" | "sqlite", naming: Naming): { dialect: "duckdb" | "postgres" | "sqlite";… (418 characters)
function sqlFor(dialect: "duckdb" | "postgres" | "sqlite", naming: Naming): { dialect: "duckdb" | "postgres" | "sqlite"; language: Dialect; query: () => Query; name: (text: string) => string; table: (role: "blobs" | "cache" | "epochs" | "heads" | "meta" | "names" | "search" | "siblings" | "trust" | "vector" | "versions") => string; byteLength: string; type: (kind: "bytes" | "integer" | "path" | "text") => string; }

The statements of the store of a naming in a dialect.

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

Querytypeno test imports it
type Query = { /** Adds text written by this library (a keyword, an operator): never a value and never a name. */ te… (908 characters)
type Query = {
  /** Adds text written by this library (a keyword, an operator): never a value and never a name. */
  text(sql: string): Query;
  /** Adds a parameter: its placeholder goes into the text, and the value into the parameters. */
  param(value: SqlValue): Query;
  /** Adds the name of a table of the store (with its schema where there is one). */
  table(role: TableRole): Query;
  /** Adds a name written as an identifier of the dialect. */
  name(name: string): Query;
  /** Adds the bytes of an object as a value for a binary column. */
  bytes(value: Uint8Array): Query;
  /** Adds the binary column as it is selected (of a table given by an alias where there is one), so that its bytes come back as bytes or as hex. */
  selectBytes(column: string, qualifier?: string): Query;
  /** Adds ` FOR UPDATE` where the dialect locks a row that way. */
  forUpdate(): Query;
  done(): Statement;
};

The builder of one statement.

Sqltypeno test imports it
type Sql = ReturnType<typeof sqlFor>;

The statements of one store.

Statementtypeno test imports it
type Statement = { readonly text: string; readonly params: readonly SqlValue[] };

A statement: its text and the parameters in the order they appear.

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