2025-08-29

(sph sql)

build sql strings from structured data

features

  • handles quoting/escaping automatically
  • uses lists instead of raw strings
  • plain strings can be mixed anywhere
  • expressive where expression builder

usage

  • procedures like sql-select return sql strings
  • utility procedures generate sql fragments

where expressions

lists represent logical conditions:

  • prefix with every / any / not (default = every)
  • conditions: (column value) or (operator column value)
  • operators: = > >= < <= like isnot (and synonyms)
  • column can be single or list
  • value can be atom, flat list (or), or tree list (nested or/and)

example

(sql-select "t" ("c1" "c2") 
  '((any (c1 2) (not (like c1 3))) 
    (every (c2 (4 5)))))

produces

select c1,c2 from t where (c1=2 or not c1 like 3) and (c2=4 and c2=5)

examples

(sql-value
  1 "1"
  "a" "'a'")
(sql-where-expr
  ((("a" 1))) "a=1"
  ((not ("a" 1))) "not a=1")
(sql-create-table
  ("t" "c") "create table t(c)")
(sql-select
  ("t" "c" (("a" 1) ("b" "2"))) "select c from t where a=1 and b='2'")
(sql-insert
  ("t" (("a" . 1) ("b" . "2"))) "insert into t(a,b)values(1,'2')")
(sql-update
  ("t" "c") "update t set c")

plain string example

(sql-select "mytable m" "col1, col2")