mirror of
https://github.com/github/codeql.git
synced 2025-12-16 00:33:11 +01:00
Codegen: introduce name conflict error in dbschemegen
This commit is contained in:
@@ -24,6 +24,10 @@ from misc.codegen.lib.dbscheme import *
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Error(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def dbtype(typename: str, add_or_none_except: typing.Optional[str] = None) -> str:
|
||||
""" translate a type to a dbscheme counterpart, using `@lower_underscore` format for classes.
|
||||
For class types, appends an underscore followed by `null` if provided
|
||||
@@ -108,6 +112,16 @@ def cls_to_dbscheme(cls: schema.Class, lookup: typing.Dict[str, schema.Class], a
|
||||
)
|
||||
|
||||
|
||||
def check_name_conflicts(decls: list[Table | Union]):
|
||||
names = set()
|
||||
for decl in decls:
|
||||
match decl:
|
||||
case Table(name=name):
|
||||
if name in names:
|
||||
raise Error(f"Duplicate table name: {name}")
|
||||
names.add(name)
|
||||
|
||||
|
||||
def get_declarations(data: schema.Schema):
|
||||
add_or_none_except = data.root_class.name if data.null else None
|
||||
declarations = [d for cls in data.classes.values() if not cls.imported for d in cls_to_dbscheme(cls,
|
||||
@@ -120,6 +134,7 @@ def get_declarations(data: schema.Schema):
|
||||
declarations += [
|
||||
Union(dbtype(t, data.null), [dbtype(t), dbtype(data.null)]) for t in sorted(property_classes)
|
||||
]
|
||||
check_name_conflicts(declarations)
|
||||
return declarations
|
||||
|
||||
|
||||
|
||||
@@ -593,5 +593,15 @@ def test_synth_properties_ignored(generate):
|
||||
)
|
||||
|
||||
|
||||
def test_table_conflict(generate, dir_param):
|
||||
with pytest.raises(dbschemegen.Error):
|
||||
generate([
|
||||
schema.Class("Foo", pragmas={"group": dir_param.input}, properties=[
|
||||
schema.OptionalProperty("bar", "FooBar"),
|
||||
]),
|
||||
schema.Class("FooBar", pragmas={"group": dir_param.input}),
|
||||
])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(pytest.main([__file__] + sys.argv[1:]))
|
||||
|
||||
Reference in New Issue
Block a user