diff options
| author | Daniel Black <daniel@mariadb.org> | 2021-09-28 14:20:06 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-06-02 12:51:20 -0400 |
| commit | 466ed5b53a3af83f337c93be95715e4b3ab1255e (patch) | |
| tree | 73564b3a1d08e6b8add40c66a600625dd5f733fa /lib/sqlalchemy/engine/default.py | |
| parent | 7b6fb299bb6b47dfeb22a5650b95af7fa0b35ec2 (diff) | |
| download | sqlalchemy-466ed5b53a3af83f337c93be95715e4b3ab1255e.tar.gz | |
Generalize RETURNING and suppor for MariaDB / SQLite
As almost every dialect supports RETURNING now, RETURNING
is also made more of a default assumption.
* the default compiler generates a RETURNING clause now
when specified; CompileError is no longer raised.
* The dialect-level implicit_returning parameter now has
no effect. It's not fully clear if there are real world
cases relying on the dialect-level parameter, so we will see
once 2.0 is released. ORM-level RETURNING can be disabled
at the table level, and perhaps "implicit returning" should
become an ORM-level option at some point as that's where
it applies.
* Altered ORM update() / delete() to respect table-level
implicit returning for fetch.
* Since MariaDB doesnt support UPDATE returning, "full_returning"
is now split into insert_returning, update_returning, delete_returning
* Crazy new thing. Dialects that have *both* cursor.lastrowid
*and* returning. so now we can pick between them for SQLite
and mariadb. so, we are trying to keep it on .lastrowid for
simple inserts with an autoincrement column, this helps with
some edge case test scenarios and i bet .lastrowid is faster
anyway. any return_defaults() / multiparams etc then we
use returning
* SQLite decided they dont want to return rows that match in
ON CONFLICT. this is flat out wrong, but for now we need to
work with it.
Fixes: #6195
Fixes: #7011
Closes: #7047
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7047
Pull-request-sha: d25d5ea3abe094f282c53c7dd87f5f53a9e85248
Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Change-Id: I9908ce0ff7bdc50bd5b27722081767c31c19a950
Diffstat (limited to 'lib/sqlalchemy/engine/default.py')
| -rw-r--r-- | lib/sqlalchemy/engine/default.py | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index bcbe83f3f..6b76601ff 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -57,6 +57,7 @@ from ..sql.compiler import DDLCompiler from ..sql.compiler import SQLCompiler from ..sql.elements import quoted_name from ..sql.schema import default_is_scalar +from ..util.typing import Literal if typing.TYPE_CHECKING: from types import ModuleType @@ -135,9 +136,11 @@ class DefaultDialect(Dialect): preexecute_autoincrement_sequences = False supports_identity_columns = False postfetch_lastrowid = True + favor_returning_over_lastrowid = False insert_null_pk_still_autoincrements = False - implicit_returning = False - full_returning = False + update_returning = False + delete_returning = False + insert_returning = False insert_executemany_returning = False cte_follows_insert = False @@ -258,7 +261,7 @@ class DefaultDialect(Dialect): paramstyle: Optional[_ParamStyle] = None, isolation_level: Optional[_IsolationLevel] = None, dbapi: Optional[ModuleType] = None, - implicit_returning: Optional[bool] = None, + implicit_returning: Literal[True] = True, supports_native_boolean: Optional[bool] = None, max_identifier_length: Optional[int] = None, label_length: Optional[int] = None, @@ -296,8 +299,6 @@ class DefaultDialect(Dialect): self.paramstyle = self.dbapi.paramstyle else: self.paramstyle = self.default_paramstyle - if implicit_returning is not None: - self.implicit_returning = implicit_returning self.positional = self.paramstyle in ("qmark", "format", "numeric") self.identifier_preparer = self.preparer(self) self._on_connect_isolation_level = isolation_level @@ -324,6 +325,18 @@ class DefaultDialect(Dialect): self.label_length = label_length self.compiler_linting = compiler_linting + @util.deprecated_property( + "2.0", + "full_returning is deprecated, please use insert_returning, " + "update_returning, delete_returning", + ) + def full_returning(self): + return ( + self.insert_returning + and self.update_returning + and self.delete_returning + ) + @util.memoized_property def loaded_dbapi(self) -> ModuleType: if self.dbapi is None: @@ -771,7 +784,6 @@ class StrCompileDialect(DefaultDialect): supports_sequences = True sequences_optional = True preexecute_autoincrement_sequences = False - implicit_returning = False supports_native_boolean = True @@ -806,6 +818,8 @@ class DefaultExecutionContext(ExecutionContext): _soft_closed = False + _has_rowcount = False + # a hook for SQLite's translation of # result column names # NOTE: pyhive is using this hook, can't remove it :( @@ -1450,6 +1464,7 @@ class DefaultExecutionContext(ExecutionContext): # is testing this, and psycopg will no longer return # rowcount after cursor is closed. result.rowcount + self._has_rowcount = True row = result.fetchone() if row is not None: @@ -1465,7 +1480,12 @@ class DefaultExecutionContext(ExecutionContext): # no results, get rowcount # (which requires open cursor on some drivers) result.rowcount + self._has_rowcount = True result._soft_close() + elif self.isupdate or self.isdelete: + result.rowcount + self._has_rowcount = True + return result @util.memoized_property @@ -1479,7 +1499,6 @@ class DefaultExecutionContext(ExecutionContext): getter = cast( SQLCompiler, self.compiled )._inserted_primary_key_from_lastrowid_getter - lastrowid = self.get_lastrowid() return [getter(lastrowid, self.compiled_parameters[0])] |
