Compile this SQL expression. The return value is a :class:`~.Compiled` object. Calling ``str()`` or ``unicode()`` on the returned value will yield a string representation of the result. The :class:`~.Compiled` object also can return a dictionary of bind param
(
self,
bind: Optional[_HasDialect] = None,
dialect: Optional[Dialect] = None,
**kw: Any,
)
| 248 | @util.preload_module("sqlalchemy.engine.default") |
| 249 | @util.preload_module("sqlalchemy.engine.url") |
| 250 | def compile( |
| 251 | self, |
| 252 | bind: Optional[_HasDialect] = None, |
| 253 | dialect: Optional[Dialect] = None, |
| 254 | **kw: Any, |
| 255 | ) -> Compiled: |
| 256 | """Compile this SQL expression. |
| 257 | |
| 258 | The return value is a :class:`~.Compiled` object. |
| 259 | Calling ``str()`` or ``unicode()`` on the returned value will yield a |
| 260 | string representation of the result. The |
| 261 | :class:`~.Compiled` object also can return a |
| 262 | dictionary of bind parameter names and values |
| 263 | using the ``params`` accessor. |
| 264 | |
| 265 | :param bind: An :class:`.Connection` or :class:`.Engine` which |
| 266 | can provide a :class:`.Dialect` in order to generate a |
| 267 | :class:`.Compiled` object. If the ``bind`` and |
| 268 | ``dialect`` parameters are both omitted, a default SQL compiler |
| 269 | is used. |
| 270 | |
| 271 | :param column_keys: Used for INSERT and UPDATE statements, a list of |
| 272 | column names which should be present in the VALUES clause of the |
| 273 | compiled statement. If ``None``, all columns from the target table |
| 274 | object are rendered. |
| 275 | |
| 276 | :param dialect: A :class:`.Dialect` instance which can generate |
| 277 | a :class:`.Compiled` object. This argument takes precedence over |
| 278 | the ``bind`` argument. |
| 279 | |
| 280 | :param compile_kwargs: optional dictionary of additional parameters |
| 281 | that will be passed through to the compiler within all "visit" |
| 282 | methods. This allows any custom flag to be passed through to |
| 283 | a custom compilation construct, for example. It is also used |
| 284 | for the case of passing the ``literal_binds`` flag through:: |
| 285 | |
| 286 | from sqlalchemy.sql import table, column, select |
| 287 | |
| 288 | t = table("t", column("x")) |
| 289 | |
| 290 | s = select(t).where(t.c.x == 5) |
| 291 | |
| 292 | print(s.compile(compile_kwargs={"literal_binds": True})) |
| 293 | |
| 294 | .. seealso:: |
| 295 | |
| 296 | :ref:`faq_sql_expression_string` |
| 297 | |
| 298 | """ |
| 299 | |
| 300 | if dialect is None: |
| 301 | if bind: |
| 302 | dialect = bind.dialect |
| 303 | elif self.stringify_dialect == "default": |
| 304 | dialect = self._default_dialect() |
| 305 | else: |
| 306 | url = util.preloaded.engine_url |
| 307 | dialect = url.URL.create( |
no test coverage detected