(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) type t = Any | Blob | Int | Integer | Real | Text let of_type_name ty = match String.uppercase_ascii ty with | "ANY" -> Some Any | "BLOB" -> Some Blob | "INT" -> Some Int | "INTEGER" -> Some Integer | "REAL" -> Some Real | "TEXT" -> Some Text | _ -> None let to_string = function | Any -> "ANY" | Blob -> "BLOB" | Int -> "INT" | Integer -> "INTEGER" | Real -> "REAL" | Text -> "TEXT" let pp = Fmt.of_to_string to_string (* A column's affinity is the one its declared type name carries, with one override: a STRICT ANY column takes BLOB affinity rather than the NUMERIC affinity the name would carry outside a STRICT table. BLOB affinity converts nothing, so ANY and BLOB values keep the storage class they were written in. *) let affinity : t -> Value_ops.affinity = function | Any | Blob -> `Blob | Int | Integer -> `Integer | Real -> `Real | Text -> `Text let accepts t (v : Catalog.Value.t) = match v with | Null -> true | _ -> ( match t with | Any -> true | Blob -> ( match v with Blob _ -> true | _ -> false) | Int | Integer -> ( match v with Int _ -> true | _ -> false) (* REAL affinity has already turned an integer into a real; an integer that survived it (one too large to be one) is a real all the same. *) | Real -> ( match v with Float _ | Int _ -> true | _ -> false) | Text -> ( match v with Text _ -> true | _ -> false)) let storage_class (v : Catalog.Value.t) = match v with | Null -> "NULL" | Int _ -> "INT" | Float _ -> "REAL" | Text _ -> "TEXT" | Blob _ -> "BLOB"