A literal DDL statement. Specifies literal SQL DDL to be executed by the database. DDL objects function as DDL event listeners, and can be subscribed to those events listed in :class:`.DDLEvents`, using either :class:`_schema.Table` or :class:`_schema.MetaData` objects as targets.
| 337 | |
| 338 | |
| 339 | class DDL(ExecutableDDLElement): |
| 340 | """A literal DDL statement. |
| 341 | |
| 342 | Specifies literal SQL DDL to be executed by the database. DDL objects |
| 343 | function as DDL event listeners, and can be subscribed to those events |
| 344 | listed in :class:`.DDLEvents`, using either :class:`_schema.Table` or |
| 345 | :class:`_schema.MetaData` objects as targets. |
| 346 | Basic templating support allows |
| 347 | a single DDL instance to handle repetitive tasks for multiple tables. |
| 348 | |
| 349 | Examples:: |
| 350 | |
| 351 | from sqlalchemy import event, DDL |
| 352 | |
| 353 | tbl = Table("users", metadata, Column("uid", Integer)) |
| 354 | event.listen(tbl, "before_create", DDL("DROP TRIGGER users_trigger")) |
| 355 | |
| 356 | spow = DDL("ALTER TABLE %(table)s SET secretpowers TRUE") |
| 357 | event.listen(tbl, "after_create", spow.execute_if(dialect="somedb")) |
| 358 | |
| 359 | drop_spow = DDL("ALTER TABLE users SET secretpowers FALSE") |
| 360 | connection.execute(drop_spow) |
| 361 | |
| 362 | When operating on Table events, the following ``statement`` |
| 363 | string substitutions are available: |
| 364 | |
| 365 | .. sourcecode:: text |
| 366 | |
| 367 | %(table)s - the Table name, with any required quoting applied |
| 368 | %(schema)s - the schema name, with any required quoting applied |
| 369 | %(fullname)s - the Table name including schema, quoted if needed |
| 370 | |
| 371 | The DDL's "context", if any, will be combined with the standard |
| 372 | substitutions noted above. Keys present in the context will override |
| 373 | the standard substitutions. |
| 374 | |
| 375 | """ |
| 376 | |
| 377 | __visit_name__ = "ddl" |
| 378 | |
| 379 | def __init__(self, statement, context=None): |
| 380 | """Create a DDL statement. |
| 381 | |
| 382 | :param statement: |
| 383 | A string or unicode string to be executed. Statements will be |
| 384 | processed with Python's string formatting operator using |
| 385 | a fixed set of string substitutions, as well as additional |
| 386 | substitutions provided by the optional :paramref:`.DDL.context` |
| 387 | parameter. |
| 388 | |
| 389 | A literal '%' in a statement must be escaped as '%%'. |
| 390 | |
| 391 | SQL bind parameters are not available in DDL statements. |
| 392 | |
| 393 | :param context: |
| 394 | Optional dictionary, defaults to None. These values will be |
| 395 | available for use in string substitutions on the DDL statement. |
| 396 |
no outgoing calls