(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) open Ast (* A bare [?] states no index of its own, and the one it is given belongs to the whole statement rather than to the expression it stands in, so the parser leaves it unnumbered and this pass assigns it. Negative, so the two readings cannot be confused and a placeholder that never reached this pass reads no bound value rather than the first one. *) let unnumbered = -1 (* How a placeholder states the parameter it reads: by an index, which is {!unnumbered} for a bare [?], or by a name. *) type written = Index of int | Name of string (* Map a list left to right, one element finished before the next is begun. [List.map] leaves the order of its applications unspecified, and every traversal here reads it: [next] is a monotonic counter, so an element mapped out of turn takes a number the text gives another. Every list a statement writes side by side goes through this rather than through [List.map]. *) let rec ordered_map f = function | [] -> [] | x :: rest -> let y = f x in y :: ordered_map f rest let rec renum_expr next e = match e with | Param i -> Param (next (Index i)) | Named_param s -> Param (next (Name s)) | e -> Ast.map_expr ~select:(renum_select next) (renum_expr next) e (* A [WITH] clause is written before whatever reads it -- a SELECT, but also a DELETE or an UPDATE, which is why this is its own function. *) and renum_ctes next ctes = ordered_map (fun (c : cte) -> { c with query = renum_select next c.query }) ctes and renum_from next = function | (Unit | Table _) as f -> f | Select fs -> Select { fs with fs_select = renum_select next fs.fs_select } | Values rows -> Values (ordered_map (ordered_map (renum_expr next)) rows) | Table_function tf -> Table_function { tf with tf_args = ordered_map (renum_expr next) tf.tf_args } and renum_joins next joins = ordered_map (fun (j : join) -> let table = match j.table.subquery with | Some q -> { j.table with subquery = Some (renum_select next q) } | None -> j.table in { j with table; on = Option.map (renum_expr next) j.on }) joins (* Source order: SELECT cols, FROM (subquery), JOINs, WHERE, ORDER BY, LIMIT. *) and renum_select next (s : select) = let ctes = renum_ctes next s.ctes in let cols = ordered_map (fun (rc : result_col) -> { rc with expr = renum_expr next rc.expr }) s.cols in let from = renum_from next s.from in let joins = renum_joins next s.joins in let where = Option.map (renum_expr next) s.where in let group_by = ordered_map (renum_expr next) s.group_by in let having = Option.map (renum_expr next) s.having in let compound = ordered_map (fun (op, arm) -> (op, renum_select next arm)) s.compound in let order_by = ordered_map (fun (e, d) -> (renum_expr next e, d)) s.order_by in let limit = Option.map (renum_expr next) s.limit in let offset = Option.map (renum_expr next) s.offset in { s with ctes; cols; from; joins; where; group_by; having; compound; order_by; limit; offset; } (* A list of expressions written side by side -- the tuple of an INSERT, the arguments of a call -- numbered in the order it is written. *) let renum_exprs next = ordered_map (renum_expr next) (* Whether the query holds a [?] at all, answered by the numbering traversal rather than by a walk of its own: the numbering already reaches every expression a select can hold, and a second traversal would be one more thing to keep in step with the AST. *) let select_has_param s = let found = ref false in let next _ = found := true; 0 in ignore (renum_select next s); !found let rec renum_stmt next stmt = let e = renum_expr next and es = renum_exprs next in let rcs l = ordered_map (fun (rc : result_col) -> { rc with expr = e rc.expr }) l in match stmt with | Select s -> Select (renum_select next s) | Insert (i : insert) -> let source = match i.source with | Values rows -> Values (ordered_map es rows) | Select s -> Select (renum_select next s) | Default_values -> Default_values in let upsert = Option.map (fun (u : upsert) -> (* The target's own WHERE is numbered before the DO UPDATE's, since that is the order the statement writes them in. *) let target_where = Option.map e u.target_where in match u.action with | Nothing -> { u with target_where } | Update (sets, w) -> let sets = ordered_map (fun (c, x) -> (c, e x)) sets in { u with target_where; action = Update (sets, Option.map e w) }) i.upsert in let returning = rcs i.returning in Insert { i with source; upsert; returning } | Delete d -> let ctes = renum_ctes next d.ctes in let where = Option.map e d.where in Delete { d with ctes; where; returning = rcs d.returning } | Update u -> let ctes = renum_ctes next u.ctes in let sets = ordered_map (fun (c, x) -> (c, e x)) u.sets in (* [UPDATE t SET ... FROM u JOIN v ON ...]: written after the assignments and before the WHERE, and its operands carry placeholders like any other FROM. *) let from = Option.map (fun (f, joins) -> (renum_from next f, renum_joins next joins)) u.from in let where = Option.map e u.where in Update { u with ctes; sets; from; where; returning = rcs u.returning } | Create_table_as cta -> Create_table_as { cta with query = renum_select next cta.query } (* An EXPLAIN takes no parameters of its own, and the statement it describes numbers its own from one -- the same numbering it would get run alone, so that describing a statement and running it bind the same values. *) | Explain e -> Explain { e with explained = renum_stmt next e.explained } | Vacuum into -> Vacuum (Option.map e into) (* A trigger's own body and WHEN are not numbered: a stored program has no statement to bind a placeholder in, and {!stmt_has_param} is what refuses one there. The rest declare no expression a caller binds. *) | ( Create_table _ | Create_index _ | Create_view _ | Create_trigger _ | Drop _ | Alter_table _ | Pragma _ | Begin _ | Commit | Rollback | Savepoint _ | Release _ | Rollback_to _ | Analyze ) as s -> s (* A name takes the index after the largest so far where it first appears, and keeps it: sqlite3ExprAssignVarNumber (src/expr.c) looks the spelling up in the names it has numbered before it counts another. *) let numbering () = (* the number before the first, so the first bare [?] takes 0 *) let largest = ref (-1) in let names = Hashtbl.create 8 in let take written = let i = if written < 0 then !largest + 1 else written in if i > !largest then largest := i; i in function | Index written -> take written | Name s -> ( match Hashtbl.find_opt names s with | Some i -> i | None -> let i = take unnumbered in Hashtbl.add names s i; i) let number_params stmt = renum_stmt (numbering ()) stmt let number_select s = renum_select (numbering ()) s (* Whether [f] writes a [?] anywhere, answered by the numbering traversal it is given rather than by a walk of its own: the numbering already reaches every expression, and a second traversal would be one more thing to keep in step with the AST. *) let holds_param f = let found = ref false in let next _ = found := true; 0 in ignore (f next); !found let stmt_has_param stmt = holds_param (fun next -> renum_stmt next stmt) let expr_has_param e = holds_param (fun next -> renum_expr next e)