|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import pathlib |
| 3 | + |
| 4 | +import inflection |
| 5 | + |
| 6 | +from lib import paths, schema, generator |
| 7 | +from lib.dbscheme import * |
| 8 | + |
| 9 | +log = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def dbtype(typename): |
| 13 | + """ translate a type to a dbscheme counterpart, using `@lower_underscore` format for classes """ |
| 14 | + if typename[0].isupper(): |
| 15 | + return "@" + inflection.underscore(typename) |
| 16 | + return typename |
| 17 | + |
| 18 | + |
| 19 | +def cls_to_dbscheme(cls: schema.Class): |
| 20 | + """ Yield all dbscheme entities needed to model class `cls` """ |
| 21 | + if cls.derived: |
| 22 | + yield DbUnion(dbtype(cls.name), (dbtype(c) for c in cls.derived)) |
| 23 | + # output a table specific to a class only if it is a leaf class or it has 1-to-1 properties |
| 24 | + # Leaf classes need a table to bind the `@` ids |
| 25 | + # 1-to-1 properties are added to a class specific table |
| 26 | + # in other cases, separate tables are used for the properties, and a class specific table is unneeded |
| 27 | + if not cls.derived or any(f.is_single for f in cls.properties): |
| 28 | + binding = not cls.derived |
| 29 | + keyset = DbKeySet(["id"]) if cls.derived else None |
| 30 | + yield DbTable( |
| 31 | + keyset=keyset, |
| 32 | + name=inflection.tableize(cls.name), |
| 33 | + columns=[ |
| 34 | + DbColumn("id", type=dbtype(cls.name), binding=binding), |
| 35 | + ] + [ |
| 36 | + DbColumn(f.name, dbtype(f.type)) for f in cls.properties if f.is_single |
| 37 | + ] |
| 38 | + ) |
| 39 | + # use property-specific tables for 1-to-many and 1-to-at-most-1 properties |
| 40 | + for f in cls.properties: |
| 41 | + if f.is_optional: |
| 42 | + yield DbTable( |
| 43 | + keyset=DbKeySet(["id"]), |
| 44 | + name=inflection.tableize(f"{cls.name}_{f.name}"), |
| 45 | + columns=[ |
| 46 | + DbColumn("id", type=dbtype(cls.name)), |
| 47 | + DbColumn(f.name, dbtype(f.type)), |
| 48 | + ], |
| 49 | + ) |
| 50 | + elif f.is_repeated: |
| 51 | + yield DbTable( |
| 52 | + keyset=DbKeySet(["id", "index"]), |
| 53 | + name=inflection.tableize(f"{cls.name}_{f.name}"), |
| 54 | + columns=[ |
| 55 | + DbColumn("id", type=dbtype(cls.name)), |
| 56 | + DbColumn("index", type="int"), |
| 57 | + DbColumn(inflection.singularize(f.name), dbtype(f.type)), |
| 58 | + ] |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def get_declarations(data: schema.Schema): |
| 63 | + return [d for cls in data.classes.values() for d in cls_to_dbscheme(cls)] |
| 64 | + |
| 65 | + |
| 66 | +def get_includes(data: schema.Schema, include_dir: pathlib.Path): |
| 67 | + includes = [] |
| 68 | + for inc in data.includes: |
| 69 | + inc = include_dir / inc |
| 70 | + with open(inc) as inclusion: |
| 71 | + includes.append(DbSchemeInclude(src=inc.relative_to(paths.swift_dir), data=inclusion.read())) |
| 72 | + return includes |
| 73 | + |
| 74 | + |
| 75 | +def generate(opts, renderer): |
| 76 | + input = opts.schema.resolve() |
| 77 | + out = opts.dbscheme.resolve() |
| 78 | + |
| 79 | + with open(input) as src: |
| 80 | + data = schema.load(src) |
| 81 | + |
| 82 | + dbscheme = DbScheme(src=input.relative_to(paths.swift_dir), |
| 83 | + includes=get_includes(data, include_dir=input.parent), |
| 84 | + declarations=get_declarations(data)) |
| 85 | + |
| 86 | + renderer.render(dbscheme, out) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + generator.run(generate, tags=["schema", "dbscheme"]) |
0 commit comments