quickdbd-cli 0.1.18__py3-none-any.whl → 0.2.0__py3-none-any.whl

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.
quickdbd_cli/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.1.18"
1
+ __version__ = "0.2.0"
@@ -24,8 +24,27 @@
24
24
  { regex: /^[^\s#-][^#]*$/, token: null, sol: true },
25
25
  ],
26
26
  });
27
+ CodeMirror.defineSimpleMode("dbml", {
28
+ start: [
29
+ { regex: /\/\/.*/, token: "comment" },
30
+ { regex: /\/\*/, token: "comment", next: "comment" },
31
+ { regex: /'''/, token: "string", next: "note" },
32
+ { regex: /'(?:[^'\\]|\\.)*'?/, token: "string" },
33
+ { regex: /"(?:[^"\\]|\\.)*"?/, token: "variable-2" },
34
+ { regex: /`[^`]*`?/, token: "string-2" },
35
+ { regex: /#[0-9a-fA-F]{3,6}\b/, token: "number" },
36
+ { regex: /\b(Table|TablePartial|TableGroup|Ref|Enum|Project|Note|indexes|checks|records|Dep|as)\b/i, token: "qdbd-kw" },
37
+ { regex: /\b(pk|primary key|not null|null|unique|increment|default|note|ref|headercolor|delete|update)\b/i, token: "keyword" },
38
+ { regex: /\?(<>|<|>|-)\??|(<>|<|>|-)\?|<>|[<>~]/, token: "qdbd-op" },
39
+ { regex: /-?\d+(\.\d+)?/, token: "number" },
40
+ ],
41
+ comment: [{ regex: /.*?\*\//, token: "comment", next: "start" }, { regex: /.*/, token: "comment" }],
42
+ note: [{ regex: /.*?'''/, token: "string", next: "start" }, { regex: /.*/, token: "string" }],
43
+ });
27
44
  }
28
45
 
46
+ function modeOf(f) { return f.syntax === "dbml" || /\.dbml$/i.test(f.path) ? "dbml" : "qdbd"; }
47
+
29
48
  function editorValue() { return cm ? cm.getValue() : textarea.value; }
30
49
 
31
50
  function makeEditor() {
@@ -43,7 +62,7 @@
43
62
 
44
63
  function setEditorText(f) {
45
64
  if (cm) {
46
- if (!f.doc) f.doc = CodeMirror.Doc(f.text, "qdbd");
65
+ if (!f.doc) f.doc = CodeMirror.Doc(f.text, modeOf(f));
47
66
  cm.swapDoc(f.doc);
48
67
  } else {
49
68
  textarea.value = f.buffer != null ? f.buffer : f.text;
@@ -205,7 +224,7 @@
205
224
  seen[sf.path] = true;
206
225
  var f = files[sf.path];
207
226
  if (!f) {
208
- files[sf.path] = { path: sf.path, text: sf.text, mtime: sf.mtime, dirty: false };
227
+ files[sf.path] = { path: sf.path, text: sf.text, mtime: sf.mtime, syntax: sf.syntax, dirty: false };
209
228
  order.push(sf.path);
210
229
  return;
211
230
  }
@@ -31,6 +31,18 @@
31
31
 
32
32
  function n(v) { return Math.round(v * 100) / 100; }
33
33
 
34
+ /* Relative luminance above 0.45: only pale colours. Every built-in header, diff and tag colour is below it. */
35
+ function isLight(hex) {
36
+ var m = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex || "");
37
+ if (!m) return false;
38
+ var h = m[1].length === 3 ? m[1].replace(/./g, "$&$&") : m[1];
39
+ var lum = [0, 2, 4].map(function (i) {
40
+ var c = parseInt(h.substr(i, 2), 16) / 255;
41
+ return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
42
+ });
43
+ return 0.2126 * lum[0] + 0.7152 * lum[1] + 0.0722 * lum[2] > 0.45;
44
+ }
45
+
34
46
  function relatedColumns(model) {
35
47
  var set = {};
36
48
  model.rels.forEach(function (r) {
@@ -84,10 +96,11 @@
84
96
  out.push('<text x="' + (w - st.padX) + '" y="' + n(st.headerH / 2 + 4) + '" text-anchor="end" fill="#fff" font-family="' + FONT +
85
97
  '" font-size="10" font-weight="bold">' + (state === "added" ? "NEW" : "CHANGED") + "</text>");
86
98
  }
87
- out.push('<text class="tn" x="' + st.padX + '" y="' + n(st.headerH / 2 + st.headerSize * 0.36) + '" fill="' + C.headerText +
99
+ var light = isLight(hcol); // a pale DBML headercolor or tag colour: dark text on it
100
+ out.push('<text class="tn" x="' + st.padX + '" y="' + n(st.headerH / 2 + st.headerSize * 0.36) + '" fill="' + (light ? C.text : C.headerText) +
88
101
  '" font-family="' + FONT + '" font-size="' + st.headerSize + '">' + esc(t.name) + "</text>");
89
102
  if (docMarks && t.doc) {
90
- out.push(docMark(st.padX, st.headerH / 2 + st.headerSize * 0.36 + 3, t.nameW, "rgba(255,255,255,.7)"));
103
+ out.push(docMark(st.padX, st.headerH / 2 + st.headerSize * 0.36 + 3, t.nameW, light ? "rgba(0,0,0,.45)" : "rgba(255,255,255,.7)"));
91
104
  }
92
105
  g.cols.forEach(function (c, i) {
93
106
  var y = st.headerH + i * st.rowH, cy = y + st.rowH / 2, base = n(cy + st.rowSize * 0.36);
@@ -310,7 +323,8 @@
310
323
  cells.push(cell({
311
324
  id: tid, value: t.name, vertex: "1", parent: "1",
312
325
  style: "shape=table;startSize=" + HEADH + ";container=1;collapsible=1;childLayout=tableLayout;fixedRows=1;rowLines=0;" +
313
- "fontStyle=1;align=left;spacingLeft=10;resizeLast=1;html=0;whiteSpace=wrap;fillColor=" + (t.color || C.header) + ";fontColor=#ffffff;" +
326
+ "fontStyle=1;align=left;spacingLeft=10;resizeLast=1;html=0;whiteSpace=wrap;fillColor=" + (t.color || C.header) +
327
+ ";fontColor=" + (isLight(t.color) ? C.text : "#ffffff") + ";" +
314
328
  "strokeColor=#dfe3e8;swimlaneFillColor=#ffffff;fontSize=14;",
315
329
  }, g(p[0] - b[0] + 40, p[1] - b[1] + 40, w, HEADH + Math.max(tg.cols.length, 0) * ROWH)));
316
330
  tg.cols.forEach(function (c, i) {
quickdbd_cli/cli.py CHANGED
@@ -60,7 +60,7 @@ def _symbols(schema: Schema) -> dict:
60
60
  for c in t.columns.values()
61
61
  if not c.virtual
62
62
  ]
63
- end = max([t.line + 1] + [c["line"] for c in cols])
63
+ end = t.end_line or max([t.line + 1] + [c["line"] for c in cols])
64
64
  table = {"name": t.name, "aliases": t.aliases, "file": t.path, "line": t.line, "endLine": end, "columns": cols}
65
65
  if t.comments:
66
66
  table["doc"] = "\n".join(t.comments)
@@ -294,6 +294,63 @@ def cmd_import_dbt(args) -> int:
294
294
  return 0
295
295
 
296
296
 
297
+ def cmd_convert(args) -> int:
298
+ from . import convert, layout
299
+ from .parser import compile_sources
300
+
301
+ inputs = [Path(p) for p in args.paths]
302
+ try:
303
+ project = Project.open(inputs)
304
+ except FileNotFoundError as e:
305
+ print(f"error: {e}", file=sys.stderr)
306
+ return 2
307
+ out = Path(args.output) if args.output else None
308
+ to_dir = out is not None and (len(project.files) > 1 or inputs[0].is_dir() or out.suffix == "" or out.is_dir())
309
+ if out is None and len(project.files) > 1:
310
+ print("error: several files: give -o DIR", file=sys.stderr)
311
+ return 2
312
+ sources = []
313
+ for f in project.files:
314
+ root = next((p for p in inputs if p.is_dir() and f.resolve().is_relative_to(p.resolve())), None)
315
+ rel = f.resolve().relative_to(root.resolve()).as_posix() if root else f.name
316
+ sources.append((str(f), rel, f.read_text(encoding="utf-8")))
317
+ schema = compile_sources([(d, t) for d, _, t in sources])
318
+ if not _print_diagnostics(schema, False):
319
+ return 1
320
+ res = convert.convert(sources, args.to, split_groups=to_dir)
321
+ for w in res.warnings:
322
+ print(w.format(), file=sys.stderr)
323
+ again = compile_sources(list(res.files.items()))
324
+ if again.errors or convert.canonical(again, res.lost) != convert.canonical(schema, res.lost):
325
+ for d in again.errors:
326
+ print(d.format(), file=sys.stderr)
327
+ print("error: converted schema differs from the input (please report this as a bug)", file=sys.stderr)
328
+ return 1
329
+ if out is None:
330
+ sys.stdout.write(next(iter(res.files.values())))
331
+ if res.layout:
332
+ print("warning: [layout] positions not written (give -o to write a layout file)", file=sys.stderr)
333
+ return 0
334
+ written = []
335
+ if to_dir:
336
+ for name, text in res.files.items():
337
+ p = out / name
338
+ p.parent.mkdir(parents=True, exist_ok=True)
339
+ p.write_text(text, encoding="utf-8")
340
+ written.append(p)
341
+ else:
342
+ out.parent.mkdir(parents=True, exist_ok=True)
343
+ out.write_text(next(iter(res.files.values())), encoding="utf-8")
344
+ written.append(out)
345
+ if res.layout:
346
+ sidecar = layout.default_sidecar(written, [out])
347
+ positions = {**{k: tuple(v) for k, v in res.layout.items()}, **layout.read_sidecar(sidecar)}
348
+ layout.write_sidecar(sidecar, positions)
349
+ written.append(sidecar)
350
+ print(f"Wrote {', '.join(str(p) for p in written)}")
351
+ return 0
352
+
353
+
297
354
  def cmd_path(args) -> int:
298
355
  from . import export
299
356
  from .jsrt import runtime
@@ -398,7 +455,7 @@ def build_parser() -> argparse.ArgumentParser:
398
455
  sub = p.add_subparsers(dest="cmd", required=True)
399
456
 
400
457
  def paths(sp):
401
- sp.add_argument("paths", nargs="+", metavar="PATH", help="schema .txt file(s) or folder(s) (recursive)")
458
+ sp.add_argument("paths", nargs="+", metavar="PATH", help="schema .txt/.dbml file(s) or folder(s) (recursive)")
402
459
 
403
460
  def selectors(sp):
404
461
  sp.add_argument(
@@ -520,6 +577,14 @@ def build_parser() -> argparse.ArgumentParser:
520
577
  )
521
578
  dp.set_defaults(func=cmd_import_dbt)
522
579
 
580
+ sp = sub.add_parser("convert", help="convert schema files between QuickDBD text and DBML")
581
+ sp.add_argument("paths", nargs="+", metavar="PATH", help="schema .txt/.dbml file(s) or folder(s) (recursive)")
582
+ sp.add_argument("--to", required=True, choices=["dbml", "quickdbd"], help="target syntax")
583
+ sp.add_argument(
584
+ "-o", "--output", help="output file, or a directory (keeps relative paths; DBML TableGroups become files)"
585
+ )
586
+ sp.set_defaults(func=cmd_convert)
587
+
523
588
  sp = sub.add_parser("serve", help="live editor: reload on save, drag tables to arrange")
524
589
  paths(sp)
525
590
  sp.add_argument("--port", type=int, help="port to listen on (default: 8765, or the next free one)")
@@ -0,0 +1,375 @@
1
+ """`qdbd convert`: QuickDBD text <-> DBML.
2
+
3
+ Each input file becomes one output file (or, DBML -> QuickDBD with ``split_groups``, one file per TableGroup, since
4
+ QuickDBD groups tables by file). Whatever the target can't express is reported as a warning and recorded in
5
+ ``ConvertResult.lost``; ``canonical`` leaves those out, so the caller's self-check (recompile the output, compare)
6
+ holds for everything else.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import dataclasses
12
+ import re
13
+ from dataclasses import dataclass, field
14
+ from pathlib import PurePosixPath
15
+
16
+ from .dbml import REL_OPS
17
+ from .model import REL_TYPES, Column, Diagnostic, Relationship, Schema, Table
18
+ from .parser import FileParse, compile_files
19
+
20
+ SUFFIX = {"dbml": ".dbml", "quickdbd": ".txt"}
21
+ _QDBD_OP = {cards: op for op, cards in REL_TYPES.items()}
22
+ _DBML_OP = {cards: op for op, cards in REL_OPS.items()}
23
+ _PLAIN_DBML = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
24
+ _PLAIN_QDBD = re.compile(r"[A-Za-z0-9_]+")
25
+ _ARG = r"([+-]*\d+(\.\d+)?|[A-Za-z_]\w*)" # what dbdiagram.io takes inside a type's parentheses
26
+ _DBML_TYPE = re.compile(rf"[A-Za-z_]\w*(\.[A-Za-z_]\w*)?(\({_ARG}(,{_ARG})*\))?(\[\d*\])*")
27
+ _NUMBER = re.compile(r"[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?")
28
+ _EXPRESSION = re.compile(r"[A-Z][A-Z0-9_]*|.*\(.*\).*") # CURRENT_TIMESTAMP, now(), GETUTCDATE()
29
+
30
+
31
+ @dataclass
32
+ class ConvertResult:
33
+ files: dict[str, str] = field(default_factory=dict) # output path (relative) -> text
34
+ warnings: list[Diagnostic] = field(default_factory=list)
35
+ lost: set[tuple] = field(default_factory=set) # canonical keys the target can't hold
36
+ layout: dict[str, tuple[float, float]] = field(default_factory=dict) # QuickDBD [layout], by table name
37
+
38
+
39
+ def convert(sources: list[tuple[str, str, str]], to: str, split_groups: bool) -> ConvertResult:
40
+ """``sources``: ``(display path, output-relative path, text)``. Files already in ``to`` are copied as is."""
41
+ schema, parses = compile_files([(d, t) for d, _, t in sources])
42
+ res = ConvertResult()
43
+ for t in schema.tables.values():
44
+ if t.id in schema.layout:
45
+ res.layout[t.name] = schema.layout[t.id]
46
+ outs = [str(PurePosixPath(rel).with_suffix(SUFFIX[to])) for _, rel, _ in sources]
47
+ copied = {fp.path for fp in parses if fp.syntax == to}
48
+ taken = {out for out, fp in zip(outs, parses, strict=True) if fp.path in copied}
49
+ fks = _plan_foreign_keys(schema, {fp.path for fp in parses} - copied, res) if to == "quickdbd" else {}
50
+ for (_, _, text), fp, out in zip(sources, parses, outs, strict=True):
51
+ if fp.path in copied:
52
+ res.files[out] = text
53
+ continue
54
+ if to == "dbml":
55
+ files = _DbmlWriter(schema, fp, res).write(out)
56
+ else:
57
+ files = _QuickdbdWriter(schema, fp, res, split_groups, fks, taken).write(out)
58
+ res.files.update(files)
59
+ return res
60
+
61
+
62
+ # --------------------------------------------------------------------------- canonical form (the self-check)
63
+
64
+
65
+ def _norm_default(value: str | None) -> str | None:
66
+ if value is None:
67
+ return None
68
+ return value.lower() if value.lower() in ("true", "false", "null") else value
69
+
70
+
71
+ def _doc(lines: list[str]) -> list[str]:
72
+ return [x.strip() for x in lines if x.strip()]
73
+
74
+
75
+ def canonical(schema: Schema, lost: set[tuple]) -> dict:
76
+ """Tables, columns and relationships by name, minus the ``lost`` keys."""
77
+ tables: dict = {}
78
+ for t in schema.tables.values():
79
+ cols = {}
80
+ for c in t.columns.values():
81
+ if c.virtual:
82
+ continue
83
+ col = {
84
+ "type": c.type or "", "pk": c.pk, "unique": c.unique, "index": c.index, "nullable": c.nullable,
85
+ "identity": c.identity, "default": _norm_default(c.default), "doc": _doc(c.comments),
86
+ } # fmt: skip
87
+ for k in list(col):
88
+ if ("col", t.name, c.name, k) in lost:
89
+ del col[k]
90
+ cols[c.name] = col
91
+ table = {"cols": cols, "doc": _doc(t.comments), "aliases": t.aliases, "tags": t.tags, "dbt_tags": t.dbt_tags}
92
+ for k in list(table):
93
+ if ("table", t.name, k) in lost:
94
+ del table[k]
95
+ tables[t.name] = table
96
+ rels = {}
97
+ for r in schema.valid_relationships():
98
+ key, cards = _rel_key(schema, r)
99
+ if ("rel", *key) not in lost:
100
+ rels[key] = cards
101
+ return {"tables": tables, "rels": rels}
102
+
103
+
104
+ def _end(schema: Schema, e) -> tuple[str, str | None]:
105
+ t = schema.tables[e.table]
106
+ c = t.columns[e.column]
107
+ return t.name, None if c.virtual else c.name
108
+
109
+
110
+ def _rel_key(schema: Schema, r: Relationship) -> tuple[tuple, tuple]:
111
+ a, b = _end(schema, r.a), _end(schema, r.b)
112
+ if (a[0], a[1] or "") <= (b[0], b[1] or ""):
113
+ return (a, b), (r.a.card, r.b.card)
114
+ return (b, a), (r.b.card, r.a.card)
115
+
116
+
117
+ # --------------------------------------------------------------------------- DBML output
118
+
119
+
120
+ def _dbml_name(name: str) -> str:
121
+ if _PLAIN_DBML.fullmatch(name):
122
+ return name
123
+ return '"' + name.replace("\\", "\\\\").replace('"', '\\"') + '"'
124
+
125
+
126
+ def _dbml_string(text: str) -> str:
127
+ return "'" + text.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n") + "'"
128
+
129
+
130
+ def _dbml_default(value: str) -> str:
131
+ if _NUMBER.fullmatch(value) or value.lower() in ("true", "false", "null"):
132
+ return value
133
+ if _EXPRESSION.fullmatch(value) and "`" not in value:
134
+ return f"`{value}`"
135
+ return _dbml_string(value)
136
+
137
+
138
+ class _DbmlWriter:
139
+ def __init__(self, schema: Schema, fp: FileParse, res: ConvertResult):
140
+ self.schema, self.fp, self.res = schema, fp, res
141
+
142
+ def warn(self, line: int, msg: str) -> None:
143
+ self.res.warnings.append(Diagnostic(self.fp.path, line, msg, "warning"))
144
+
145
+ def write(self, out: str) -> dict[str, str]:
146
+ fp, lines = self.fp, []
147
+ lines += [f"// {c}" for c in fp.general_comments]
148
+ if lines:
149
+ lines.append("")
150
+ for k in ("zoom", "position"):
151
+ if fp.settings.get(k) is not None:
152
+ line = fp.settings.get("_lines", {}).get(k, 1)
153
+ self.warn(line, f"settings: [settings] {k.capitalize()} has no DBML equivalent; dropped")
154
+ for tag, (color, _) in sorted(fp.colors.items()):
155
+ lines.append(f"// @color {tag} {color}")
156
+ if fp.colors:
157
+ lines.append("")
158
+ for t in (t for t in self.schema.tables.values() if t.path == fp.path):
159
+ lines += self.table(t)
160
+ lines.append("")
161
+ for r in (r for r in self.schema.relationships if r.path == fp.path):
162
+ ref = self.ref(r)
163
+ if ref:
164
+ lines.append(ref)
165
+ return {out: "\n".join(lines).rstrip("\n") + "\n"}
166
+
167
+ def table(self, t: Table) -> list[str]:
168
+ out = []
169
+ if t.tags:
170
+ out.append(f"// @tag: {', '.join(t.tags)}")
171
+ if t.dbt_tags:
172
+ out.append(f"// @dbt-tags: {', '.join(t.dbt_tags)}")
173
+ head = f"Table {_dbml_name(t.name)}"
174
+ if t.aliases:
175
+ head += f" as {_dbml_name(t.aliases[0])}"
176
+ if len(t.aliases) > 1:
177
+ self.warn(t.line, f"alias: DBML takes one alias; dropped {', '.join(t.aliases[1:])}")
178
+ self.res.lost.add(("table", t.name, "aliases"))
179
+ out.append(head + " {")
180
+ indexed = []
181
+ for c in t.columns.values():
182
+ if c.virtual:
183
+ continue
184
+ settings = []
185
+ if c.pk:
186
+ settings.append("pk")
187
+ if c.identity:
188
+ settings.append("increment")
189
+ if c.nullable:
190
+ settings.append("null")
191
+ if c.unique:
192
+ settings.append("unique")
193
+ if c.default is not None:
194
+ settings.append(f"default: {_dbml_default(c.default)}")
195
+ if c.comments:
196
+ settings.append(f"note: {_dbml_string(chr(10).join(c.comments))}")
197
+ if c.index:
198
+ indexed.append(c.name)
199
+ typ = c.type or ""
200
+ if not typ:
201
+ self.warn(c.line, f"type: column '{c.name}' has no type, which DBML requires; wrote 'unknown'")
202
+ self.res.lost.add(("col", t.name, c.name, "type"))
203
+ typ = "unknown"
204
+ typ = typ if _DBML_TYPE.fullmatch(typ) else _dbml_name(typ)
205
+ out.append(f" {_dbml_name(c.name)} {typ}" + (f" [{', '.join(settings)}]" if settings else ""))
206
+ if indexed:
207
+ out += ["", " indexes {"] + [f" {_dbml_name(n)}" for n in indexed] + [" }"]
208
+ if t.comments:
209
+ lines = [x.replace("\\", "\\\\").replace("'''", "\\'''") for x in t.comments]
210
+ out += ["", " Note: '''"] + [f" {x}".rstrip() for x in lines] + [" '''"]
211
+ out.append("}")
212
+ return out
213
+
214
+ def ref(self, r: Relationship) -> str | None:
215
+ a, b = _end(self.schema, r.a), _end(self.schema, r.b)
216
+ if a[1] is None or b[1] is None:
217
+ key, _ = _rel_key(self.schema, r)
218
+ self.res.lost.add(("rel", *key))
219
+ self.warn(r.line, f"table-level relationship: {a[0]} - {b[0]} has no DBML form; kept as a comment")
220
+ return f"// rel: {a[0]} {_QDBD_OP.get((r.a.card, r.b.card), '-')} {b[0]}"
221
+ op = _DBML_OP[(r.a.card, r.b.card)]
222
+ return f"Ref: {_dbml_name(a[0])}.{_dbml_name(a[1])} {op} {_dbml_name(b[0])}.{_dbml_name(b[1])}"
223
+
224
+
225
+ # --------------------------------------------------------------------------- QuickDBD output
226
+
227
+
228
+ def _qdbd_name(name: str) -> str:
229
+ return name if _PLAIN_QDBD.fullmatch(name) else f'"{name}"'
230
+
231
+
232
+ def _quickdbd_column(col: Column, fks: list[str]) -> str:
233
+ parts = [_qdbd_name(col.name)]
234
+ if col.pk:
235
+ parts.append("PK")
236
+ typ = col.type or ""
237
+ parts.append(_qdbd_name(typ) if " " in typ or "#" in typ else typ)
238
+ if col.nullable:
239
+ parts.append("NULL")
240
+ if col.unique and not col.pk:
241
+ parts.append("UNIQUE")
242
+ if col.index and not (col.pk or col.unique):
243
+ parts.append("INDEX")
244
+ if col.identity:
245
+ parts.append("IDENTITY")
246
+ if col.default is not None:
247
+ d = col.default
248
+ parts.append(f'default="{d}"' if re.search(r"[\s#]", d) else f"default={d}")
249
+ parts += fks
250
+ return " ".join(p for p in parts if p)
251
+
252
+
253
+ def _plan_foreign_keys(schema: Schema, written: set[str], res: ConvertResult) -> dict[tuple[str, str], list[str]]:
254
+ """`FK op table.column` clauses per (table id, column id), for the relationships declared in the files being
255
+ written. A clause goes on a column of a table that's being written, the non-key end when it can."""
256
+ out: dict[tuple[str, str], list[str]] = {}
257
+ valid = {id(r) for r in schema.valid_relationships()}
258
+
259
+ def warn(r: Relationship, msg: str) -> None:
260
+ res.warnings.append(Diagnostic(r.path, r.line, msg, "warning"))
261
+
262
+ for r in schema.relationships:
263
+ if r.path not in written or id(r) not in valid:
264
+ continue
265
+ ends = [(r.a, r.b), (r.b, r.a)]
266
+ if schema.tables[r.a.table].columns[r.a.column].pk and not schema.tables[r.b.table].columns[r.b.column].pk:
267
+ ends.reverse()
268
+ ends = [e for e in ends if schema.tables[e[0].table].path in written]
269
+ key, _ = _rel_key(schema, r)
270
+ if not ends:
271
+ res.lost.add(("rel", *key))
272
+ warn(r, "relationship: both tables are in files kept as they are; add it to one of them by hand")
273
+ continue
274
+ chosen = next(((h, t, _QDBD_OP[(h.card, t.card)]) for h, t in ends if (h.card, t.card) in _QDBD_OP), None)
275
+ if chosen is None:
276
+ here, there = ends[0]
277
+ chosen = (here, there, _QDBD_OP[(_required(here.card), _required(there.card))])
278
+ res.lost.add(("rel", *key))
279
+ warn(r, "relationship: QuickDBD has no operator for these cardinalities; made both sides required")
280
+ here, there, op = chosen
281
+ t = schema.tables[there.table]
282
+ target = f"{_qdbd_name(t.name)}.{_qdbd_name(t.columns[there.column].name)}"
283
+ out.setdefault((here.table, here.column), []).append(f"FK {op} {target}")
284
+ return out
285
+
286
+
287
+ class _QuickdbdWriter:
288
+ def __init__(
289
+ self,
290
+ schema: Schema,
291
+ fp: FileParse,
292
+ res: ConvertResult,
293
+ split_groups: bool,
294
+ fks: dict[tuple[str, str], list[str]],
295
+ taken: set[str],
296
+ ):
297
+ self.schema, self.fp, self.res, self.split = schema, fp, res, split_groups
298
+ self.fks, self.taken = fks, taken # FK clauses for every file; output names already used
299
+
300
+ def warn(self, line: int, msg: str) -> None:
301
+ self.res.warnings.append(Diagnostic(self.fp.path, line, msg, "warning"))
302
+
303
+ def write(self, out: str) -> dict[str, str]:
304
+ fp = self.fp
305
+ tables = [t for t in self.schema.tables.values() if t.path == fp.path]
306
+ for line, what in fp.unmodelled:
307
+ self.warn(line, f"{what}: no QuickDBD equivalent; dropped")
308
+ header = [f"# {c}" for c in fp.general_comments]
309
+ if header:
310
+ header.append("")
311
+ for name, values in fp.enums.items():
312
+ header.append(f"# enum {name}: {', '.join(values)}")
313
+ for tag, (color, _) in sorted(fp.colors.items()):
314
+ header.append(f"# @color {tag} {color}")
315
+ if not self.split:
316
+ for group, line in fp.groups.items():
317
+ self.warn(line, f"TableGroup {group}: QuickDBD groups tables by file (use -o DIR to split); dropped")
318
+ groups: dict[str, list[Table]] = {}
319
+ for t in tables:
320
+ groups.setdefault(t.group if (t.group and self.split) else "", []).append(t)
321
+ if not groups:
322
+ groups[""] = []
323
+ files = {}
324
+ for group, members in groups.items():
325
+ wanted = str(PurePosixPath(out).with_name(re.sub(r"[^\w.-]+", "_", group) + ".txt")) if group else out
326
+ name, n = wanted, 1
327
+ while name in self.taken:
328
+ n += 1
329
+ name = str(PurePosixPath(wanted).with_name(f"{PurePosixPath(wanted).stem}-{n}.txt"))
330
+ if name != wanted:
331
+ self.warn(fp.groups.get(group, 1), f"TableGroup {group}: {wanted} is taken; wrote {name}")
332
+ self.taken.add(name)
333
+ body = (header + [""] if not files else []) + [line for t in members for line in [*self.table(t), ""]]
334
+ files[name] = re.sub(r"\n{3,}", "\n\n", "\n".join(body)).rstrip("\n") + "\n"
335
+ return files
336
+
337
+ def table(self, t: Table) -> list[str]:
338
+ out = []
339
+ if t.color:
340
+ self.warn(t.line, f"headercolor: QuickDBD has no table colour; dropped {t.color}")
341
+ if any("#" in x for x in t.comments):
342
+ self.warn(t.line, f"note: QuickDBD ends a comment at '#', so the note of {t.name} is cut there")
343
+ self.res.lost.add(("table", t.name, "doc"))
344
+ out += [f"# {x}" for x in t.comments if x.strip()]
345
+ if t.tags:
346
+ out.append(f"# @tag: {', '.join(t.tags)}")
347
+ if t.dbt_tags:
348
+ out.append(f"# @dbt-tags: {', '.join(t.dbt_tags)}")
349
+ out.append(_qdbd_name(t.name) + (f" as {', '.join(t.aliases)}" if t.aliases else ""))
350
+ out.append("-")
351
+ for c in t.columns.values():
352
+ if c.virtual:
353
+ continue
354
+ if c.pk and c.unique:
355
+ self.warn(c.line, f"unique: '{c.name}' is a primary key, which QuickDBD treats as unique already")
356
+ self.res.lost.add(("col", t.name, c.name, "unique"))
357
+ if c.index and (c.pk or c.unique):
358
+ self.warn(c.line, f"index: '{c.name}' is a key, which QuickDBD treats as indexed already")
359
+ self.res.lost.add(("col", t.name, c.name, "index"))
360
+ if any("#" in x for x in c.comments):
361
+ self.warn(
362
+ c.line, f"note: QuickDBD ends a comment at '#', so the note of {t.name}.{c.name} is cut there"
363
+ )
364
+ self.res.lost.add(("col", t.name, c.name, "doc"))
365
+ if c.default is not None and '"' in c.default:
366
+ self.warn(c.line, f"default: QuickDBD can't hold a '\"' in a default; dropped {t.name}.{c.name}'s")
367
+ self.res.lost.add(("col", t.name, c.name, "default"))
368
+ c = dataclasses.replace(c, default=None)
369
+ out += [f"# {x}" for x in c.comments if x.strip()]
370
+ out.append(_quickdbd_column(c, self.fks.get((t.id, c.id), [])))
371
+ return out
372
+
373
+
374
+ def _required(card: str) -> str:
375
+ return {"zeroOne": "one", "zeroMany": "many"}.get(card, card)