summaryrefslogtreecommitdiffhomepage
path: root/dlang/src/ocda/load_db.d
diff options
context:
space:
mode:
Diffstat (limited to 'dlang/src/ocda/load_db.d')
-rw-r--r--dlang/src/ocda/load_db.d115
1 files changed, 76 insertions, 39 deletions
diff --git a/dlang/src/ocda/load_db.d b/dlang/src/ocda/load_db.d
index ffc28a8..f602da9 100644
--- a/dlang/src/ocda/load_db.d
+++ b/dlang/src/ocda/load_db.d
@@ -12,8 +12,10 @@ module ocda.load_db;
column correspondence this module implements.
+/
-import std.array : split;
+import std.array : join, split;
+import std.json : JSONType, parseJSON;
import std.conv : to;
+import std.algorithm : startsWith;
import std.string : strip;
import ocda.model;
import ocda.sqlite;
@@ -103,8 +105,31 @@ OcdaDocument loadDbFile(string path, string lang = "", bool any_version = false)
+/
foreach (r; db.rows("SELECT key, value FROM metadata WHERE doc_id = "
~ chosen.doc_id.to!string ~ " ORDER BY key")) {
- if (r[0].length > 7 && r[0][0 .. 7] == "source.") { continue; }
- doc.metadata[r[0]] = r[1];
+ string key = r[0];
+ if (key.startsWith("source.")) { continue; }
+ /+ ↓ translation.ocn_aligned says whether the languages of this
+ document share one numbering. A .ocda.db can know it because it
+ holds every language; a .ssp is one language and cannot. So it is
+ a fact about the *file* rather than about the document, and putting
+ it in the document's metadata would let a writer tell which
+ serialisation it was handed, which is the one thing this library
+ exists to prevent. Left out until the model has a place for
+ collection-wide facts.
+ +/
+ if (key.startsWith("translation.")) { continue; }
+ /+ ↓ the database has one metadata table where the text form has three
+ blocks, and tells them apart by a key prefix. Unprefixing them
+ here is what makes the two loaders give one model: a writer asks
+ doc.make["footer"] whichever serialisation it was handed, and
+ never doc.metadata["make.footer"].
+ +/
+ if (key.startsWith("make.")) {
+ doc.make[key["make.".length .. $]] = r[1].strip;
+ } else if (key.startsWith("doc_has.")) {
+ doc.doc_has[key["doc_has.".length .. $]] = r[1].strip;
+ } else {
+ doc.metadata[key] = r[1].strip;
+ }
}
/+ ↓ the objects, in document order within each section +/
@@ -215,51 +240,63 @@ private string levelName(int index) {
}
}
-/+ ↓ the fixed width arrays are JSON in the database and space separated in
- the text form (specification section 3.3) +/
+/+ ↓ the JSON columns, read as JSON.
+ .
+ The fixed width arrays are JSON in the database and space separated
+ (or "|" separated) in the text form. This was first written as a
+ character scan that threw away brackets and quotes, which is wrong the
+ moment a value holds an escape: Phobos' JSONValue.toString escapes
+ "/" as "\/", so every heading containing a slash came back with a
+ backslash in it. 36 of 36 documents differed on
+ heading_ancestors_text and nothing else had shown it.
+ .
+ So: parse. The values are small, there are few of them, and a real
+ parser is shorter than the scan it replaces.
++/
+private string[] jsonStrings(string v) {
+ if (v.length == 0) { return []; }
+ string[] out_;
+ try {
+ auto j = parseJSON(v);
+ if (j.type != JSONType.array) { return []; }
+ foreach (e; j.array) {
+ switch (e.type) {
+ case JSONType.string: out_ ~= e.str; break;
+ case JSONType.integer: out_ ~= e.integer.to!string; break;
+ case JSONType.uinteger: out_ ~= e.uinteger.to!string; break;
+ case JSONType.float_: out_ ~= e.floating.to!string; break;
+ case JSONType.true_: out_ ~= "true"; break;
+ case JSONType.false_: out_ ~= "false"; break;
+ default: out_ ~= ""; break;
+ }
+ }
+ } catch (Exception) {
+ return [];
+ }
+ return out_;
+}
+
private int[] jsonInts(string v) {
int[] out_;
- foreach (w; jsonWords(v).split(" ")) {
- if (w.length == 0) { continue; }
- try { out_ ~= w.to!int; } catch (Exception) {}
+ foreach (s; jsonStrings(v)) {
+ if (s.length == 0) { continue; }
+ try { out_ ~= s.to!int; } catch (Exception) {}
}
return out_;
}
+/+ ↓ space separated in the text form: table_widths, table_aligns +/
private string jsonWords(string v) {
- string out_;
- bool in_string = false;
- foreach (c; v) {
- switch (c) {
- case '[': case ']': case ' ': break;
- case '"': in_string = !in_string; break;
- case ',': out_ ~= " "; break;
- default: out_ ~= c; break;
- }
- }
- return out_.strip;
+ return jsonStrings(v).join(" ");
}
/+ ↓ heading_ancestors_text is eight slots, joined with "|" in the text
- form and a JSON array here +/
+ form and a JSON array here. The empty slots are significant: they say
+ which levels the heading sits under, so the separators carry as much
+ as the text does.
++/
private string jsonJoined(string v) {
- if (v.length == 0) { return ""; }
- string out_;
- bool in_string = false;
- bool first = true;
- string cur;
- foreach (c; v) {
- if (c == '"') {
- in_string = !in_string;
- if (!in_string) {
- if (!first) { out_ ~= "|"; }
- out_ ~= cur;
- cur = "";
- first = false;
- }
- continue;
- }
- if (in_string) { cur ~= c; }
- }
- return out_;
+ auto parts = jsonStrings(v);
+ if (parts.length == 0) { return ""; }
+ return parts.join("|");
}