Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
module Spi = Catalog.Spi
(* sqlite/src/pragma.c, PragTyp_INTEGRITY_CHECK: the message budget when the pragma is given no count, and the one a count of zero or less falls back to ([SQLITE_INTEGRITY_CHECK_ERROR_MAX]). *)let error_max = 100
(* sqlite3 reads the budget off the token as written, without the minus of a minus_num, so [integrity_check(-2)] is a budget of 2 and [integrity_check('2')] a table named "2". *)let argument = function | Some (Ast.Num s as v) -> ( let token = if String.starts_with ~prefix:"-" s then String.sub s 1 (String.length s - 1) else s in match Pragma_value.int32 token with | Some n -> ((if n <= 0 then error_max else n), None) | None -> (error_max, Some (Ast.pragma_text v))) | Some (Name name) -> (error_max, Some name) | None -> (error_max, None)
let null_value ~table ~column = Fmt.str "NULL value in %s.%s" table column
let wrong_datatype ~declared ~table ~column = Fmt.str "non-%s value in %s.%s" declared table column
let numeric_in_text_column ~table ~column = Fmt.str "NUMERIC value in %s.%s" table column
let text_in_numeric_column ~table ~column = Fmt.str "TEXT value in %s.%s" table column
let check_violated ~table = Fmt.str "CHECK constraint failed in %s" table
let has_rule = function | `Blob -> false | `Text | `Numeric | `Integer | `Real -> true
(* A NOT NULL column holding NULL, then a STRICT column's declared type or an ordinary column's affinity: text in a TEXT column, a number a numeric column would have read out of the text it holds (pragma.c, the type checks of PragTyp_INTEGRITY_CHECK). *)let value_problem ~table ~column ~not_null ~(storage : Table.storage) ~affinity v = match (v, not_null, storage) with | Ast.Null, true, _ -> Some (null_value ~table ~column) | _, _, Strict st when not (Strict.accepts st v) -> Some (wrong_datatype ~declared:(Strict.to_string st) ~table ~column) | _, _, Strict _ -> None | _ -> ( match affinity with | `Text -> ( match v with | Ast.Null | Text _ | Blob _ -> None | _ -> Some (numeric_in_text_column ~table ~column)) | `Numeric | `Integer | `Real -> ( match v with | Ast.Text _ -> ( match Value_ops.apply_affinity `Numeric v with | Ast.Int _ | Float _ -> Some (text_in_numeric_column ~table ~column) | _ -> None) | _ -> None) | `Blob -> None)
(* The rules a row of [s] meets, one per column a rule reaches, in table order: the rowid's alias, and a column that is neither NOT NULL, STRICT nor of an affinity with a rule, is never read. *)let column_rules (s : Spi.Schema.t) table = let alias = Spi.Schema.rowid_alias s in List.filter_map Fun.id (List.mapi (fun i ((c : Spi.Schema.column), (storage : Table.storage)) -> let affinity = match storage with Strict Any -> `Blob | st -> Table.affinity st in let strict = match storage with Strict _ -> true | _ -> false in if alias = Some i || not (c.not_null || strict || has_rule affinity) then None else Some (i, c.name, c.not_null, storage, affinity)) (List.combine s.columns (Table.storage table)))
(* The problems of every row of [name], in order, no more than [budget]. *)let table_problems cat ~budget name = match Spi.schema cat name with | None -> [] | Some s -> let table = Table.v s in let rules = column_rules s table in let checks = match Table.checks table with | [] -> None | exprs -> Some (Conflict.checks (Table.catalog table) [||] ~table:s.table ~cols:(Spi.Schema.column_names s) exprs) in let found = ref [] and n = ref 0 in let add m = found := m :: !found; incr n; if !n >= budget then raise_notrace Exit in (try List.iter (fun (rowid, values) -> let values = Array.of_list values in List.iter (fun (i, column, not_null, storage, affinity) -> Option.iter add (value_problem ~table:s.table ~column ~not_null ~storage ~affinity (if i < Array.length values then values.(i) else Ast.Null))) rules; Option.iter (fun (c : Conflict.checks) -> if Option.is_some (c.failing ?rowid (Array.to_list values)) then add (check_violated ~table:s.table)) checks) (Spi.rows_rowid cat name) with Exit -> ()); List.rev !found
let check cat ~budget table = let names = match table with Some t -> [ t ] | None -> Spi.tables cat in let rec go budget acc = function | [] -> acc | _ when budget <= 0 -> acc | name :: rest -> let found = table_problems cat ~budget name in go (budget - List.length found) (acc @ found) rest in match go budget [] names with [] -> [ "ok" ] | l -> l