Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
module Spi = Catalog.Spi
let of_sql sql = match Lexer.parse_statements sql with | Ok [ Ast.Create_view cv ] -> Some cv | _ -> None
let find cat name = List.find_map (fun (_, (d : Spi.Definition.t)) -> if Spi.Definition.is d `View name then Option.bind d.sql of_sql else None) (Spi.definitions cat)
(* The views this domain is in the middle of expanding, innermost first. A view whose body names itself would expand without end, so the name is recorded for as long as the expansion under it runs. A view reached twice in a row is expanded twice; only one reached from inside its own expansion is a cycle. *)let expanding_key : string list ref Domain.DLS.key = Domain.DLS.new_key (fun () -> ref [])
let expanding name f = let stack = Domain.DLS.get expanding_key in if List.exists (Ast.ident_equal name) !stack then Catalog.Error.fail_prepare "view %s is circularly defined" name; stack := name :: !stack; Fun.protect ~finally:(fun () -> stack := List.tl !stack) f
(* A view's stars are expanded, and its compound's arms compared, where it is read, never where it is created (select.c, sqlite3ViewGetColumnNames). *)let columns (env : Resolve.env) (cv : Ast.create_view) = Resolve.check_stars cv.select; Resolve.check_widths env cv.select; if cv.columns <> [] then cv.columns else let s = cv.select in let from_columns () = (* A derived table publishes what its query yields. *) let derived sub = Resolve.select_columns env sub in let of_ref (tr : Ast.table_ref) = match tr.subquery with | Some sub -> derived sub | None -> env.columns tr.name in let main = match s.from with | Ast.Table tr -> of_ref tr | Select { fs_select; _ } -> derived fs_select | Unit | Values _ | Table_function _ -> [] in main @ List.concat_map (fun (j : Ast.join) -> of_ref j.table) s.joins in List.concat_map (fun (rc : Ast.result_col) -> match rc.alias with | Some a -> [ a ] | None -> ( match rc.expr with | Star -> from_columns () (* a COLLATE names a sequence, not a column, so the name under it is the one published (Resolve.named_column) *) | e -> ( match Resolve.named_column e with | Some c -> [ c ] | None -> [ Render.expr e ]))) s.cols
(* What the result column [col] of the view carries: [f] over its position in the view's columns. *)let column env cv col f = Option.bind (Catalog.Ident.index col (columns env cv)) (f env cv.Ast.select)
let collation env cv col = column env cv col Resolve.output_collationlet affinity env cv col = column env cv col Resolve.output_affinitylet decl_type env cv col = column env cv col Resolve.output_type
(* [base] answering for the views [view] finds too, each answer for one worked out under {!expanding}, so a view read through itself is refused where its columns are first asked for; [run] is a view's rows. *)let viewing (base : Query.catalog) ~view ~env ~run = let on name ~table f = match view name with | Some cv -> expanding name (fun () -> f cv) | None -> table () in let rows name = on name ~table:(fun () -> base.rows name) run in let unless_view name f = match view name with Some _ -> None | None -> f () in let column name col ~table f = on name ~table:(fun () -> table name col) (fun cv -> f (env ()) cv col) in { base with columns = (fun name -> on name ~table:(fun () -> base.columns name) (columns (env ()))); rows; rows_rowid = (fun name -> match view name with | Some _ -> List.map (fun r -> (None, r)) (rows name) | None -> base.rows_rowid name); has_rowid = (fun name -> Option.is_none (view name) && base.has_rowid name); holds = (fun name -> base.holds name || Option.is_some (view name)); index_scan = (fun name eqs -> unless_view name (fun () -> base.index_scan name eqs)); best_index_for = (fun name cols -> unless_view name (fun () -> base.best_index_for name cols)); affinity = (fun name col -> column name col ~table:base.affinity affinity); decl_type = (fun name col -> column name col ~table:base.decl_type decl_type); collation = (fun name col -> column name col ~table:base.collation collation); row_count = (fun name ~upto -> match view name with | Some _ -> upto | None -> base.row_count name ~upto); }
(* A name is looked up as sqlite3LocateTable looks it up: a table first, then a view. *)let over cat base = let view name = if List.exists (Ast.ident_equal name) (Spi.tables cat) then None else find cat name in let rec q = lazy (viewing base ~view ~env:(fun () -> Query.resolve_env (Lazy.force q)) ~run:(fun cv -> let self = Lazy.force q in self.subquery self [] cv.Ast.select [||])) in Lazy.force q
(* {1 Statements} *)
type holder = [ `Table | `View | `Index ]
(* The questions sqlite3CreateView asks, in its order: the placeholders, then the name among tables, views and indexes (build.c, sqlite3CheckObjectName, sqlite3StartTable). *)let check_create ~(held : holder option) (cv : Ast.create_view) = if Param_number.select_has_param cv.select then Catalog.Error.fail_prepare "parameters are not allowed in views"; match held with | None -> true | Some `Index -> Catalog.Error.fail_prepare "there is already an index named %s" cv.name | Some (`Table | `View) when cv.if_not_exists -> false | Some `Table -> Catalog.Error.fail_prepare "table %s already exists" cv.name | Some `View -> Catalog.Error.fail_prepare "view %s already exists" cv.name