Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
module Spi = Catalog.Spi
let columns = [ "type"; "name"; "tbl_name"; "rootpage"; "sql" ]let is_name name = Ast.is_schema_table name || Ast.is_temp_schema_table name
let name n = if Ast.is_temp_schema_table n then Ast.temp_schema_table else "sqlite_master"
let type_name : Spi.Definition.kind -> string = function | `Table -> "table" | `Index -> "index" | `View -> "view" | `Trigger -> "trigger"
(* A store's definitions carry no b-tree, so each row has the root page sqlite3 gives an object it keeps none for: 0 (fileformat2.html, 2.6). *)let rows cat = List.map (fun (rowid, (d : Spi.Definition.t)) -> ( Some rowid, [ Catalog.Value.Text (type_name d.kind); Text d.name; Text d.table; Int 0L; (match d.sql with Some s -> Text s | None -> Null); ] )) (Spi.definitions cat)
(* The temp database this engine opens holds no object, so its schema table has no row. *)let rows_rowid cat name = if Ast.is_temp_schema_table name then [] else rows cat
let over cat (base : Query.catalog) : Query.catalog = let unless_schema name f = if is_name name then None else f () in { base with columns = (fun name -> if is_name name then columns else base.columns name); rows = (fun name -> if is_name name then List.map snd (rows_rowid cat name) else base.rows name); rows_rowid = (fun name -> if is_name name then rows_rowid cat name else base.rows_rowid name); has_rowid = (fun name -> is_name name || base.has_rowid name); holds = (fun name -> is_name name || base.holds name); index_scan = (fun name eqs -> unless_schema name (fun () -> base.index_scan name eqs)); best_index_for = (fun name cols -> unless_schema name (fun () -> base.best_index_for name cols)); affinity = (fun name col -> unless_schema name (fun () -> base.affinity name col)); decl_type = (fun name col -> unless_schema name (fun () -> base.decl_type name col)); collation = (fun name col -> unless_schema name (fun () -> base.collation name col)); row_count = (fun name ~upto -> if is_name name then upto else base.row_count name ~upto); }
(* sqlite3 locates a schema table a DROP names, finds its "sqlite_" prefix and stops before asking whether DROP TABLE was aimed at a view, so each name of it is refused under the table's own (build.c, sqlite3DropTable). Under [temp] every name of it is the temp one; under another schema it is missing. *)let check_drop (d : Ast.drop) = let refuse name = Catalog.Error.fail_prepare "table %s may not be dropped" name in match (d.object_, d.schema) with | (Table | View), None when is_name d.name -> refuse (name d.name) | (Table | View), Some s when Ast.ident_equal s "temp" && is_name d.name -> refuse Ast.temp_schema_table | (Table | View | Index | Trigger), _ -> ()