|
| 1 | +import asyncio |
| 2 | +import enum |
| 3 | +import uuid |
| 4 | +import warnings |
| 5 | +from abc import ABC, abstractmethod |
| 6 | + |
| 7 | +import psycopg2 |
| 8 | +from aiopg.utils import PY_35, _TransactionPointContextManager |
| 9 | + |
| 10 | +__all__ = ('IsolationLevel', 'Transaction') |
| 11 | + |
| 12 | + |
| 13 | +class IsolationCompiler(ABC): |
| 14 | + name = '' |
| 15 | + |
| 16 | + __slots__ = ('_readonly', '_deferrable') |
| 17 | + |
| 18 | + def __init__(self, readonly, deferrable): |
| 19 | + self._readonly = readonly |
| 20 | + self._deferrable = deferrable |
| 21 | + self._check_readonly_deferrable() |
| 22 | + |
| 23 | + def _check_readonly_deferrable(self): |
| 24 | + available = self._readonly or self._deferrable |
| 25 | + if not isinstance(self, SerializableCompiler) and available: |
| 26 | + raise ValueError('Is only available for serializable transactions') |
| 27 | + |
| 28 | + def savepoint(self, unique_id): |
| 29 | + return 'SAVEPOINT {}'.format(unique_id) |
| 30 | + |
| 31 | + def release_savepoint(self, unique_id): |
| 32 | + return 'RELEASE SAVEPOINT {}'.format(unique_id) |
| 33 | + |
| 34 | + def rollback_savepoint(self, unique_id): |
| 35 | + return 'ROLLBACK TO SAVEPOINT {}'.format(unique_id) |
| 36 | + |
| 37 | + def commit(self): |
| 38 | + return 'COMMIT' |
| 39 | + |
| 40 | + def rollback(self): |
| 41 | + return 'ROLLBACK' |
| 42 | + |
| 43 | + @abstractmethod |
| 44 | + def begin(self): |
| 45 | + raise NotImplementedError("Please Implement this method") |
| 46 | + |
| 47 | + def __repr__(self): |
| 48 | + return self.name |
| 49 | + |
| 50 | + |
| 51 | +class ReadCommittedCompiler(IsolationCompiler): |
| 52 | + name = 'Read committed' |
| 53 | + |
| 54 | + def begin(self): |
| 55 | + return 'BEGIN' |
| 56 | + |
| 57 | + |
| 58 | +class RepeatableReadCompiler(IsolationCompiler): |
| 59 | + name = 'Repeatable read' |
| 60 | + |
| 61 | + def begin(self): |
| 62 | + return 'BEGIN ISOLATION LEVEL REPEATABLE READ' |
| 63 | + |
| 64 | + |
| 65 | +class SerializableCompiler(IsolationCompiler): |
| 66 | + name = 'Serializable' |
| 67 | + |
| 68 | + def begin(self): |
| 69 | + query = 'BEGIN ISOLATION LEVEL SERIALIZABLE' |
| 70 | + |
| 71 | + if self._readonly: |
| 72 | + query += ' READ ONLY' |
| 73 | + |
| 74 | + if self._deferrable: |
| 75 | + query += ' DEFERRABLE' |
| 76 | + |
| 77 | + return query |
| 78 | + |
| 79 | + |
| 80 | +class IsolationLevel(enum.Enum): |
| 81 | + serializable = SerializableCompiler |
| 82 | + repeatable_read = RepeatableReadCompiler |
| 83 | + read_committed = ReadCommittedCompiler |
| 84 | + |
| 85 | + def __call__(self, readonly, deferrable): |
| 86 | + return self.value(readonly, deferrable) |
| 87 | + |
| 88 | + |
| 89 | +class Transaction: |
| 90 | + __slots__ = ('_cur', '_is_begin', '_isolation', '_unique_id') |
| 91 | + |
| 92 | + def __init__(self, cur, isolation_level, |
| 93 | + readonly=False, deferrable=False): |
| 94 | + self._cur = cur |
| 95 | + self._is_begin = False |
| 96 | + self._unique_id = None |
| 97 | + self._isolation = isolation_level(readonly, deferrable) |
| 98 | + |
| 99 | + @property |
| 100 | + def is_begin(self): |
| 101 | + return self._is_begin |
| 102 | + |
| 103 | + @asyncio.coroutine |
| 104 | + def begin(self): |
| 105 | + if self._is_begin: |
| 106 | + raise psycopg2.ProgrammingError( |
| 107 | + 'You are trying to open a new transaction, use the save point') |
| 108 | + self._is_begin = True |
| 109 | + yield from self._cur.execute(self._isolation.begin()) |
| 110 | + return self |
| 111 | + |
| 112 | + @asyncio.coroutine |
| 113 | + def commit(self): |
| 114 | + self._check_commit_rollback() |
| 115 | + yield from self._cur.execute(self._isolation.commit()) |
| 116 | + self._is_begin = False |
| 117 | + |
| 118 | + @asyncio.coroutine |
| 119 | + def rollback(self): |
| 120 | + self._check_commit_rollback() |
| 121 | + yield from self._cur.execute(self._isolation.rollback()) |
| 122 | + self._is_begin = False |
| 123 | + |
| 124 | + @asyncio.coroutine |
| 125 | + def rollback_savepoint(self): |
| 126 | + self._check_release_rollback() |
| 127 | + yield from self._cur.execute( |
| 128 | + self._isolation.rollback_savepoint(self._unique_id)) |
| 129 | + self._unique_id = None |
| 130 | + |
| 131 | + @asyncio.coroutine |
| 132 | + def release_savepoint(self): |
| 133 | + self._check_release_rollback() |
| 134 | + yield from self._cur.execute( |
| 135 | + self._isolation.release_savepoint(self._unique_id)) |
| 136 | + self._unique_id = None |
| 137 | + |
| 138 | + @asyncio.coroutine |
| 139 | + def savepoint(self): |
| 140 | + self._check_commit_rollback() |
| 141 | + if self._unique_id is not None: |
| 142 | + raise psycopg2.ProgrammingError('You do not shut down savepoint') |
| 143 | + |
| 144 | + self._unique_id = 's{}'.format(uuid.uuid1().hex) |
| 145 | + yield from self._cur.execute( |
| 146 | + self._isolation.savepoint(self._unique_id)) |
| 147 | + |
| 148 | + return self |
| 149 | + |
| 150 | + def point(self): |
| 151 | + return _TransactionPointContextManager(self.savepoint()) |
| 152 | + |
| 153 | + def _check_commit_rollback(self): |
| 154 | + if not self._is_begin: |
| 155 | + raise psycopg2.ProgrammingError('You are trying to commit ' |
| 156 | + 'the transaction does not open') |
| 157 | + |
| 158 | + def _check_release_rollback(self): |
| 159 | + self._check_commit_rollback() |
| 160 | + if self._unique_id is None: |
| 161 | + raise psycopg2.ProgrammingError('You do not start savepoint') |
| 162 | + |
| 163 | + def __repr__(self): |
| 164 | + return "<{} transaction={} id={:#x}>".format( |
| 165 | + self.__class__.__name__, |
| 166 | + self._isolation, |
| 167 | + id(self) |
| 168 | + ) |
| 169 | + |
| 170 | + def __del__(self): |
| 171 | + if self._is_begin: |
| 172 | + warnings.warn( |
| 173 | + "You have not closed transaction {!r}".format(self), |
| 174 | + ResourceWarning) |
| 175 | + |
| 176 | + if self._unique_id is not None: |
| 177 | + warnings.warn( |
| 178 | + "You have not closed savepoint {!r}".format(self), |
| 179 | + ResourceWarning) |
| 180 | + |
| 181 | + if PY_35: |
| 182 | + @asyncio.coroutine |
| 183 | + def __aenter__(self): |
| 184 | + return (yield from self.begin()) |
| 185 | + |
| 186 | + @asyncio.coroutine |
| 187 | + def __aexit__(self, exc_type, exc, tb): |
| 188 | + if exc_type is not None: |
| 189 | + yield from self.rollback() |
| 190 | + else: |
| 191 | + yield from self.commit() |
0 commit comments