2015-04-29 06:42:28 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2015-04-29 06:42:28 -04:00
|
|
|
#
|
|
|
|
#
|
2020-02-27 06:53:40 -05:00
|
|
|
import abc
|
2022-01-25 09:14:46 -05:00
|
|
|
from enum import IntEnum
|
2022-05-16 19:34:38 -04:00
|
|
|
from typing import TYPE_CHECKING, Any, Generic, Mapping, Optional, TypeVar
|
2020-02-27 06:53:40 -05:00
|
|
|
|
2022-05-16 19:34:38 -04:00
|
|
|
from synapse.storage.types import Connection, Cursor, DBAPI2Module
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.storage.database import LoggingDatabaseConnection
|
2015-04-29 06:42:28 -04:00
|
|
|
|
|
|
|
|
2022-01-25 09:14:46 -05:00
|
|
|
class IsolationLevel(IntEnum):
|
|
|
|
READ_COMMITTED: int = 1
|
|
|
|
REPEATABLE_READ: int = 2
|
|
|
|
SERIALIZABLE: int = 3
|
|
|
|
|
|
|
|
|
2015-04-29 06:42:28 -04:00
|
|
|
class IncorrectDatabaseSetup(RuntimeError):
|
|
|
|
pass
|
2020-02-27 06:53:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
ConnectionType = TypeVar("ConnectionType", bound=Connection)
|
2022-09-26 13:28:32 -04:00
|
|
|
CursorType = TypeVar("CursorType", bound=Cursor)
|
2020-02-27 06:53:40 -05:00
|
|
|
|
|
|
|
|
2022-09-26 13:28:32 -04:00
|
|
|
class BaseDatabaseEngine(Generic[ConnectionType, CursorType], metaclass=abc.ABCMeta):
|
2022-05-16 19:34:38 -04:00
|
|
|
def __init__(self, module: DBAPI2Module, config: Mapping[str, Any]):
|
2020-02-27 06:53:40 -05:00
|
|
|
self.module = module
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def single_threaded(self) -> bool:
|
|
|
|
...
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def supports_using_any_list(self) -> bool:
|
|
|
|
"""
|
|
|
|
Do we support using `a = ANY(?)` and passing a list
|
|
|
|
"""
|
|
|
|
...
|
|
|
|
|
2021-06-30 07:07:16 -04:00
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def supports_returning(self) -> bool:
|
|
|
|
"""Do we support the `RETURNING` clause in insert/update/delete?"""
|
|
|
|
...
|
|
|
|
|
2020-02-27 06:53:40 -05:00
|
|
|
@abc.abstractmethod
|
|
|
|
def check_database(
|
|
|
|
self, db_conn: ConnectionType, allow_outdated_version: bool = False
|
|
|
|
) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2022-09-26 13:28:32 -04:00
|
|
|
def check_new_database(self, txn: CursorType) -> None:
|
2020-02-27 06:53:40 -05:00
|
|
|
"""Gets called when setting up a brand new database. This allows us to
|
|
|
|
apply stricter checks on new databases versus existing database.
|
|
|
|
"""
|
|
|
|
...
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def convert_param_style(self, sql: str) -> str:
|
|
|
|
...
|
|
|
|
|
2022-05-16 19:34:38 -04:00
|
|
|
# This method would ideally take a plain ConnectionType, but it seems that
|
|
|
|
# the Sqlite engine expects to use LoggingDatabaseConnection.cursor
|
|
|
|
# instead of sqlite3.Connection.cursor: only the former takes a txn_name.
|
2020-02-27 06:53:40 -05:00
|
|
|
@abc.abstractmethod
|
2022-05-16 19:34:38 -04:00
|
|
|
def on_new_connection(self, db_conn: "LoggingDatabaseConnection") -> None:
|
2020-02-27 06:53:40 -05:00
|
|
|
...
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_deadlock(self, error: Exception) -> bool:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_connection_closed(self, conn: ConnectionType) -> bool:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2022-05-16 19:34:38 -04:00
|
|
|
def lock_table(self, txn: Cursor, table: str) -> None:
|
2020-02-27 06:53:40 -05:00
|
|
|
...
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def server_version(self) -> str:
|
|
|
|
"""Gets a string giving the server version. For example: '3.22.0'"""
|
|
|
|
...
|
2020-10-07 10:15:57 -04:00
|
|
|
|
2023-09-06 03:30:53 -04:00
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def row_id_name(self) -> str:
|
|
|
|
"""Gets the literal name representing a row id for this engine."""
|
|
|
|
...
|
|
|
|
|
2020-10-07 10:15:57 -04:00
|
|
|
@abc.abstractmethod
|
2022-05-16 19:34:38 -04:00
|
|
|
def in_transaction(self, conn: ConnectionType) -> bool:
|
2020-10-07 10:15:57 -04:00
|
|
|
"""Whether the connection is currently in a transaction."""
|
|
|
|
...
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2022-05-16 19:34:38 -04:00
|
|
|
def attempt_to_set_autocommit(self, conn: ConnectionType, autocommit: bool) -> None:
|
2020-10-07 10:15:57 -04:00
|
|
|
"""Attempt to set the connections autocommit mode.
|
|
|
|
|
|
|
|
When True queries are run outside of transactions.
|
|
|
|
|
|
|
|
Note: This has no effect on SQLite3, so callers still need to
|
|
|
|
commit/rollback the connections.
|
|
|
|
"""
|
|
|
|
...
|
2022-01-25 09:14:46 -05:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def attempt_to_set_isolation_level(
|
2022-05-16 19:34:38 -04:00
|
|
|
self, conn: ConnectionType, isolation_level: Optional[int]
|
|
|
|
) -> None:
|
2022-01-25 09:14:46 -05:00
|
|
|
"""Attempt to set the connections isolation level.
|
|
|
|
|
|
|
|
Note: This has no effect on SQLite3, as transactions are SERIALIZABLE by default.
|
|
|
|
"""
|
|
|
|
...
|
2022-09-26 13:28:32 -04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abc.abstractmethod
|
|
|
|
def executescript(cursor: CursorType, script: str) -> None:
|
|
|
|
"""Execute a chunk of SQL containing multiple semicolon-delimited statements.
|
|
|
|
|
|
|
|
This is not provided by DBAPI2, and so needs engine-specific support.
|
2023-01-25 08:38:53 -05:00
|
|
|
|
2023-01-31 06:03:55 -05:00
|
|
|
Any ongoing transaction is committed before executing the script in its own
|
|
|
|
transaction. The script transaction is left open and it is the responsibility of
|
|
|
|
the caller to commit it.
|
2022-09-26 13:28:32 -04:00
|
|
|
"""
|
|
|
|
...
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def execute_script_file(cls, cursor: CursorType, filepath: str) -> None:
|
|
|
|
"""Execute a file containing multiple semicolon-delimited SQL statements.
|
|
|
|
|
|
|
|
This is not provided by DBAPI2, and so needs engine-specific support.
|
|
|
|
"""
|
2023-08-15 08:11:20 -04:00
|
|
|
with open(filepath) as f:
|
2022-09-26 13:28:32 -04:00
|
|
|
cls.executescript(cursor, f.read())
|