Skip to content

Commit f48aa79

Browse files
author
Paolo Tranquilli
committed
Codegen: implement db_table_name in qlgen
1 parent fc9e066 commit f48aa79

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

misc/codegen/generators/qlgen.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
130130
internal="ql_internal" in prop.pragmas,
131131
)
132132
ql_name = prop.pragmas.get("ql_name", prop.name)
133+
db_table_name = prop.pragmas.get("ql_db_table_name")
134+
if db_table_name and prop.is_single:
135+
raise Error(f"`db_table_name` pragma is not supported for single properties, but {cls.name}.{prop.name} has it")
133136
if prop.is_single:
134137
args.update(
135138
singular=inflection.camelize(ql_name),
@@ -141,22 +144,22 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, lookup: typing.Dic
141144
args.update(
142145
singular=inflection.singularize(inflection.camelize(ql_name)),
143146
plural=inflection.pluralize(inflection.camelize(ql_name)),
144-
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
147+
tablename=db_table_name or inflection.tableize(f"{cls.name}_{prop.name}"),
145148
tableparams=["this", "index", "result"] if not prop.is_unordered else ["this", "result"],
146149
doc=_get_doc(cls, prop, plural=False),
147150
doc_plural=_get_doc(cls, prop, plural=True),
148151
)
149152
elif prop.is_optional:
150153
args.update(
151154
singular=inflection.camelize(ql_name),
152-
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
155+
tablename=db_table_name or inflection.tableize(f"{cls.name}_{prop.name}"),
153156
tableparams=["this", "result"],
154157
doc=_get_doc(cls, prop),
155158
)
156159
elif prop.is_predicate:
157160
args.update(
158161
singular=inflection.camelize(ql_name, uppercase_first_letter=False),
159-
tablename=inflection.underscore(f"{cls.name}_{prop.name}"),
162+
tablename=db_table_name or inflection.underscore(f"{cls.name}_{prop.name}"),
160163
tableparams=["this"],
161164
doc=_get_doc(cls, prop),
162165
)

misc/codegen/test/test_qlgen.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,5 +1013,38 @@ def test_hideable_property(generate_classes):
10131013
}
10141014

10151015

1016+
def test_property_with_custom_db_table_name(generate_classes):
1017+
assert generate_classes([
1018+
schema.Class("Obj", properties=[
1019+
schema.OptionalProperty("x", "a", pragmas={"ql_db_table_name": "foo"}),
1020+
schema.RepeatedProperty("y", "b", pragmas={"ql_db_table_name": "bar"}),
1021+
schema.RepeatedOptionalProperty("z", "c", pragmas={"ql_db_table_name": "baz"}),
1022+
schema.PredicateProperty("p", pragmas={"ql_db_table_name": "hello"}),
1023+
schema.RepeatedUnorderedProperty("q", "d", pragmas={"ql_db_table_name": "world"}),
1024+
]),
1025+
]) == {
1026+
"Obj.qll": (a_ql_class_public(name="Obj"),
1027+
a_ql_stub(name="Obj"),
1028+
a_ql_class(name="Obj", final=True, properties=[
1029+
ql.Property(singular="X", type="a", tablename="foo",
1030+
tableparams=["this", "result"],
1031+
is_optional=True, doc="x of this obj"),
1032+
ql.Property(singular="Y", plural="Ys", type="b", tablename="bar",
1033+
tableparams=["this", "index", "result"],
1034+
doc="y of this obj", doc_plural="ys of this obj"),
1035+
ql.Property(singular="Z", plural="Zs", type="c", tablename="baz",
1036+
tableparams=["this", "index", "result"],
1037+
is_optional=True, doc="z of this obj", doc_plural="zs of this obj"),
1038+
ql.Property(singular="p", type="predicate", tablename="hello",
1039+
tableparams=["this"], is_predicate=True,
1040+
doc="this obj p"),
1041+
ql.Property(singular="Q", plural="Qs", type="d", tablename="world",
1042+
tableparams=["this", "result"], is_unordered=True,
1043+
doc="q of this obj", doc_plural="qs of this obj"),
1044+
],
1045+
imports=[stub_import_prefix + "Obj"])),
1046+
}
1047+
1048+
10161049
if __name__ == '__main__':
10171050
sys.exit(pytest.main([__file__] + sys.argv[1:]))

0 commit comments

Comments
 (0)