if TYPE_CHECKING:
from sqlalchemy.engine.base import Connection
from sqlalchemy.engine.url import URL
- from sqlalchemy.sql.elements import ClauseElement
+ from sqlalchemy.sql import Executable
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.schema import FetchedValue
from sqlalchemy.sql.schema import MetaData
"""
def execute(
- sql: Union[ClauseElement, str], execution_options: Optional[dict] = None
+ sql: Union[Executable, str], execution_options: Optional[dict] = None
) -> None:
"""Execute the given SQL using the current change context.
from sqlalchemy.engine import Dialect
from sqlalchemy.engine.cursor import CursorResult
from sqlalchemy.engine.reflection import Inspector
- from sqlalchemy.sql.elements import ClauseElement
+ from sqlalchemy.sql import ClauseElement
+ from sqlalchemy.sql import Executable
from sqlalchemy.sql.elements import ColumnElement
from sqlalchemy.sql.elements import quoted_name
from sqlalchemy.sql.schema import Column
def _exec(
self,
- construct: Union[ClauseElement, str],
+ construct: Union[Executable, str],
execution_options: Optional[dict[str, Any]] = None,
multiparams: Sequence[dict] = (),
params: Dict[str, Any] = util.immutabledict(),
# TODO: coverage
raise Exception("Execution arguments not allowed with as_sql")
+ compile_kw: dict[str, Any]
if self.literal_binds and not isinstance(
construct, schema.DDLElement
):
else:
compile_kw = {}
- compiled = construct.compile(
- dialect=self.dialect, **compile_kw # type: ignore[arg-type]
- )
+ if TYPE_CHECKING:
+ assert isinstance(construct, ClauseElement)
+ compiled = construct.compile(dialect=self.dialect, **compile_kw)
self.static_output(
str(compiled).replace("\t", " ").strip()
+ self.command_terminator
assert isinstance(multiparams, tuple)
multiparams += (params,)
- return conn.execute( # type: ignore[call-overload]
- construct, multiparams
- )
+ return conn.execute(construct, multiparams)
def execute(
self,
- sql: Union[ClauseElement, str],
+ sql: Union[Executable, str],
execution_options: Optional[dict[str, Any]] = None,
) -> None:
self._exec(sql, execution_options)
"""
- compile_kw = {
- "compile_kwargs": {"literal_binds": True, "include_table": False}
- }
+ compile_kw = {"literal_binds": True, "include_table": False}
+
return str(
- expr.compile(
- dialect=self.dialect, **compile_kw # type: ignore[arg-type]
- )
+ expr.compile(dialect=self.dialect, compile_kwargs=compile_kw)
)
def _compat_autogen_column_reflect(self, inspector: Inspector) -> Callable:
from typing import TypeVar
from typing import Union
-from sqlalchemy.sql.expression import TableClause
-from sqlalchemy.sql.expression import Update
-
if TYPE_CHECKING:
from sqlalchemy.engine import Connection
+ from sqlalchemy.sql import Executable
from sqlalchemy.sql.elements import ColumnElement
from sqlalchemy.sql.elements import conv
from sqlalchemy.sql.elements import TextClause
+ from sqlalchemy.sql.expression import TableClause
from sqlalchemy.sql.functions import Function
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.schema import Computed
"""
def execute(
- sqltext: Union[str, TextClause, Update],
+ sqltext: Union[Executable, str],
*,
execution_options: Optional[dict[str, Any]] = None,
) -> None:
* a string
* a :func:`sqlalchemy.sql.expression.text` construct.
* a :func:`sqlalchemy.sql.expression.insert` construct.
- * a :func:`sqlalchemy.sql.expression.update`,
- :func:`sqlalchemy.sql.expression.insert`,
- or :func:`sqlalchemy.sql.expression.delete` construct.
+ * a :func:`sqlalchemy.sql.expression.update` construct.
+ * a :func:`sqlalchemy.sql.expression.delete` construct.
* Any "executable" described in SQLAlchemy Core documentation,
noting that no result set is returned.
from sqlalchemy import Table
from sqlalchemy.engine import Connection
+ from sqlalchemy.sql import Executable
from sqlalchemy.sql.expression import ColumnElement
from sqlalchemy.sql.expression import TableClause
from sqlalchemy.sql.expression import TextClause
- from sqlalchemy.sql.expression import Update
from sqlalchemy.sql.functions import Function
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.schema import Computed
def execute(
self,
- sqltext: Union[str, TextClause, Update],
+ sqltext: Union[Executable, str],
*,
execution_options: Optional[dict[str, Any]] = None,
) -> None:
* a string
* a :func:`sqlalchemy.sql.expression.text` construct.
* a :func:`sqlalchemy.sql.expression.insert` construct.
- * a :func:`sqlalchemy.sql.expression.update`,
- :func:`sqlalchemy.sql.expression.insert`,
- or :func:`sqlalchemy.sql.expression.delete` construct.
+ * a :func:`sqlalchemy.sql.expression.update` construct.
+ * a :func:`sqlalchemy.sql.expression.delete` construct.
* Any "executable" described in SQLAlchemy Core documentation,
noting that no result set is returned.
def execute(
self,
- sqltext: Union[str, TextClause, Update],
+ sqltext: Union[Executable, str],
*,
execution_options: Optional[dict[str, Any]] = None,
) -> None:
if TYPE_CHECKING:
from typing import Literal
- from sqlalchemy.sql.dml import Insert
- from sqlalchemy.sql.dml import Update
+ from sqlalchemy.sql import Executable
from sqlalchemy.sql.elements import ColumnElement
from sqlalchemy.sql.elements import conv
from sqlalchemy.sql.elements import quoted_name
def __init__(
self,
- sqltext: Union[Update, str, Insert, TextClause],
+ sqltext: Union[Executable, str],
*,
execution_options: Optional[dict[str, Any]] = None,
) -> None:
def execute(
cls,
operations: Operations,
- sqltext: Union[str, TextClause, Update],
+ sqltext: Union[Executable, str],
*,
execution_options: Optional[dict[str, Any]] = None,
) -> None:
* a string
* a :func:`sqlalchemy.sql.expression.text` construct.
* a :func:`sqlalchemy.sql.expression.insert` construct.
- * a :func:`sqlalchemy.sql.expression.update`,
- :func:`sqlalchemy.sql.expression.insert`,
- or :func:`sqlalchemy.sql.expression.delete` construct.
+ * a :func:`sqlalchemy.sql.expression.update` construct.
+ * a :func:`sqlalchemy.sql.expression.delete` construct.
* Any "executable" described in SQLAlchemy Core documentation,
noting that no result set is returned.
def batch_execute(
cls,
operations: Operations,
- sqltext: Union[str, TextClause, Update],
+ sqltext: Union[Executable, str],
*,
execution_options: Optional[dict[str, Any]] = None,
) -> None:
if TYPE_CHECKING:
from sqlalchemy.engine import URL
from sqlalchemy.engine.base import Connection
- from sqlalchemy.sql.elements import ClauseElement
+ from sqlalchemy.sql import Executable
from sqlalchemy.sql.schema import MetaData
from sqlalchemy.sql.schema import SchemaItem
from sqlalchemy.sql.type_api import TypeEngine
def execute(
self,
- sql: Union[ClauseElement, str],
+ sql: Union[Executable, str],
execution_options: Optional[dict] = None,
) -> None:
"""Execute the given SQL using the current change context.
from sqlalchemy.engine.base import Connection
from sqlalchemy.engine.base import Transaction
from sqlalchemy.engine.mock import MockConnection
- from sqlalchemy.sql.elements import ClauseElement
+ from sqlalchemy.sql import Executable
from .environment import EnvironmentContext
from ..config import Config
def execute(
self,
- sql: Union[ClauseElement, str],
+ sql: Union[Executable, str],
execution_options: Optional[dict] = None,
) -> None:
"""Execute a SQL construct or string statement.
--- /dev/null
+.. change::
+ :tags: typing
+ :tickets: 1058, 1277
+
+ Properly type ``op.execute`` method.
+ Pull request curtesy of Mihail Milushev.
"FOREIGN KEY(foo_bar) REFERENCES foo (bar))"
)
+ def test_execute_delete(self):
+ context = op_fixture()
+
+ account = table(
+ "account", column("name", String), column("id", Integer)
+ )
+ op.execute(account.delete().where(account.c.name == "account 1"))
+ context.assert_(
+ "DELETE FROM account WHERE account.name = :name_1",
+ )
+
+ def test_execute_insert(self):
+ context = op_fixture()
+
+ account = table(
+ "account", column("name", String), column("id", Integer)
+ )
+ op.execute(account.insert().values(name="account 1"))
+ context.assert_(
+ "INSERT INTO account (name) VALUES (:name)",
+ )
+
+ def test_execute_update(self):
+ context = op_fixture()
+
+ account = table(
+ "account", column("name", String), column("id", Integer)
+ )
+ op.execute(
+ account.update()
+ .where(account.c.name == "account 1")
+ .values({"name": "account 2"})
+ )
+ context.assert_(
+ "UPDATE account SET name=:name " "WHERE account.name = :name_1",
+ )
+
+ def test_execute_str(self):
+ context = op_fixture()
+
+ op.execute("SELECT 'test'")
+ context.assert_("SELECT 'test'")
+
+ def test_execute_textclause(self):
+ context = op_fixture()
+
+ op.execute(text("SELECT 'test'"))
+ context.assert_("SELECT 'test'")
+
def test_inline_literal(self):
context = op_fixture()
import sqlalchemy as sa
TRIM_MODULE = [
- "alembic.runtime.migration.",
+ "alembic.autogenerate.api.",
"alembic.operations.base.",
"alembic.operations.ops.",
- "alembic.autogenerate.api.",
+ "alembic.runtime.migration.",
"sqlalchemy.engine.base.",
"sqlalchemy.engine.url.",
+ "sqlalchemy.sql.base.",
+ "sqlalchemy.sql.dml.",
+ "sqlalchemy.sql.elements.",
+ "sqlalchemy.sql.functions.",
"sqlalchemy.sql.schema.",
"sqlalchemy.sql.selectable.",
- "sqlalchemy.sql.elements.",
"sqlalchemy.sql.type_api.",
- "sqlalchemy.sql.functions.",
- "sqlalchemy.sql.dml.",
"typing.",
]
ADDITIONAL_ENV = {