_base.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import abc
  15. from enum import IntEnum
  16. from typing import TYPE_CHECKING, Any, Generic, Mapping, Optional, TypeVar
  17. from synapse.storage.types import Connection, Cursor, DBAPI2Module
  18. if TYPE_CHECKING:
  19. from synapse.storage.database import LoggingDatabaseConnection
  20. class IsolationLevel(IntEnum):
  21. READ_COMMITTED: int = 1
  22. REPEATABLE_READ: int = 2
  23. SERIALIZABLE: int = 3
  24. class IncorrectDatabaseSetup(RuntimeError):
  25. pass
  26. ConnectionType = TypeVar("ConnectionType", bound=Connection)
  27. class BaseDatabaseEngine(Generic[ConnectionType], metaclass=abc.ABCMeta):
  28. def __init__(self, module: DBAPI2Module, config: Mapping[str, Any]):
  29. self.module = module
  30. @property
  31. @abc.abstractmethod
  32. def single_threaded(self) -> bool:
  33. ...
  34. @property
  35. @abc.abstractmethod
  36. def supports_using_any_list(self) -> bool:
  37. """
  38. Do we support using `a = ANY(?)` and passing a list
  39. """
  40. ...
  41. @property
  42. @abc.abstractmethod
  43. def supports_returning(self) -> bool:
  44. """Do we support the `RETURNING` clause in insert/update/delete?"""
  45. ...
  46. @abc.abstractmethod
  47. def check_database(
  48. self, db_conn: ConnectionType, allow_outdated_version: bool = False
  49. ) -> None:
  50. ...
  51. @abc.abstractmethod
  52. def check_new_database(self, txn: Cursor) -> None:
  53. """Gets called when setting up a brand new database. This allows us to
  54. apply stricter checks on new databases versus existing database.
  55. """
  56. ...
  57. @abc.abstractmethod
  58. def convert_param_style(self, sql: str) -> str:
  59. ...
  60. # This method would ideally take a plain ConnectionType, but it seems that
  61. # the Sqlite engine expects to use LoggingDatabaseConnection.cursor
  62. # instead of sqlite3.Connection.cursor: only the former takes a txn_name.
  63. @abc.abstractmethod
  64. def on_new_connection(self, db_conn: "LoggingDatabaseConnection") -> None:
  65. ...
  66. @abc.abstractmethod
  67. def is_deadlock(self, error: Exception) -> bool:
  68. ...
  69. @abc.abstractmethod
  70. def is_connection_closed(self, conn: ConnectionType) -> bool:
  71. ...
  72. @abc.abstractmethod
  73. def lock_table(self, txn: Cursor, table: str) -> None:
  74. ...
  75. @property
  76. @abc.abstractmethod
  77. def server_version(self) -> str:
  78. """Gets a string giving the server version. For example: '3.22.0'"""
  79. ...
  80. @abc.abstractmethod
  81. def in_transaction(self, conn: ConnectionType) -> bool:
  82. """Whether the connection is currently in a transaction."""
  83. ...
  84. @abc.abstractmethod
  85. def attempt_to_set_autocommit(self, conn: ConnectionType, autocommit: bool) -> None:
  86. """Attempt to set the connections autocommit mode.
  87. When True queries are run outside of transactions.
  88. Note: This has no effect on SQLite3, so callers still need to
  89. commit/rollback the connections.
  90. """
  91. ...
  92. @abc.abstractmethod
  93. def attempt_to_set_isolation_level(
  94. self, conn: ConnectionType, isolation_level: Optional[int]
  95. ) -> None:
  96. """Attempt to set the connections isolation level.
  97. Note: This has no effect on SQLite3, as transactions are SERIALIZABLE by default.
  98. """
  99. ...