You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

150 lines
4.7 KiB

  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. CursorType = TypeVar("CursorType", bound=Cursor)
  28. class BaseDatabaseEngine(Generic[ConnectionType, CursorType], metaclass=abc.ABCMeta):
  29. def __init__(self, module: DBAPI2Module, config: Mapping[str, Any]):
  30. self.module = module
  31. @property
  32. @abc.abstractmethod
  33. def single_threaded(self) -> bool:
  34. ...
  35. @property
  36. @abc.abstractmethod
  37. def supports_using_any_list(self) -> bool:
  38. """
  39. Do we support using `a = ANY(?)` and passing a list
  40. """
  41. ...
  42. @property
  43. @abc.abstractmethod
  44. def supports_returning(self) -> bool:
  45. """Do we support the `RETURNING` clause in insert/update/delete?"""
  46. ...
  47. @abc.abstractmethod
  48. def check_database(
  49. self, db_conn: ConnectionType, allow_outdated_version: bool = False
  50. ) -> None:
  51. ...
  52. @abc.abstractmethod
  53. def check_new_database(self, txn: CursorType) -> None:
  54. """Gets called when setting up a brand new database. This allows us to
  55. apply stricter checks on new databases versus existing database.
  56. """
  57. ...
  58. @abc.abstractmethod
  59. def convert_param_style(self, sql: str) -> str:
  60. ...
  61. # This method would ideally take a plain ConnectionType, but it seems that
  62. # the Sqlite engine expects to use LoggingDatabaseConnection.cursor
  63. # instead of sqlite3.Connection.cursor: only the former takes a txn_name.
  64. @abc.abstractmethod
  65. def on_new_connection(self, db_conn: "LoggingDatabaseConnection") -> None:
  66. ...
  67. @abc.abstractmethod
  68. def is_deadlock(self, error: Exception) -> bool:
  69. ...
  70. @abc.abstractmethod
  71. def is_connection_closed(self, conn: ConnectionType) -> bool:
  72. ...
  73. @abc.abstractmethod
  74. def lock_table(self, txn: Cursor, table: str) -> None:
  75. ...
  76. @property
  77. @abc.abstractmethod
  78. def server_version(self) -> str:
  79. """Gets a string giving the server version. For example: '3.22.0'"""
  80. ...
  81. @abc.abstractmethod
  82. def in_transaction(self, conn: ConnectionType) -> bool:
  83. """Whether the connection is currently in a transaction."""
  84. ...
  85. @abc.abstractmethod
  86. def attempt_to_set_autocommit(self, conn: ConnectionType, autocommit: bool) -> None:
  87. """Attempt to set the connections autocommit mode.
  88. When True queries are run outside of transactions.
  89. Note: This has no effect on SQLite3, so callers still need to
  90. commit/rollback the connections.
  91. """
  92. ...
  93. @abc.abstractmethod
  94. def attempt_to_set_isolation_level(
  95. self, conn: ConnectionType, isolation_level: Optional[int]
  96. ) -> None:
  97. """Attempt to set the connections isolation level.
  98. Note: This has no effect on SQLite3, as transactions are SERIALIZABLE by default.
  99. """
  100. ...
  101. @staticmethod
  102. @abc.abstractmethod
  103. def executescript(cursor: CursorType, script: str) -> None:
  104. """Execute a chunk of SQL containing multiple semicolon-delimited statements.
  105. This is not provided by DBAPI2, and so needs engine-specific support.
  106. Any ongoing transaction is committed before executing the script in its own
  107. transaction. The script transaction is left open and it is the responsibility of
  108. the caller to commit it.
  109. """
  110. ...
  111. @classmethod
  112. def execute_script_file(cls, cursor: CursorType, filepath: str) -> None:
  113. """Execute a file containing multiple semicolon-delimited SQL statements.
  114. This is not provided by DBAPI2, and so needs engine-specific support.
  115. """
  116. with open(filepath) as f:
  117. cls.executescript(cursor, f.read())