Codegen: implement db_table_name in qlgen

This commit is contained in:
Paolo Tranquilli
2025-03-19 14:50:44 +01:00
parent fc9e066ecd
commit f48aa79927
2 changed files with 39 additions and 3 deletions

View File

@@ -130,6 +130,9 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
internal="ql_internal" in prop.pragmas,
)
ql_name = prop.pragmas.get("ql_name", prop.name)
db_table_name = prop.pragmas.get("ql_db_table_name")
if db_table_name and prop.is_single:
raise Error(f"`db_table_name` pragma is not supported for single properties, but {cls.name}.{prop.name} has it")
if prop.is_single:
args.update(
singular=inflection.camelize(ql_name),
@@ -141,7 +144,7 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
args.update(
singular=inflection.singularize(inflection.camelize(ql_name)),
plural=inflection.pluralize(inflection.camelize(ql_name)),
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
tablename=db_table_name or inflection.tableize(f"{cls.name}_{prop.name}"),
tableparams=["this", "index", "result"] if not prop.is_unordered else ["this", "result"],
doc=_get_doc(cls, prop, plural=False),
doc_plural=_get_doc(cls, prop, plural=True),
@@ -149,14 +152,14 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
elif prop.is_optional:
args.update(
singular=inflection.camelize(ql_name),
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
tablename=db_table_name or inflection.tableize(f"{cls.name}_{prop.name}"),
tableparams=["this", "result"],
doc=_get_doc(cls, prop),
)
elif prop.is_predicate:
args.update(
singular=inflection.camelize(ql_name, uppercase_first_letter=False),
tablename=inflection.underscore(f"{cls.name}_{prop.name}"),
tablename=db_table_name or inflection.underscore(f"{cls.name}_{prop.name}"),
tableparams=["this"],
doc=_get_doc(cls, prop),
)

View File

@@ -1013,5 +1013,38 @@ def test_hideable_property(generate_classes):
}
def test_property_with_custom_db_table_name(generate_classes):
assert generate_classes([
schema.Class("Obj", properties=[
schema.OptionalProperty("x", "a", pragmas={"ql_db_table_name": "foo"}),
schema.RepeatedProperty("y", "b", pragmas={"ql_db_table_name": "bar"}),
schema.RepeatedOptionalProperty("z", "c", pragmas={"ql_db_table_name": "baz"}),
schema.PredicateProperty("p", pragmas={"ql_db_table_name": "hello"}),
schema.RepeatedUnorderedProperty("q", "d", pragmas={"ql_db_table_name": "world"}),
]),
]) == {
"Obj.qll": (a_ql_class_public(name="Obj"),
a_ql_stub(name="Obj"),
a_ql_class(name="Obj", final=True, properties=[
ql.Property(singular="X", type="a", tablename="foo",
tableparams=["this", "result"],
is_optional=True, doc="x of this obj"),
ql.Property(singular="Y", plural="Ys", type="b", tablename="bar",
tableparams=["this", "index", "result"],
doc="y of this obj", doc_plural="ys of this obj"),
ql.Property(singular="Z", plural="Zs", type="c", tablename="baz",
tableparams=["this", "index", "result"],
is_optional=True, doc="z of this obj", doc_plural="zs of this obj"),
ql.Property(singular="p", type="predicate", tablename="hello",
tableparams=["this"], is_predicate=True,
doc="this obj p"),
ql.Property(singular="Q", plural="Qs", type="d", tablename="world",
tableparams=["this", "result"], is_unordered=True,
doc="q of this obj", doc_plural="qs of this obj"),
],
imports=[stub_import_prefix + "Obj"])),
}
if __name__ == '__main__':
sys.exit(pytest.main([__file__] + sys.argv[1:]))