Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
open Modelopen Declmodule Table = Sql.Private.Table
let table (schema : schema) = Lazy.force schema.tablelet record_defaults (schema : schema) = Lazy.force schema.record_defaultslet has_virtual (schema : schema) = List.exists is_virtual schema.columns
(* sqlite3 3.54.1 stores a REAL column's value one of two ways, by where the statement applies the column affinities. OP_MakeRecord, given them, flags a whole real MEM_IntReal and writes it as an integer when it fits in six bytes, and an integer that does not fit as the real nearest it (src/vdbe.c:3642-3643, 3757; datatype3.html sec.3.2): 0.0 and -0.0 are serial type 8, 5.0 serial type 1, and 2^47 a real. OP_Affinity, which sqlite3GenerateConstraintChecks applies them with before a CHECK or an index entry (src/insert.c:2077, 2426), makes every number a real (vdbe.c:3549-3553): an integer is converted, and a whole real passes through the integer sqlite3VdbeIntegerAffinity makes of it, so -0.0 comes out 0.0. [reals] is the second. The read widens an integer back ({!real_affinity}). *)let real_in_record ~reals v = if reals then match v with | Sql.Ast.Int i -> Sql.Ast.Float (Int64.to_float i) | Sql.Ast.Float f when f = 0. -> Sql.Ast.Float 0. | v -> v else match v with | Sql.Ast.Float f when Float.is_integer f && f >= -0x1p47 && f < 0x1p47 -> Sql.Ast.Int (Int64.of_float f) | Sql.Ast.Int i when i < -0x8000_0000_0000L || i > 0x7FFF_FFFF_FFFFL -> Sql.Ast.Float (Int64.to_float i) | v -> v
let is_real mask j = j >= 0 && j < Bytes.length mask && Bytes.get mask j <> '\000'
let stored_values ~reals (schema : schema) values = match Lazy.force schema.real_cols with | None -> values | Some mask -> List.mapi (fun j v -> if is_real mask j then real_in_record ~reals v else v) values
type record = Sql.Ast.value list
let record values = values
(* One walk over the row: the INTEGER PRIMARY KEY's slot NULL, a REAL column's value as the record stores it, a VIRTUAL column left out. Values past the declared columns pass as they are. *)let to_record ~reals (schema : schema) values = let mask = Lazy.force schema.real_cols in let alias = Option.value (rowid_alias schema) ~default:(-1) in let stored j v = if j = alias then Sql.Ast.Null else match mask with | Some m when is_real m j -> real_in_record ~reals v | _ -> v in let rec walk j columns values = match (columns, values) with | _, [] -> [] | [], values -> values | (c : column) :: cs, v :: vs -> if is_virtual c then walk (j + 1) cs vs else stored j v :: walk (j + 1) cs vs in if mask = None && alias < 0 && not (has_virtual schema) then values else walk 0 schema.columns values
let record_index (schema : schema) pos = let rec go i r = function | [] -> None | (c : column) :: rest -> if i = pos then if is_virtual c then None else Some r else go (i + 1) (if is_virtual c then r else r + 1) rest in go 0 0 schema.columns
let held (schema : schema) record pos = Option.bind (record_index schema pos) (List.nth_opt record)
let rec set_nth i f = function | [] -> [] | v :: vs -> if i = 0 then f v :: vs else v :: set_nth (i - 1) f vs
(* A REAL column does not hold a real: sqlite3 stores an integer-valued one as an integer and widens it back on the way out (fileformat.html sec.2.1; sqlite/src/select.c, sqlite3ColumnDefault), so the row a record spreads to is not the record. Only an integer moves; text and a blob keep their class, and NULL is left where every affinity leaves it. The write runs it backwards ({!real_in_record}). *)let real_affinity (schema : schema) = Option.map is_real (Lazy.force schema.real_cols)
let realify = function | Sql.Ast.Int i -> Sql.Ast.Float (Int64.to_float i) | v -> v
(* A row held as an array, widened in place: the reader's own row. *)let realify_row (schema : schema) row = match Lazy.force schema.real_cols with | None -> () | Some mask -> for j = 0 to min (Bytes.length mask) (Array.length row) - 1 do if Bytes.get mask j <> '\000' then row.(j) <- realify row.(j) done
(* The same over a row held as a list, which is what a whole-row read hands back. *)let realify_values (schema : schema) values = match Lazy.force schema.real_cols with | None -> values | Some mask -> List.mapi (fun j v -> if is_real mask j then realify v else v) values
let pad defs (schema : schema) values = let n_cols = List.length schema.columns in let len = List.length values in if len >= n_cols then values else values @ List.init (n_cols - len) (fun i -> defs.(len + i))
let pad_record schema values = pad (record_defaults schema) schema valueslet pad_values schema values = pad (Table.defaults (table schema)) schema values
let rowid_alias_value rowid = function | Sql.Ast.Null -> Sql.Ast.Int rowid | v -> v
let replace_rowid_alias rowid idx values = set_nth idx (rowid_alias_value rowid) values
(* A record the table's arity outruns is padded; one that outruns the table has values no column reaches and those are dropped, as a read stops at the last declared column. *)let fit_record (schema : schema) values = let n = List.length schema.columns in if List.compare_length_with values n > 0 then List.filteri (fun i _ -> i < n) values else pad_record schema values
let on_record (schema : schema) r ~rowid stored = Table.start r (Some rowid); let row = Table.slots r in Array.blit (record_defaults schema) 0 row 0 (Array.length row); let rec place stored i = function | [] -> () | (c : column) :: rest when is_virtual c -> place stored (i + 1) rest | _ :: rest -> ( match stored with | [] -> () | x :: more -> row.(i) <- x; place more (i + 1) rest) in place stored 0 schema.columns; (match rowid_alias schema with | Some idx when idx < Array.length row -> row.(idx) <- rowid_alias_value rowid row.(idx) | _ -> ()); realify_row schema row
let on_key (schema : schema) r layout stored = Table.start r None; let row = Table.slots r in Array.blit (record_defaults schema) 0 row 0 (Array.length row); let slots = Array.length layout in List.iteri (fun i v -> if i < slots then row.(layout.(i)) <- v) stored; realify_row schema row
let of_key (schema : schema) layout stored = let r = Table.row (table schema) in on_key schema r layout stored; Table.values r
let of_record ~(schema : schema) ~rowid stored = if not (has_virtual schema) then let row = fit_record schema stored in realify_values schema (match rowid_alias schema with | None -> row | Some idx -> replace_rowid_alias rowid idx row) else let r = Table.row (table schema) in on_record schema r ~rowid stored; Table.values r