From a6d8ba93056c1a4e243d56da447e241b2504fae2 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 23:20:37 +0300 Subject: move files again --- include/py/homekit/database/sqlite.py | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 include/py/homekit/database/sqlite.py (limited to 'include/py/homekit/database/sqlite.py') diff --git a/include/py/homekit/database/sqlite.py b/include/py/homekit/database/sqlite.py new file mode 100644 index 0000000..0af1f54 --- /dev/null +++ b/include/py/homekit/database/sqlite.py @@ -0,0 +1,67 @@ +import sqlite3 +import os.path +import logging + +from ._base import get_data_root_directory +from ..config import config, is_development_mode + + +def _get_database_path(name: str) -> str: + return os.path.join( + get_data_root_directory(), + f'{name}.db') + + +class SQLiteBase: + SCHEMA = 1 + + def __init__(self, name=None, check_same_thread=False): + if name is None: + name = config.app_config['database_name'] + database_path = _get_database_path(name) + if not os.path.exists(os.path.dirname(database_path)): + os.makedirs(os.path.dirname(database_path)) + + self.logger = logging.getLogger(self.__class__.__name__) + self.sqlite = sqlite3.connect(database_path, check_same_thread=check_same_thread) + + if is_development_mode(): + self.sql_logger = logging.getLogger(self.__class__.__name__) + self.sql_logger.setLevel('TRACE') + self.sqlite.set_trace_callback(self.sql_logger.trace) + + sqlite_version = self._get_sqlite_version() + self.logger.debug(f'SQLite version: {sqlite_version}') + + schema_version = self.schema_get_version() + self.logger.debug(f'Schema version: {schema_version}') + + self.schema_init(schema_version) + self.schema_set_version(self.SCHEMA) + + def __del__(self): + if self.sqlite: + self.sqlite.commit() + self.sqlite.close() + + def _get_sqlite_version(self) -> str: + cursor = self.sqlite.cursor() + cursor.execute("SELECT sqlite_version()") + return cursor.fetchone()[0] + + def schema_get_version(self) -> int: + cursor = self.sqlite.execute('PRAGMA user_version') + return int(cursor.fetchone()[0]) + + def schema_set_version(self, v) -> None: + self.sqlite.execute('PRAGMA user_version={:d}'.format(v)) + self.logger.info(f'Schema set to {v}') + + def cursor(self) -> sqlite3.Cursor: + return self.sqlite.cursor() + + def commit(self) -> None: + return self.sqlite.commit() + + def schema_init(self, version: int) -> None: + raise ValueError(f'{self.__class__.__name__}: must override schema_init') -- cgit v1.2.3 From 62ee71fdb0eb07adbf0071103617aa96c993fe22 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 11 Jun 2023 05:03:43 +0300 Subject: ipcam: start porting to new config and multiserver scheme --- include/py/homekit/database/sqlite.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'include/py/homekit/database/sqlite.py') diff --git a/include/py/homekit/database/sqlite.py b/include/py/homekit/database/sqlite.py index 0af1f54..8b0c44c 100644 --- a/include/py/homekit/database/sqlite.py +++ b/include/py/homekit/database/sqlite.py @@ -15,10 +15,13 @@ def _get_database_path(name: str) -> str: class SQLiteBase: SCHEMA = 1 - def __init__(self, name=None, check_same_thread=False): - if name is None: - name = config.app_config['database_name'] - database_path = _get_database_path(name) + def __init__(self, name=None, path=None, check_same_thread=False): + if not path: + if not name: + name = config.app_config['database_name'] + database_path = _get_database_path(name) + else: + database_path = path if not os.path.exists(os.path.dirname(database_path)): os.makedirs(os.path.dirname(database_path)) -- cgit v1.2.3 From a32e4a1629a20026c364059c7bbaec1dbd64353b Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 17 Sep 2023 04:38:12 +0300 Subject: multiple fixes --- include/py/homekit/database/sqlite.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/py/homekit/database/sqlite.py') diff --git a/include/py/homekit/database/sqlite.py b/include/py/homekit/database/sqlite.py index 8b0c44c..1651a93 100644 --- a/include/py/homekit/database/sqlite.py +++ b/include/py/homekit/database/sqlite.py @@ -18,7 +18,7 @@ class SQLiteBase: def __init__(self, name=None, path=None, check_same_thread=False): if not path: if not name: - name = config.app_config['database_name'] + name = config.app_name database_path = _get_database_path(name) else: database_path = path -- cgit v1.2.3