Skip to content

Add SQL casts, list comparisons, and JSON expressions - #89

Open
elcritch wants to merge 5 commits into
Araq:masterfrom
elcritch:feature/postgres-expression-syntax
Open

elcritch wants to merge 5 commits into
Araq:masterfrom
elcritch:feature/postgres-expression-syntax

Conversation

@elcritch

@elcritch elcritch commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Ormin queries need explicit casts, bound-list comparisons, JSON extraction, and NULL-safe comparisons. This adds those expressions for PostgreSQL and SQLite, plus PostgreSQL-specific array and JSONB operators.

let ids = @[1, 3, 5]
let rows = query:
  select records(id, (payload ->> "name") as name)
  where id == any(?ids)
  orderby id

The sequence stays one bound parameter. PostgreSQL receives an array; SQLite receives a JSON array and searches it with json_each. SQLite supports == any, == some, and != all with bound sequences, including empty and NULL arrays. Strings are encoded as data, including quotes, backslashes, and SQL-shaped text.

Use a backtick-quoted type after as to cast a value. For example, (?value as `text`) produces ($1)::text in PostgreSQL and CAST(? AS text) in SQLite. Unquoted as label names a result column; backticks are reserved for supported cast types. SQLite supports common text, integer, and real casts; backend conversion rules still apply.

The change also includes:

  • JSON ->/->> extraction and isDistinctFrom/isNotDistinctFrom on both backends. SQLite JSON and type conversion rules still apply; JSON support requires SQLite 3.38 or later with JSON enabled, and NULL-safe comparisons require SQLite 3.39 or later.
  • PostgreSQL array containment and overlap, JSONB containment, JSON path helpers, and additional ANY/ALL comparisons.
  • PostgreSQL schema imports for array columns and double precision, with UUIDs, arrays, and exact decimals returned as strings.
  • Database type information preserved through CTEs, and qualified column references resolved to generated table aliases.
  • README examples and tests covering both backends, generated routines, parameter ordering, empty and NULL arrays, escaped values, JSON null versus missing values, invalid types, and schema round-trips.

Validated locally with Nim 2.4.2 and PostgreSQL 14: nim test, nim test_postgres, and nim buildexamples pass. The SQLite expression tests also pass in release and danger builds and on Nim 2.2.12.

@elcritch
elcritch marked this pull request as draft November 14, 2026 01:51
@elcritch elcritch changed the title Add PostgreSQL casts, array comparisons, and JSON expressions Add SQL casts, list comparisons, and JSON expressions Nov 14, 2026
@elcritch
elcritch marked this pull request as ready for review November 14, 2026 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant