]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- add docs about not using text() with session.execute()
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 23 Jun 2011 21:30:07 +0000 (17:30 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 23 Jun 2011 21:30:07 +0000 (17:30 -0400)
- rewrite connection.execute() documentation.

lib/sqlalchemy/engine/base.py
lib/sqlalchemy/orm/session.py

index 04636b5e577916cd3a30cc2d3725e3632439d191..3cfe800814d699f82a488c8c65a628c55c5cc2ab 100644 (file)
@@ -1333,21 +1333,62 @@ class Connection(Connectable):
         return self.execute(object, *multiparams, **params).scalar()
 
     def execute(self, object, *multiparams, **params):
-        """Executes the given construct and returns a :class:`.ResultProxy`.
-
-        The construct can be one of:
-
-        * a textual SQL string
-        * any :class:`.ClauseElement` construct that is also
-          a subclass of :class:`.Executable`, such as a 
-          :func:`expression.select` construct
-        * a :class:`.FunctionElement`, such as that generated
-          by :attr:`.func`, will be automatically wrapped in
-          a SELECT statement, which is then executed.
-        * a :class:`.DDLElement` object
-        * a :class:`.DefaultGenerator` object
-        * a :class:`.Compiled` object
-
+        """Executes the a SQL statement construct and returns a :class:`.ResultProxy`.
+
+        :param object: The statement to be executed.  May be 
+         one of:
+
+         * a plain string
+         * any :class:`.ClauseElement` construct that is also
+           a subclass of :class:`.Executable`, such as a 
+           :func:`~.expression.select` construct
+         * a :class:`.FunctionElement`, such as that generated
+           by :attr:`.func`, will be automatically wrapped in
+           a SELECT statement, which is then executed.
+         * a :class:`.DDLElement` object
+         * a :class:`.DefaultGenerator` object
+         * a :class:`.Compiled` object
+        
+        :param \*multiparams/\**params: represent bound parameter
+         values to be used in the execution.   Typically,
+         the format is either a collection of one or more
+         dictionaries passed to \*multiparams::
+         
+             conn.execute(
+                 table.insert(), 
+                 {"id":1, "value":"v1"},
+                 {"id":2, "value":"v2"}
+             )
+         
+         ...or individual key/values interpreted by \**params::
+         
+             conn.execute(
+                 table.insert(), id=1, value="v1"
+             )
+         
+         In the case that a plain SQL string is passed, and the underlying 
+         DBAPI accepts positional bind parameters, a collection of tuples
+         or individual values in \*multiparams may be passed::
+             conn.execute(
+                 "INSERT INTO table (id, value) VALUES (?, ?)",
+                 (1, "v1"), (2, "v2")
+             )
+         
+             conn.execute(
+                 "INSERT INTO table (id, value) VALUES (?, ?)",
+                 1, "v1"
+             )
+         
+         Note above, the usage of a question mark "?" or other
+         symbol is contingent upon the "paramstyle" accepted by the DBAPI 
+         in use, which may be any of "qmark", "named", "pyformat", "format",
+         "numeric".   See `pep-249 <http://www.python.org/dev/peps/pep-0249/>`_ 
+         for details on paramstyle.
+         
+         To execute a textual SQL statement which uses bound parameters in a
+         DBAPI-agnostic way, use the :func:`~.expression.text` construct.
+        
         """
         for c in type(object).__mro__:
             if c in Connection.executors:
index fe38f01be8314aa4bc3a394228d4f43777771799..ce4d96591ea72c7068e84ab8f6b8f86e8c1ee5fd 100644 (file)
@@ -713,6 +713,9 @@ class Session(object):
         passed, it is first converted to a
         :func:`~.sql.expression.text` construct, which here means
         that bind parameters should be specified using the format ``:param``.
+        If raw DBAPI statement execution is desired, use :meth:`.Session.connection`
+        to acquire a :class:`.Connection`, then call its :meth:`~.Connection.execute`
+        method.
 
         The statement is executed within the current transactional context of
         this :class:`.Session`, using the same behavior as that of