@yaks/sqlite 0.0.0-stage → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (289) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1596 -2
  3. package/_dist/archetype.d.ts +75 -0
  4. package/_dist/archetype.d.ts.map +1 -0
  5. package/_dist/branches.d.ts +23 -0
  6. package/_dist/branches.d.ts.map +1 -0
  7. package/_dist/budget.d.ts +52 -0
  8. package/_dist/budget.d.ts.map +1 -0
  9. package/_dist/bundle.d.ts +2 -0
  10. package/_dist/bundle.d.ts.map +1 -0
  11. package/_dist/cache.d.ts +24 -0
  12. package/_dist/cache.d.ts.map +1 -0
  13. package/_dist/catalog.d.ts +9 -0
  14. package/_dist/catalog.d.ts.map +1 -0
  15. package/_dist/check.d.ts +4 -0
  16. package/_dist/check.d.ts.map +1 -0
  17. package/_dist/db.d.ts +36 -0
  18. package/_dist/db.d.ts.map +1 -0
  19. package/_dist/ddl.d.ts +143 -0
  20. package/_dist/ddl.d.ts.map +1 -0
  21. package/_dist/doom.d.ts +22 -0
  22. package/_dist/doom.d.ts.map +1 -0
  23. package/_dist/follow.d.ts +74 -0
  24. package/_dist/follow.d.ts.map +1 -0
  25. package/_dist/history-shapes.d.ts +3 -0
  26. package/_dist/history-shapes.d.ts.map +1 -0
  27. package/_dist/history.d.ts +18 -0
  28. package/_dist/history.d.ts.map +1 -0
  29. package/_dist/inspect.d.ts +43 -0
  30. package/_dist/inspect.d.ts.map +1 -0
  31. package/_dist/jsonb.d.ts +17 -0
  32. package/_dist/jsonb.d.ts.map +1 -0
  33. package/_dist/layout.d.ts +7 -0
  34. package/_dist/layout.d.ts.map +1 -0
  35. package/_dist/ledger.d.ts +41 -0
  36. package/_dist/ledger.d.ts.map +1 -0
  37. package/_dist/meta.d.ts +89 -0
  38. package/_dist/meta.d.ts.map +1 -0
  39. package/_dist/migration.d.ts +41 -0
  40. package/_dist/migration.d.ts.map +1 -0
  41. package/_dist/mod.d.ts +201 -0
  42. package/_dist/mod.d.ts.map +1 -0
  43. package/_dist/mode.d.ts +14 -0
  44. package/_dist/mode.d.ts.map +1 -0
  45. package/_dist/overlay.d.ts +30 -0
  46. package/_dist/overlay.d.ts.map +1 -0
  47. package/_dist/owe.d.ts +180 -0
  48. package/_dist/owe.d.ts.map +1 -0
  49. package/_dist/physical.d.ts +89 -0
  50. package/_dist/physical.d.ts.map +1 -0
  51. package/_dist/point.d.ts +10 -0
  52. package/_dist/point.d.ts.map +1 -0
  53. package/_dist/raise.d.ts +22 -0
  54. package/_dist/raise.d.ts.map +1 -0
  55. package/_dist/read-db.d.ts +9 -0
  56. package/_dist/read-db.d.ts.map +1 -0
  57. package/_dist/read-store.d.ts +10 -0
  58. package/_dist/read-store.d.ts.map +1 -0
  59. package/_dist/read.d.ts +59 -0
  60. package/_dist/read.d.ts.map +1 -0
  61. package/_dist/reader.d.ts +11 -0
  62. package/_dist/reader.d.ts.map +1 -0
  63. package/_dist/rules.d.ts +19 -0
  64. package/_dist/rules.d.ts.map +1 -0
  65. package/_dist/service.d.ts +12 -0
  66. package/_dist/service.d.ts.map +1 -0
  67. package/_dist/shape.d.ts +12 -0
  68. package/_dist/shape.d.ts.map +1 -0
  69. package/_dist/statements.d.ts +4 -0
  70. package/_dist/statements.d.ts.map +1 -0
  71. package/_dist/survey.d.ts +31 -0
  72. package/_dist/survey.d.ts.map +1 -0
  73. package/_dist/tools.d.ts +15 -0
  74. package/_dist/tools.d.ts.map +1 -0
  75. package/_dist/trash.d.ts +18 -0
  76. package/_dist/trash.d.ts.map +1 -0
  77. package/_dist/unit.d.ts +11 -0
  78. package/_dist/unit.d.ts.map +1 -0
  79. package/_dist/vocab.d.ts +6 -0
  80. package/_dist/vocab.d.ts.map +1 -0
  81. package/_dist/write.d.ts +169 -0
  82. package/_dist/write.d.ts.map +1 -0
  83. package/answer.js +110 -0
  84. package/answer.js.map +1 -0
  85. package/answer.ts +122 -0
  86. package/archetype.js +730 -0
  87. package/archetype.js.map +1 -0
  88. package/archetype.ts +790 -0
  89. package/binary_cases.ts +240 -0
  90. package/branches.js +195 -0
  91. package/branches.js.map +1 -0
  92. package/branches.ts +257 -0
  93. package/budget.js +89 -0
  94. package/budget.js.map +1 -0
  95. package/budget.ts +117 -0
  96. package/builtin.js +34 -0
  97. package/builtin.js.map +1 -0
  98. package/builtin.ts +31 -0
  99. package/bundle.js +21 -0
  100. package/bundle.js.map +1 -0
  101. package/bundle.ts +22 -0
  102. package/buried.js +95 -0
  103. package/buried.js.map +1 -0
  104. package/buried.ts +124 -0
  105. package/cache.js +153 -0
  106. package/cache.js.map +1 -0
  107. package/cache.ts +154 -0
  108. package/cache_worker.ts +16 -0
  109. package/catalog.js +173 -0
  110. package/catalog.js.map +1 -0
  111. package/catalog.ts +224 -0
  112. package/check.js +80 -0
  113. package/check.js.map +1 -0
  114. package/check.ts +92 -0
  115. package/checkpoint.js +98 -0
  116. package/checkpoint.js.map +1 -0
  117. package/checkpoint.ts +129 -0
  118. package/checkpoint_worker.ts +37 -0
  119. package/db.js +170 -0
  120. package/db.js.map +1 -0
  121. package/db.ts +173 -0
  122. package/ddl.js +1441 -0
  123. package/ddl.js.map +1 -0
  124. package/ddl.ts +1508 -0
  125. package/deno.json +1 -0
  126. package/doom.js +59 -0
  127. package/doom.js.map +1 -0
  128. package/doom.ts +75 -0
  129. package/driver.js +69 -0
  130. package/driver.js.map +1 -0
  131. package/driver.ts +101 -0
  132. package/error.js +30 -0
  133. package/error.js.map +1 -0
  134. package/error.ts +46 -0
  135. package/ffi/LICENSE +202 -0
  136. package/ffi/blob.js +99 -0
  137. package/ffi/blob.js.map +1 -0
  138. package/ffi/blob.ts +156 -0
  139. package/ffi/constants.js +65 -0
  140. package/ffi/constants.js.map +1 -0
  141. package/ffi/constants.ts +67 -0
  142. package/ffi/database.js +613 -0
  143. package/ffi/database.js.map +1 -0
  144. package/ffi/database.ts +931 -0
  145. package/ffi/ffi.js +535 -0
  146. package/ffi/ffi.js.map +1 -0
  147. package/ffi/ffi.ts +604 -0
  148. package/ffi/mod.js +15 -0
  149. package/ffi/mod.js.map +1 -0
  150. package/ffi/mod.ts +28 -0
  151. package/ffi/statement.js +555 -0
  152. package/ffi/statement.js.map +1 -0
  153. package/ffi/statement.ts +788 -0
  154. package/ffi/util.js +32 -0
  155. package/ffi/util.js.map +1 -0
  156. package/ffi/util.ts +48 -0
  157. package/follow.js +342 -0
  158. package/follow.js.map +1 -0
  159. package/follow.ts +401 -0
  160. package/history-branch.js +166 -0
  161. package/history-branch.js.map +1 -0
  162. package/history-branch.ts +140 -0
  163. package/history-shapes.js +254 -0
  164. package/history-shapes.js.map +1 -0
  165. package/history-shapes.ts +268 -0
  166. package/history.js +620 -0
  167. package/history.js.map +1 -0
  168. package/history.ts +674 -0
  169. package/inspect.js +120 -0
  170. package/inspect.js.map +1 -0
  171. package/inspect.ts +164 -0
  172. package/jsonb.js +73 -0
  173. package/jsonb.js.map +1 -0
  174. package/jsonb.ts +108 -0
  175. package/layout.js +22 -0
  176. package/layout.js.map +1 -0
  177. package/layout.ts +26 -0
  178. package/ledger.js +210 -0
  179. package/ledger.js.map +1 -0
  180. package/ledger.ts +210 -0
  181. package/legacy.js +4 -0
  182. package/legacy.js.map +1 -0
  183. package/legacy.ts +3 -0
  184. package/maintenance.js +148 -0
  185. package/maintenance.js.map +1 -0
  186. package/maintenance.ts +154 -0
  187. package/measure.js +133 -0
  188. package/measure.js.map +1 -0
  189. package/measure.ts +167 -0
  190. package/measure_worker.ts +18 -0
  191. package/memo.js +1250 -0
  192. package/memo.js.map +1 -0
  193. package/memo.ts +1480 -0
  194. package/meta.js +265 -0
  195. package/meta.js.map +1 -0
  196. package/meta.ts +321 -0
  197. package/migration.js +272 -0
  198. package/migration.js.map +1 -0
  199. package/migration.ts +286 -0
  200. package/mod.js +1103 -0
  201. package/mod.js.map +1 -0
  202. package/mod.ts +1762 -0
  203. package/mode.js +192 -0
  204. package/mode.js.map +1 -0
  205. package/mode.ts +207 -0
  206. package/native.js +544 -0
  207. package/native.js.map +1 -0
  208. package/native.ts +544 -0
  209. package/overlay.js +408 -0
  210. package/overlay.js.map +1 -0
  211. package/overlay.ts +485 -0
  212. package/owe.js +719 -0
  213. package/owe.js.map +1 -0
  214. package/owe.ts +873 -0
  215. package/package.json +99 -4
  216. package/physical.js +350 -0
  217. package/physical.js.map +1 -0
  218. package/physical.ts +434 -0
  219. package/plan.js +78 -0
  220. package/plan.js.map +1 -0
  221. package/plan.ts +104 -0
  222. package/point.js +81 -0
  223. package/point.js.map +1 -0
  224. package/point.ts +52 -0
  225. package/population.js +639 -0
  226. package/population.js.map +1 -0
  227. package/population.ts +630 -0
  228. package/purge.js +117 -0
  229. package/purge.js.map +1 -0
  230. package/purge.ts +127 -0
  231. package/raise.js +270 -0
  232. package/raise.js.map +1 -0
  233. package/raise.ts +316 -0
  234. package/read-db.js +381 -0
  235. package/read-db.js.map +1 -0
  236. package/read-db.ts +282 -0
  237. package/read-store.js +74 -0
  238. package/read-store.js.map +1 -0
  239. package/read-store.ts +86 -0
  240. package/read.js +1100 -0
  241. package/read.js.map +1 -0
  242. package/read.ts +1441 -0
  243. package/reader.js +141 -0
  244. package/reader.js.map +1 -0
  245. package/reader.ts +159 -0
  246. package/rules.js +88 -0
  247. package/rules.js.map +1 -0
  248. package/rules.ts +113 -0
  249. package/service.js +27 -0
  250. package/service.js.map +1 -0
  251. package/service.ts +41 -0
  252. package/shape.js +76 -0
  253. package/shape.js.map +1 -0
  254. package/shape.ts +108 -0
  255. package/sparse_presence_fixture.ts +45 -0
  256. package/sqlitepath.js +24 -0
  257. package/sqlitepath.js.map +1 -0
  258. package/sqlitepath.ts +29 -0
  259. package/statements.js +78 -0
  260. package/statements.js.map +1 -0
  261. package/statements.ts +96 -0
  262. package/survey.js +135 -0
  263. package/survey.js.map +1 -0
  264. package/survey.ts +196 -0
  265. package/tools.js +17 -0
  266. package/tools.js.map +1 -0
  267. package/tools.ts +50 -0
  268. package/trash.js +144 -0
  269. package/trash.js.map +1 -0
  270. package/trash.ts +187 -0
  271. package/unit.js +215 -0
  272. package/unit.js.map +1 -0
  273. package/unit.ts +211 -0
  274. package/versions.js +115 -0
  275. package/versions.js.map +1 -0
  276. package/versions.ts +110 -0
  277. package/vocab.js +19 -0
  278. package/vocab.js.map +1 -0
  279. package/vocab.json +27 -0
  280. package/vocab.ts +19 -0
  281. package/watch.js +33 -0
  282. package/watch.js.map +1 -0
  283. package/watch.ts +42 -0
  284. package/write.js +1164 -0
  285. package/write.js.map +1 -0
  286. package/write.ts +1356 -0
  287. package/written.js +200 -0
  288. package/written.js.map +1 -0
  289. package/written.ts +202 -0
package/mode.js ADDED
@@ -0,0 +1,192 @@
1
+ // Reading the past, and writing to a branch (D-130964), through a driver of
2
+ // the mode's own. Each statement it runs is given, in front of each component
3
+ // table it names, a common table expression of that name: the rows its mode
4
+ // reads of that component (./history.ts `seen`). A name a statement's `with`
5
+ // gives is the name everywhere in that statement, its subqueries, joins and
6
+ // the fragments compiled as text included, so every statement this package
7
+ // builds, the compiler's and every other, reads the mode without knowing it.
8
+ //
9
+ // Nothing stands in the database for it: no view, no table, no temporary
10
+ // object, which a Durable Object refuses. So a mode reads over any connection,
11
+ // the one its main store writes through included, and a statement holds the
12
+ // mode it was written for whatever else runs beside it. A table named with
13
+ // its schema (`main.doc`) is the table itself.
14
+ //
15
+ // Binding the driver to another mode answers the same store, and its memory
16
+ // (./memo.ts) lets go of everything it held, as it does when the schema moves.
17
+ //
18
+ // A mode reads its table sets from history's archetype index, populated by
19
+ // the same writes that keep its component versions (./history-shapes.ts).
20
+ // Index hints name live indexes a common table expression does not have.
21
+ //
22
+ // A driver as of a transaction refuses every write. A branch's writes go to
23
+ // the branch's rows beside the live tables (./history.ts `branched`); the
24
+ // live tables are never a mode's to write.
25
+ import { lit, render, revision, select, star } from "@yaks/sql";
26
+ import { labelStored } from "@yaks/vocab";
27
+ import { pastShapes } from "./history-shapes.js";
28
+ import { docDdl, indexed } from "./ddl.js";
29
+ import { covered, keep, seen } from "./history.js";
30
+ import { turned } from "./memo.js";
31
+ import { bindStorage } from "./shape.js";
32
+ import { storage } from "./mod.js";
33
+ // What a mode reads each relation it stands in front of as, by name, in the
34
+ // order a statement's `with` gives them: each component's table, then
35
+ // `doc_value`, which reads `doc`. A view's body sees no `with` of the
36
+ // statement naming it, so the view is given as its own. The archetypes are
37
+ // the store's own, and a mode reads none of them: their table's columns, and
38
+ // no row.
39
+ let relations = (vocab, derived, mode)=>[
40
+ ...[
41
+ ...covered(vocab)
42
+ ].map((comp)=>({
43
+ name: comp,
44
+ materialized: false,
45
+ q: comp == 'archetype' ? select({
46
+ cols: [
47
+ star()
48
+ ],
49
+ from: {
50
+ t: 'table',
51
+ name: comp,
52
+ schema: 'main'
53
+ },
54
+ where: lit(false)
55
+ }) : seen(vocab, comp, mode)
56
+ })),
57
+ ...docDdl(vocab, derived).flatMap((s)=>s.t == 'create view' ? [
58
+ {
59
+ name: s.name,
60
+ materialized: false,
61
+ q: s.q
62
+ }
63
+ ] : [])
64
+ ];
65
+ // The names a statement's text holds, quoted or bare. More than the tables it
66
+ // reads (a column, a word in a literal) costs only a relation it never reads.
67
+ let NAME = /"((?:[^"]|"")+)"|([A-Za-z_][A-Za-z0-9_$]*)/g;
68
+ let named = (sql)=>new Set([
69
+ ...sql.matchAll(NAME)
70
+ ].map((m)=>m[1]?.replaceAll('""', '"') ?? m[2]));
71
+ let dressable = (s)=>s.t == 'select' || s.t == 'compound' || s.t == 'insert' || s.t == 'update' || s.t == 'delete';
72
+ let written = (s)=>s.t == 'insert' ? s.into : s.t == 'update' ? s.table : s.t == 'delete' ? s.from : undefined;
73
+ /**
74
+ * `s` with the relations it names from `all` in front of the tables of those
75
+ * names, and those they read (`doc_value` reads `doc`). A statement that names
76
+ * none is itself. A statement that writes a live table a mode stands in front
77
+ * of is a bug in its writer: a mode's writes are the branch's rows.
78
+ */ let dressed = (s, all)=>{
79
+ let into = written(s);
80
+ if (into && all.some((r)=>r.name == into)) {
81
+ throw new Error(`a mode cannot write the live table ${into}`);
82
+ }
83
+ if (!dressable(s) && s.t != 'raw') return s;
84
+ let names = named(render(s).sql);
85
+ if (all.some((r)=>r.name.endsWith('_value') && names.has(r.name))) {
86
+ names.add('doc');
87
+ }
88
+ let own = new Set((dressable(s) ? s.with ?? [] : []).map((c)=>c.name));
89
+ let ctes = all.filter((r)=>names.has(r.name) && !own.has(r.name));
90
+ if (!ctes.length) return s;
91
+ if (!dressable(s)) {
92
+ return select({
93
+ with: ctes,
94
+ cols: [
95
+ star()
96
+ ],
97
+ from: {
98
+ t: 'from',
99
+ q: s
100
+ }
101
+ });
102
+ }
103
+ return {
104
+ ...s,
105
+ with: [
106
+ ...ctes,
107
+ ...s.with ?? []
108
+ ]
109
+ };
110
+ };
111
+ // The driver a mode reads through, over `driver`: every statement it runs is
112
+ // dressed for the mode `on` holds. Its memory is its own (no `connection`),
113
+ // since what it reads is not what main's driver reads. What it does to the
114
+ // file is the file's, though: each statement still passes `driver`'s, so a
115
+ // table it raises moves the schema main's driver reads (@yaks/sql
116
+ // `revision`), and main never records its tables from a stale look.
117
+ let through = (driver, on)=>{
118
+ revision(driver, 'schema');
119
+ let { connection: _, ...rest } = driver;
120
+ let query = driver.query.bind(driver);
121
+ let run = driver.run?.bind(driver);
122
+ return {
123
+ ...rest,
124
+ views: true,
125
+ query: (s)=>query(dressed(s, on().all)),
126
+ ...run ? {
127
+ run: (s)=>run(dressed(s, on().all))
128
+ } : {},
129
+ ...driver.tx ? {
130
+ tx: driver.tx.bind(driver)
131
+ } : {}
132
+ };
133
+ };
134
+ // Each driver's mode driver, bound once.
135
+ let modes = new WeakMap();
136
+ /** Explicit history erasure changes even a fixed past view. */ export let erased = (driver)=>{
137
+ turned(driver);
138
+ let past = modes.get(driver);
139
+ if (past) turned(past.driver);
140
+ };
141
+ /**
142
+ * The store over `driver` reading `mode`: main as of a transaction, or a
143
+ * branch, over a database a store with history keeps. `driver` may be the one
144
+ * its main store writes through. The first binding makes its store; binding
145
+ * the same driver again answers the same store, now reading the new mode. The
146
+ * database's schema is its main store's to keep, never a mode's.
147
+ */ export let moded = (driver, vocab, base, mode)=>{
148
+ vocab = bindStorage(driver, vocab);
149
+ let derived = base.derived ?? {};
150
+ let all = relations(vocab, derived, mode);
151
+ let made = modes.get(driver);
152
+ if (made) {
153
+ let { on } = made;
154
+ if (on.vocab != vocab) {
155
+ throw new Error('a mode driver is bound to one vocabulary');
156
+ }
157
+ on.all = all;
158
+ on.opts.mode = mode;
159
+ keep(made.driver, vocab, base.history, mode);
160
+ turned(made.driver);
161
+ return on.store;
162
+ }
163
+ let on = {
164
+ vocab,
165
+ all
166
+ };
167
+ let mine = through(driver, ()=>on);
168
+ modes.set(driver, {
169
+ driver: mine,
170
+ on
171
+ });
172
+ on.opts = {
173
+ ...base,
174
+ mode,
175
+ // The schema is its main store's: a mode's store installs nothing, even
176
+ // over a driver with no file, which would install on its first use.
177
+ schemaReady: ()=>true,
178
+ archetypes: ()=>undefined,
179
+ // Branch queries compile against label-named relations (./shape.ts).
180
+ // The physical names also cover statements over the file's own layout.
181
+ missingIndexes: [
182
+ ...new Set([
183
+ ...indexed(vocab),
184
+ ...indexed(labelStored(vocab))
185
+ ].map((i)=>i.name))
186
+ ],
187
+ held: (ids, names)=>pastShapes(mine, vocab, ids, names, on.opts.mode)
188
+ };
189
+ on.store = storage(mine, vocab, on.opts);
190
+ return on.store;
191
+ };
192
+ //# sourceMappingURL=mode.js.map
package/mode.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mode.js","sources":["./mode.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,8EAA8E;AAC9E,4EAA4E;AAC5E,6EAA6E;AAC7E,4EAA4E;AAC5E,2EAA2E;AAC3E,6EAA6E;AAC7E,EAAE;AACF,yEAAyE;AACzE,+EAA+E;AAC/E,4EAA4E;AAC5E,2EAA2E;AAC3E,+CAA+C;AAC/C,EAAE;AACF,4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MAAM;IAClB,OAAO,GAAG,KAAK;EACjB;EACA,IAAI,KAAK;IAAE;IAAO;EAAI;EACtB,IAAI,OAAO,QAAQ,QAAQ,IAAM;EACjC,MAAM,GAAG,CAAC,QAAQ;IAAE,QAAQ;IAAM;EAAG;EACrC,GAAG,IAAI,GAAG;IACR,GAAG,IAAI;IACP;IACA,wEAAwE;IACxE,oEAAoE;IACpE,aAAa,IAAM;IACnB,YAAY,IAAM;IAClB,qEAAqE;IACrE,uEAAuE;IACvE,gBAAgB;SACX,IAAI,IACL;WAAI,QAAQ;WAAW,QAAQ,YAAY;OAAQ,CAAC,GAAG,CAAC,CAAC,IAAM,EAAE,IAAI;KAExE;IACD,MAAM,CAAC,KAAe,QACpB,WAAW,MAAM,OAAO,KAAK,OAAO,GAAG,IAAI,CAAC,IAAI;EACpD;EACA,GAAG,KAAK,GAAG,QAAQ,MAAM,OAAO,GAAG,IAAI;EACvC,OAAO,GAAG,KAAK;AACjB,EAAC"}
package/mode.ts ADDED
@@ -0,0 +1,207 @@
1
+ // Reading the past, and writing to a branch (D-130964), through a driver of
2
+ // the mode's own. Each statement it runs is given, in front of each component
3
+ // table it names, a common table expression of that name: the rows its mode
4
+ // reads of that component (./history.ts `seen`). A name a statement's `with`
5
+ // gives is the name everywhere in that statement, its subqueries, joins and
6
+ // the fragments compiled as text included, so every statement this package
7
+ // builds, the compiler's and every other, reads the mode without knowing it.
8
+ //
9
+ // Nothing stands in the database for it: no view, no table, no temporary
10
+ // object, which a Durable Object refuses. So a mode reads over any connection,
11
+ // the one its main store writes through included, and a statement holds the
12
+ // mode it was written for whatever else runs beside it. A table named with
13
+ // its schema (`main.doc`) is the table itself.
14
+ //
15
+ // Binding the driver to another mode answers the same store, and its memory
16
+ // (./memo.ts) lets go of everything it held, as it does when the schema moves.
17
+ //
18
+ // A mode reads its table sets from history's archetype index, populated by
19
+ // the same writes that keep its component versions (./history-shapes.ts).
20
+ // Index hints name live indexes a common table expression does not have.
21
+ //
22
+ // A driver as of a transaction refuses every write. A branch's writes go to
23
+ // the branch's rows beside the live tables (./history.ts `branched`); the
24
+ // live tables are never a mode's to write.
25
+
26
+ import {
27
+ type Cte,
28
+ type Derived,
29
+ type Driver,
30
+ lit,
31
+ render,
32
+ revision,
33
+ select,
34
+ star,
35
+ type Stmt,
36
+ } from '@yaks/sql'
37
+ import { labelStored, type Vocab } from '@yaks/vocab'
38
+ import { pastShapes } from './history-shapes.js'
39
+ import { docDdl, indexed } from './ddl.js'
40
+ import { covered, type History, keep, type Mode, seen } from './history.js'
41
+ import { turned } from './memo.js'
42
+ import { bindStorage } from './shape.js'
43
+ import { type Opts, storage, type Store } from './mod.js'
44
+
45
+ // What a mode reads each relation it stands in front of as, by name, in the
46
+ // order a statement's `with` gives them: each component's table, then
47
+ // `doc_value`, which reads `doc`. A view's body sees no `with` of the
48
+ // statement naming it, so the view is given as its own. The archetypes are
49
+ // the store's own, and a mode reads none of them: their table's columns, and
50
+ // no row.
51
+ let relations = (vocab: Vocab, derived: Derived, mode: Mode): Cte[] => [
52
+ ...[...covered(vocab)].map((comp): Cte => ({
53
+ name: comp,
54
+ materialized: false,
55
+ q: comp == 'archetype'
56
+ ? select({
57
+ cols: [star()],
58
+ from: { t: 'table', name: comp, schema: 'main' },
59
+ where: lit(false),
60
+ })
61
+ : seen(vocab, comp, mode),
62
+ })),
63
+ ...docDdl(vocab, derived).flatMap((s): Cte[] =>
64
+ s.t == 'create view' ? [{ name: s.name, materialized: false, q: s.q }] : []
65
+ ),
66
+ ]
67
+
68
+ // The names a statement's text holds, quoted or bare. More than the tables it
69
+ // reads (a column, a word in a literal) costs only a relation it never reads.
70
+ let NAME = /"((?:[^"]|"")+)"|([A-Za-z_][A-Za-z0-9_$]*)/g
71
+ let named = (sql: string): Set<string> =>
72
+ new Set(
73
+ [...sql.matchAll(NAME)].map((m) => m[1]?.replaceAll('""', '"') ?? m[2]),
74
+ )
75
+
76
+ type Dressable = Extract<
77
+ Stmt,
78
+ { t: 'select' | 'compound' | 'insert' | 'update' | 'delete' }
79
+ >
80
+ let dressable = (s: Stmt): s is Dressable =>
81
+ s.t == 'select' || s.t == 'compound' || s.t == 'insert' ||
82
+ s.t == 'update' || s.t == 'delete'
83
+ let written = (s: Stmt): string | undefined =>
84
+ s.t == 'insert'
85
+ ? s.into
86
+ : s.t == 'update'
87
+ ? s.table
88
+ : s.t == 'delete'
89
+ ? s.from
90
+ : undefined
91
+
92
+ /**
93
+ * `s` with the relations it names from `all` in front of the tables of those
94
+ * names, and those they read (`doc_value` reads `doc`). A statement that names
95
+ * none is itself. A statement that writes a live table a mode stands in front
96
+ * of is a bug in its writer: a mode's writes are the branch's rows.
97
+ */
98
+ let dressed = (s: Stmt, all: Cte[]): Stmt => {
99
+ let into = written(s)
100
+ if (into && all.some((r) => r.name == into)) {
101
+ throw new Error(`a mode cannot write the live table ${into}`)
102
+ }
103
+ if (!dressable(s) && s.t != 'raw') return s
104
+ let names = named(render(s).sql)
105
+ if (all.some((r) => r.name.endsWith('_value') && names.has(r.name))) {
106
+ names.add('doc')
107
+ }
108
+ let own = new Set((dressable(s) ? s.with ?? [] : []).map((c) => c.name))
109
+ let ctes = all.filter((r) => names.has(r.name) && !own.has(r.name))
110
+ if (!ctes.length) return s
111
+ if (!dressable(s)) {
112
+ return select({ with: ctes, cols: [star()], from: { t: 'from', q: s } })
113
+ }
114
+ return { ...s, with: [...ctes, ...s.with ?? []] }
115
+ }
116
+
117
+ // A mode's driver: the mode it is bound to now, what it reads each relation
118
+ // as, its vocabulary, the options its store reads its mode from, and the
119
+ // store.
120
+ type Dialed = {
121
+ vocab: Vocab
122
+ all: Cte[]
123
+ opts: Opts & { mode: Mode }
124
+ store: Store
125
+ }
126
+
127
+ // The driver a mode reads through, over `driver`: every statement it runs is
128
+ // dressed for the mode `on` holds. Its memory is its own (no `connection`),
129
+ // since what it reads is not what main's driver reads. What it does to the
130
+ // file is the file's, though: each statement still passes `driver`'s, so a
131
+ // table it raises moves the schema main's driver reads (@yaks/sql
132
+ // `revision`), and main never records its tables from a stale look.
133
+ let through = (driver: Driver, on: () => Dialed): Driver => {
134
+ revision(driver, 'schema')
135
+ let { connection: _, ...rest } = driver
136
+ let query = driver.query.bind(driver)
137
+ let run = driver.run?.bind(driver)
138
+ return {
139
+ ...rest,
140
+ views: true,
141
+ query: (s) => query(dressed(s, on().all)),
142
+ ...run ? { run: (s: Stmt) => run(dressed(s, on().all)) } : {},
143
+ ...driver.tx ? { tx: driver.tx.bind(driver) } : {},
144
+ }
145
+ }
146
+
147
+ // Each driver's mode driver, bound once.
148
+ let modes = new WeakMap<Driver, { driver: Driver; on: Dialed }>()
149
+
150
+ /** Explicit history erasure changes even a fixed past view. */
151
+ export let erased = (driver: Driver): void => {
152
+ turned(driver)
153
+ let past = modes.get(driver)
154
+ if (past) turned(past.driver)
155
+ }
156
+
157
+ /**
158
+ * The store over `driver` reading `mode`: main as of a transaction, or a
159
+ * branch, over a database a store with history keeps. `driver` may be the one
160
+ * its main store writes through. The first binding makes its store; binding
161
+ * the same driver again answers the same store, now reading the new mode. The
162
+ * database's schema is its main store's to keep, never a mode's.
163
+ */
164
+ export let moded = (
165
+ driver: Driver,
166
+ vocab: Vocab,
167
+ base: Opts & { history: History },
168
+ mode: Mode,
169
+ ): Store => {
170
+ vocab = bindStorage(driver, vocab)
171
+ let derived = base.derived ?? {}
172
+ let all = relations(vocab, derived, mode)
173
+ let made = modes.get(driver)
174
+ if (made) {
175
+ let { on } = made
176
+ if (on.vocab != vocab) {
177
+ throw new Error('a mode driver is bound to one vocabulary')
178
+ }
179
+ on.all = all
180
+ on.opts.mode = mode
181
+ keep(made.driver, vocab, base.history, mode)
182
+ turned(made.driver)
183
+ return on.store
184
+ }
185
+ let on = { vocab, all } as Dialed
186
+ let mine = through(driver, () => on)
187
+ modes.set(driver, { driver: mine, on })
188
+ on.opts = {
189
+ ...base,
190
+ mode,
191
+ // The schema is its main store's: a mode's store installs nothing, even
192
+ // over a driver with no file, which would install on its first use.
193
+ schemaReady: () => true,
194
+ archetypes: () => undefined,
195
+ // Branch queries compile against label-named relations (./shape.ts).
196
+ // The physical names also cover statements over the file's own layout.
197
+ missingIndexes: [
198
+ ...new Set(
199
+ [...indexed(vocab), ...indexed(labelStored(vocab))].map((i) => i.name),
200
+ ),
201
+ ],
202
+ held: (ids: number[], names: readonly string[]) =>
203
+ pastShapes(mine, vocab, ids, names, on.opts.mode),
204
+ }
205
+ on.store = storage(mine, vocab, on.opts)
206
+ return on.store
207
+ }