summaryrefslogtreecommitdiffhomepage
path: root/gleam/src/ocda/load_db.gleam
diff options
context:
space:
mode:
authorRalph Amissah <ralph.amissah@gmail.com>2026-09-29 14:07:32 -0400
committerRalph Amissah <ralph.amissah@gmail.com>2026-09-29 14:07:32 -0400
commit2303447d77e39bbbbf3b1153fd104ecaf34a259c (patch)
tree8f2dd7a6fcdb0cd5c3e341cfcbda73e0a8a4a1df /gleam/src/ocda/load_db.gleam
parentimages from the source, and the epub segment fields (diff)
model 2.1: ocn_range, the verses a poem holds
The format now states, for a container, the first and last ocn of what it holds: .ocn_range in the .ssp, ocn_range_first and ocn_range_last in the .ocda.db. A poem is the one container, and since 2.1 it carries ocn 0 and comes before its verses. Both fields in the model, read by both loaders, in both languages, and written by the dump. A 2.0 database has neither column, so each loader asks sqlite whether it is there and reads a 2.0 file as having no ranges, which is what 2.0 meant; checked on a published 2.0 database, D and Gleam giving the same dump. Over the 36 document-languages written by spine at 2.1, the four dumps (D and Gleam, .ssp and .ocda.db) are byte identical, 36 of 36. (assisted by Claude-Code) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'gleam/src/ocda/load_db.gleam')
-rw-r--r--gleam/src/ocda/load_db.gleam37
1 files changed, 34 insertions, 3 deletions
diff --git a/gleam/src/ocda/load_db.gleam b/gleam/src/ocda/load_db.gleam
index a5f044b..641923e 100644
--- a/gleam/src/ocda/load_db.gleam
+++ b/gleam/src/ocda/load_db.gleam
@@ -245,6 +245,8 @@ fn objects(
use hla <- decode.field(37, decode.string)
use seg_epub <- decode.field(38, decode.string)
use seg_epub_is <- decode.field(39, decode.string)
+ use range_first <- decode.field(40, decode.int)
+ use range_last <- decode.field(41, decode.int)
decode.success(#(
id,
Object(
@@ -265,6 +267,8 @@ fn objects(
anchor_tags: [],
parent_ocn: parent,
last_descendant_ocn: last_descendant,
+ ocn_range_first: range_first,
+ ocn_range_last: range_last,
children: json_ints(children),
ancestors: json_ints(ancestors),
dummy_heading: dummy != 0,
@@ -297,7 +301,12 @@ fn objects(
))
}
let rows =
- sqlight.query(object_sql(doc_id), on: conn, with: [], expecting: d)
+ sqlight.query(
+ object_sql(doc_id, has_range_columns(conn)),
+ on: conn,
+ with: [],
+ expecting: d,
+ )
|> result.unwrap([])
// Built newest-first per section and reversed once at the end. Appending
@@ -331,7 +340,29 @@ fn objects(
model.Document(..document, sections: sections)
}
-fn object_sql(doc_id: Int) -> String {
+/// 2.1 added the container range as two columns. A 2.0 database has
+/// neither, and reads as having no range, which is what 2.0 meant.
+fn has_range_columns(conn: sqlight.Connection) -> Bool {
+ let d = decode.field(0, decode.int, decode.success)
+ case
+ sqlight.query(
+ "select count(*) from pragma_table_info('objects')
+ where name = 'ocn_range_first'",
+ on: conn,
+ with: [],
+ expecting: d,
+ )
+ {
+ Ok([n]) -> n > 0
+ _ -> False
+ }
+}
+
+fn object_sql(doc_id: Int, has_range: Bool) -> String {
+ let range_cols = case has_range {
+ True -> "coalesce(ocn_range_first,0), coalesce(ocn_range_last,0)"
+ False -> "0, 0"
+ }
"select section, seq, ocn, is_a, coalesce(is_of_part,''),
coalesce(is_of_section,''), coalesce(is_of_type,''),
coalesce(heading_level,-1), coalesce(identifier,''),
@@ -350,7 +381,7 @@ fn object_sql(doc_id: Int) -> String {
coalesce(code_linenumbers,0),
coalesce(heading_ancestors_text,''), coalesce(sha256,''),
coalesce(text,''), id, coalesce(heading_lev_anchor,''),
- coalesce(segment_epub,''), coalesce(segment_epub_is,'')
+ coalesce(segment_epub,''), coalesce(segment_epub_is,''), " <> range_cols <> "
from objects where doc_id = " <> int.to_string(doc_id) <> "
order by case section
when 'head' then 1 when 'toc' then 2 when 'body' then 3