MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / DDL

Class DDL

lib/sqlalchemy/sql/ddl.py:354–438  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

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

Callers 15

test_viewsMethod · 0.90
test_fetchid_triggerMethod · 0.90
define_tablesMethod · 0.90
insert_dataMethod · 0.90
define_tablesMethod · 0.90
_run_testMethod · 0.90
define_tablesMethod · 0.90
define_temp_tablesMethod · 0.85
define_tablesMethod · 0.85
define_temp_tablesMethod · 0.85
define_viewsMethod · 0.85

Calls

no outgoing calls

Tested by 15

test_viewsMethod · 0.72
test_fetchid_triggerMethod · 0.72
define_tablesMethod · 0.72
insert_dataMethod · 0.72
define_tablesMethod · 0.72
_run_testMethod · 0.72
define_tablesMethod · 0.72
define_temp_tablesMethod · 0.68
define_tablesMethod · 0.68
define_temp_tablesMethod · 0.68
define_viewsMethod · 0.68