| 68 | |
| 69 | |
| 70 | class sqlite(file): |
| 71 | mvd_str: str |
| 72 | """As in `header.file_description.description`.""" |
| 73 | |
| 74 | def __init__(self, filepath: str): |
| 75 | """ |
| 76 | Open existing sqlite IFC database. |
| 77 | |
| 78 | To create a new database from IFC file consider using Ifc2Sql IfcPatch: |
| 79 | |
| 80 | https://docs.ifcopenshell.org/autoapi/ifcpatch/recipes/Ifc2Sql/index.html |
| 81 | |
| 82 | :param filepath: Path to sqlite database. |
| 83 | """ |
| 84 | |
| 85 | if not Path(filepath).exists(): |
| 86 | raise FileNotFoundError(f"File doesn't exist: {filepath}") |
| 87 | |
| 88 | self.history_size = 64 |
| 89 | self.history = [] |
| 90 | self.future = [] |
| 91 | self.transaction = None |
| 92 | |
| 93 | self.filepath = filepath |
| 94 | self.db = sqlite3.connect(self.filepath) |
| 95 | self.db.row_factory = sqlite3.Row |
| 96 | |
| 97 | # import mysql.connector |
| 98 | # self.db = mysql.connector.connect( |
| 99 | # host="localhost", |
| 100 | # user="root", |
| 101 | # password="root", |
| 102 | # database="test" |
| 103 | # ) |
| 104 | |
| 105 | self.cursor = self.db.cursor() |
| 106 | |
| 107 | try: |
| 108 | self.cursor.execute("SELECT preprocessor, schema, mvd FROM metadata LIMIT 1") |
| 109 | row = self.cursor.fetchone() |
| 110 | if row[0] != "IfcOpenShell-1.0.0": |
| 111 | assert False, "SQLite schema not supported." |
| 112 | except: |
| 113 | assert False, "SQLite schema not supported." |
| 114 | |
| 115 | self._schema = row[1] |
| 116 | self.mvd_str = row[2] |
| 117 | self.ifc_schema = ifcopenshell.schema_by_name(self.schema) |
| 118 | |
| 119 | self.cursor.execute("SELECT ifc_id, ifc_class FROM id_map") |
| 120 | self.id_map: dict[int, str] = {} |
| 121 | self.class_map: dict[str, list[int]] = {} |
| 122 | self.entity_cache: dict[int, sqlite_entity] = {} |
| 123 | for row in self.cursor.fetchall(): |
| 124 | ifc_id, ifc_class = row |
| 125 | self.id_map[ifc_id] = ifc_class |
| 126 | self.class_map.setdefault(ifc_class, []).append(ifc_id) |
| 127 | |