|
| 1 | +# Copyright (c) 2025-Present MatrixEditor |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all |
| 11 | +# copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | +import sqlite3 |
| 21 | +from sqlalchemy import Engine, create_engine |
| 22 | + |
| 23 | +from dementor.config.session import SessionConfig |
| 24 | +from dementor.db.model import DementorDB, ModelBase |
| 25 | +from dementor.log.logger import dm_logger |
| 26 | +from dementor.config.toml import TomlConfig, Attribute as A |
| 27 | + |
| 28 | + |
| 29 | +class DatabaseConfig(TomlConfig): |
| 30 | + _section_ = "DB" |
| 31 | + _fields_ = [ |
| 32 | + A("db_raw_path", "Url", None), |
| 33 | + A("db_path", "Path", "Dementor.db"), |
| 34 | + A("db_duplicate_creds", "DuplicateCreds", False), |
| 35 | + A("db_dialect", "Dialect", None), |
| 36 | + A("db_driver", "Driver", None), |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +def init_dementor_db(session: SessionConfig) -> Engine | None: |
| 41 | + engine = init_engine(session) |
| 42 | + if engine is not None: |
| 43 | + ModelBase.metadata.create_all(engine) |
| 44 | + return engine |
| 45 | + |
| 46 | + |
| 47 | +def init_engine(session: SessionConfig) -> Engine | None: |
| 48 | + # based on dialect and driver configuration |
| 49 | + raw_path = session.db_config.db_raw_path |
| 50 | + if raw_path is None: |
| 51 | + # fall back to constructing the path manually |
| 52 | + dialect = session.db_config.db_dialect or "sqlite" |
| 53 | + driver = session.db_config.db_driver or "pysqlite" |
| 54 | + path = session.db_config.db_path |
| 55 | + if not path: |
| 56 | + return dm_logger.error("Database path not specified!") |
| 57 | + |
| 58 | + if dialect == "sqlite": |
| 59 | + if path != ":memory:": |
| 60 | + path = f"/{session.resolve_path(path)}" |
| 61 | + |
| 62 | + # see https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls |
| 63 | + raw_path = f"{dialect}+{driver}://{path}" |
| 64 | + else: |
| 65 | + sql_type, path = raw_path.split("://") |
| 66 | + if sql_type.count("+") > 0: |
| 67 | + dialect, driver = sql_type.split("+") |
| 68 | + else: |
| 69 | + dialect = sql_type |
| 70 | + driver = "<default>" |
| 71 | + |
| 72 | + if dialect != "sqlite": |
| 73 | + first_element, *parts = path.split("/") |
| 74 | + if "@" in first_element: |
| 75 | + first_element = first_element.split("@")[1] |
| 76 | + path = "***:***@" + "/".join([first_element] + list(parts)) |
| 77 | + |
| 78 | + dm_logger.debug("Using database [%s:%s] at: %s", dialect, driver, path) |
| 79 | + return create_engine(raw_path, isolation_level="AUTOCOMMIT", future=True) |
| 80 | + |
| 81 | + |
| 82 | +def create_db(session: SessionConfig) -> DementorDB: |
| 83 | + # TODO: add support for custom database implementations |
| 84 | + engine = init_engine(session) |
| 85 | + if not engine: |
| 86 | + raise Exception("Failed to create database engine") |
| 87 | + return DementorDB(engine, session) |
0 commit comments