ifc2sql - fix regression with missing resulting database filepath (9055f14) #6758

This commit is contained in:
Andrej
2025-06-04 12:34:02 +05:00
parent a0e4bdcbef
commit b07b0e4bea
+28 -11
View File
@@ -71,7 +71,7 @@ class Patcher(ifcpatch.BasePatcher):
host: str = "localhost", host: str = "localhost",
username: str = "root", username: str = "root",
password: str = "pass", password: str = "pass",
database: str = f"{DEFAULT_DATABASE_NAME}.sqlite", database: str = DEFAULT_DATABASE_NAME,
full_schema: bool = True, full_schema: bool = True,
is_strict: bool = False, is_strict: bool = False,
should_expand: bool = False, should_expand: bool = False,
@@ -83,9 +83,12 @@ class Patcher(ifcpatch.BasePatcher):
"""Convert an IFC-SPF model to SQLite or MySQL. """Convert an IFC-SPF model to SQLite or MySQL.
:param sql_type: Choose between "SQLite" or "MySQL" :param sql_type: Choose between "SQLite" or "MySQL"
:param database: Database path to save the SQL database to (already existing or not). :param database:
For SQLite - database path to save the SQL database to (already existing or not).
Could also be a directory, then the database will be stored Could also be a directory, then the database will be stored
using default filename (e.g. 'database.sqlite'). using default filename (e.g. 'database.sqlite').
For MySQL - database name.
:filter_glob database: *.db;*.sqlite :filter_glob database: *.db;*.sqlite
:param full_schema: if True, will create tables for all IFC classes, :param full_schema: if True, will create tables for all IFC classes,
regardless if they are used or not in the dataset. If False, will regardless if they are used or not in the dataset. If False, will
@@ -137,27 +140,41 @@ class Patcher(ifcpatch.BasePatcher):
geometry_rows: dict[str, tuple[str, bytes, bytes, bytes, bytes, str]] geometry_rows: dict[str, tuple[str, bytes, bytes, bytes, bytes, str]]
shape_rows: dict[int, tuple[int, list[float], list[float], list[float], bytes, str]] shape_rows: dict[int, tuple[int, list[float], list[float], list[float], bytes, str]]
def get_output(self) -> Union[str, None]:
"""Return resulting database filepath for sqlite and ``None`` for mysql."""
return self.file_patched
def patch(self) -> None: def patch(self) -> None:
suffix = ".db" if self.sql_type == "SQLite" else ".sqlite" if self.sql_type == "sqlite":
database = Path(self.database) database = Path(self.database)
if database.is_dir(): if database.is_dir():
database = (database / DEFAULT_DATABASE_NAME).with_suffix(suffix) database = database / DEFAULT_DATABASE_NAME
elif not database.parent.exists(): elif not database.parent.exists():
database.parent.mkdir(parents=True, exist_ok=True) database.parent.mkdir(parents=True, exist_ok=True)
else:
# Assume it's a filepath - existing or not.
pass
if database.suffix.lower() not in (".sqlite", ".db"):
database = database.with_suffix(database.suffix + ".sqlite")
database = str(database)
elif self.sql_type == "mysql":
database = self.database
else: else:
# Assume it's a filepath - existing or not. assert False
pass
self.schema = ifcopenshell.schema_by_name(self.file.schema_identifier) self.schema = ifcopenshell.schema_by_name(self.file.schema_identifier)
if self.sql_type == "sqlite": if self.sql_type == "sqlite":
self.db = sqlite3.connect(database) self.db = sqlite3.connect(database)
self.c = self.db.cursor() self.c = self.db.cursor()
self.file_patched = database
elif self.sql_type == "mysql": elif self.sql_type == "mysql":
self.db = mysql.connector.connect( self.db = mysql.connector.connect(
host=self.host, user=self.username, password=self.password, database=str(database) host=self.host, user=self.username, password=self.password, database=database
) )
self.c = self.db.cursor() self.c = self.db.cursor()
self.file_patched = None
else: else:
assert False assert False