_dml_constructors.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # sql/_dml_constructors.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. from __future__ import annotations
  8. from typing import TYPE_CHECKING
  9. from .dml import Delete
  10. from .dml import Insert
  11. from .dml import Update
  12. if TYPE_CHECKING:
  13. from ._typing import _DMLTableArgument
  14. def insert(table: _DMLTableArgument) -> Insert:
  15. """Construct an :class:`_expression.Insert` object.
  16. E.g.::
  17. from sqlalchemy import insert
  18. stmt = insert(user_table).values(name="username", fullname="Full Username")
  19. Similar functionality is available via the
  20. :meth:`_expression.TableClause.insert` method on
  21. :class:`_schema.Table`.
  22. .. seealso::
  23. :ref:`tutorial_core_insert` - in the :ref:`unified_tutorial`
  24. :param table: :class:`_expression.TableClause`
  25. which is the subject of the
  26. insert.
  27. :param values: collection of values to be inserted; see
  28. :meth:`_expression.Insert.values`
  29. for a description of allowed formats here.
  30. Can be omitted entirely; a :class:`_expression.Insert` construct
  31. will also dynamically render the VALUES clause at execution time
  32. based on the parameters passed to :meth:`_engine.Connection.execute`.
  33. :param inline: if True, no attempt will be made to retrieve the
  34. SQL-generated default values to be provided within the statement;
  35. in particular,
  36. this allows SQL expressions to be rendered 'inline' within the
  37. statement without the need to pre-execute them beforehand; for
  38. backends that support "returning", this turns off the "implicit
  39. returning" feature for the statement.
  40. If both :paramref:`_expression.insert.values` and compile-time bind
  41. parameters are present, the compile-time bind parameters override the
  42. information specified within :paramref:`_expression.insert.values` on a
  43. per-key basis.
  44. The keys within :paramref:`_expression.Insert.values` can be either
  45. :class:`~sqlalchemy.schema.Column` objects or their string
  46. identifiers. Each key may reference one of:
  47. * a literal data value (i.e. string, number, etc.);
  48. * a Column object;
  49. * a SELECT statement.
  50. If a ``SELECT`` statement is specified which references this
  51. ``INSERT`` statement's table, the statement will be correlated
  52. against the ``INSERT`` statement.
  53. .. seealso::
  54. :ref:`tutorial_core_insert` - in the :ref:`unified_tutorial`
  55. """ # noqa: E501
  56. return Insert(table)
  57. def update(table: _DMLTableArgument) -> Update:
  58. r"""Construct an :class:`_expression.Update` object.
  59. E.g.::
  60. from sqlalchemy import update
  61. stmt = (
  62. update(user_table).where(user_table.c.id == 5).values(name="user #5")
  63. )
  64. Similar functionality is available via the
  65. :meth:`_expression.TableClause.update` method on
  66. :class:`_schema.Table`.
  67. :param table: A :class:`_schema.Table`
  68. object representing the database
  69. table to be updated.
  70. .. seealso::
  71. :ref:`tutorial_core_update_delete` - in the :ref:`unified_tutorial`
  72. """ # noqa: E501
  73. return Update(table)
  74. def delete(table: _DMLTableArgument) -> Delete:
  75. r"""Construct :class:`_expression.Delete` object.
  76. E.g.::
  77. from sqlalchemy import delete
  78. stmt = delete(user_table).where(user_table.c.id == 5)
  79. Similar functionality is available via the
  80. :meth:`_expression.TableClause.delete` method on
  81. :class:`_schema.Table`.
  82. :param table: The table to delete rows from.
  83. .. seealso::
  84. :ref:`tutorial_core_update_delete` - in the :ref:`unified_tutorial`
  85. """
  86. return Delete(table)